1if not modules then modules = { } end modules [ ' buff-imp-parsed-xml ' ] = {
2 version = 1 . 001 ,
3 comment = " companion to buff-imp-parsed-xml.mkiv " ,
4 author = " Hans Hagen, PRAGMA-ADE, Hasselt NL " ,
5 copyright = " PRAGMA ADE / ConTeXt Development Team " ,
6 license = " see context related readme files "
7}
8
9local next = next
10local format = string . format
11
12local context = context
13local verbatim = context . verbatim
14
15local write = visualizers . write
16local writespace = visualizers . writespace
17local writeargument = visualizers . writeargument
18
19local ParsedXmlSnippetKey = context . ParsedXmlSnippetKey
20local ParsedXmlSnippetValue = context . ParsedXmlSnippetValue
21
22local ParsedXmlSnippetElement = verbatim . ParsedXmlSnippetElement
23local ParsedXmlSnippetInstruction = verbatim . ParsedXmlSnippetInstruction
24local ParsedXmlSnippetComment = verbatim . ParsedXmlSnippetComment
25local ParsedXmlSnippetCdata = verbatim . ParsedXmlSnippetCdata
26local ParsedXmlSnippetDoctype = verbatim . ParsedXmlSnippetDoctype
27
28local startParsedXmlSnippet = context . startParsedXmlSnippet
29local stopParsedXmlSnippet = context . stopParsedXmlSnippet
30
31local parsedxmlhandler = xml . newhandlers {
32 name = " parsedxml " ,
33 handle = function ( ... )
34 print ( " error: " , ... )
35 end ,
36 functions = {
37 [ " @el@ " ] = function ( e , handler )
38 local at = e . at
39 if at and next ( at ) then
40 ParsedXmlSnippetElement ( format ( " <%s " , e . tg ) )
41 for k , v in next , at do
42 writespace ( )
43 ParsedXmlSnippetKey ( )
44 writeargument ( k )
45 verbatim ( " = " )
46 ParsedXmlSnippetValue ( )
47 writeargument ( format ( " %q " , k ) )
48 end
49 ParsedXmlSnippetElement ( " > " )
50 else
51 ParsedXmlSnippetElement ( format ( " <%s> " , e . tg ) )
52 end
53 handler . serialize ( e . dt , handler )
54 ParsedXmlSnippetElement ( format ( " </%s> " , e . tg ) )
55 end ,
56 [ " @pi@ " ] = function ( e , handler )
57 ParsedXmlSnippetInstruction ( " <? " )
58 write ( e . dt [ 1 ] )
59 ParsedXmlSnippetInstruction ( " ?> " )
60 end ,
61 [ " @cm@ " ] = function ( e , handler )
62 ParsedXmlSnippetComment ( " <!-- " )
63 write ( e . dt [ 1 ] )
64 ParsedXmlSnippetComment ( " --> " )
65 end ,
66 [ " @cd@ " ] = function ( e , handler )
67 ParsedXmlSnippetCdata ( " <![CDATA[ " )
68 write ( e . dt [ 1 ] )
69 ParsedXmlSnippetCdata ( " ]]> " )
70 end ,
71 [ " @dt@ " ] = function ( e , handler )
72 ParsedXmlSnippetDoctype ( " <!DOCTYPE " )
73 write ( e . dt [ 1 ] )
74 ParsedXmlSnippetDoctype ( " > " )
75 end ,
76 [ " @tx@ " ] = function ( s , handler )
77 write ( s )
78 end ,
79 }
80}
81
82local function parsedxml ( root , pattern )
83 if root then
84 if pattern then
85 root = xml . filter ( root , pattern )
86 end
87 if root then
88 context . startParsedXmlSnippet ( )
89 xml . serialize ( root , parsedxmlhandler )
90 context . stopParsedXmlSnippet ( )
91 end
92 end
93end
94
95local function parser ( str , settings )
96 parsedxml ( xml . convert ( string . strip ( str ) ) , settings and settings . pattern )
97end
98
99visualizers . parsedxml = parsedxml
100
101visualizers . register ( " parsed-xml " , { parser = parser } )
102
103 |