buff-imp-parsed-xml.lua /size: 3536 b    last modification: 2020-07-01 14:35
1
if
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 9
local
next
=
next
10
local
format
=
string
.
format
11 12
local
context
=
context
13
local
verbatim
=
context
.
verbatim
14 15
local
write
=
visualizers
.
write
16
local
writespace
=
visualizers
.
writespace
17
local
writeargument
=
visualizers
.
writeargument
18 19
local
ParsedXmlSnippetKey
=
context
.
ParsedXmlSnippetKey
20
local
ParsedXmlSnippetValue
=
context
.
ParsedXmlSnippetValue
21 22
local
ParsedXmlSnippetElement
=
verbatim
.
ParsedXmlSnippetElement
23
local
ParsedXmlSnippetInstruction
=
verbatim
.
ParsedXmlSnippetInstruction
24
local
ParsedXmlSnippetComment
=
verbatim
.
ParsedXmlSnippetComment
25
local
ParsedXmlSnippetCdata
=
verbatim
.
ParsedXmlSnippetCdata
26
local
ParsedXmlSnippetDoctype
=
verbatim
.
ParsedXmlSnippetDoctype
27 28
local
startParsedXmlSnippet
=
context
.
startParsedXmlSnippet
29
local
stopParsedXmlSnippet
=
context
.
stopParsedXmlSnippet
30 31
local
parsedxmlhandler
=
xml
.
newhandlers
{
-- todo: treat spaces and tabs
32
name
=
"
parsedxml
"
,
33
handle
=
function
(
...
)
34
print
(
"
error:
"
,
...
)
-- we need a handler as fallback, even if not used
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 82
local
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
93
end
94 95
local
function
parser
(
str
,
settings
)
96
parsedxml
(
xml
.
convert
(
string
.
strip
(
str
)
)
,
settings
and
settings
.
pattern
)
97
end
98 99
visualizers
.
parsedxml
=
parsedxml
-- for use at the lua end (maybe namespace needed)
100 101
visualizers
.
register
(
"
parsed-xml
"
,
{
parser
=
parser
}
)
102 103