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