-
Notifications
You must be signed in to change notification settings - Fork 10
/
luaxml.sty
142 lines (119 loc) · 3.47 KB
/
luaxml.sty
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
\ProvidesExplPackage{luaxml}{{{DATE}}}{{{VERSION}}}{LuaXML package}
% \ProvidesPackage{luaxml}[{{DATE}} LuaXML package]
\RequirePackage{luacode}
% ToDo: add support for use of transformation rules for other XML syntaxes -- using LaTeX command?
\begin{luacode*}
luaxml_sty = require "luaxml-sty"
\end{luacode*}
\ExplSyntaxOn
% package options
\bool_new:N\l_luaxml_use_defaults
\keys_define:nn {luaxml}{
default .bool_set:N = \l_luaxml_use_defaults,
default .initial:n = false
}
\ProcessKeyOptions
% options used in \LXMLRule
\bool_new:N\l_luaxml_verbatim
\tl_new:N \l_luaxml_transformer
\keys_define:nn{luaxmlrule}{
verbatim .bool_set:N = \l_luaxml_verbatim ,
verbatim .initial:n = false,
unknown .code:n = \tl_set_eq:NN\l_luaxml_transformer\l_keys_key_str,
transformer .tl_set:N = \l_luaxml_transformer,
}
% select current transformer object
\NewDocumentCommand\LXMLUseTransformer{m}{
\directlua{
luaxml_sty.current.transformation = "#1",
}}
% declare new transformer object
\NewDocumentCommand\LXMLDeclareTransformer{m}{
\directlua{
% luaxml_sty.transformations["#1"] = luaxml_sty.packages.transform.new()
luaxml_sty.declare_transformer("#1")
}
}
% add transformer rule
% #1 options (verbatim, transformer, unknown key = transformer)
% #2 CSS selector
% #3 transformer rule
\NewDocumentCommand\LXMLRule{O{} m +v}{
% handle keyval options
\group_begin:
\keys_set:nn {luaxmlrule} { #1 }
\directlua{luaxml_sty.current.parameters = {}}
\bool_if:NT \l_luaxml_verbatim {\directlua{luaxml_sty.current.parameters.verbatim = true}}
\directlua{
luaxml_sty.add_rule("\l_luaxml_transformer", "#2", "\luaescapestring{#3}")
}
\group_end:
}
% Transform code snippet
% \LXMLSnippet* - parse using XML parser
% \LXMLSnippet - parse using HTML parser
% #2 transformer object name -- empty = default
% #3 XML string
\NewDocumentCommand\LXMLSnippet{s O{} m}{
\IfBooleanTF{#1}{
\directlua{luaxml_sty.set_html()}
}{
\directlua{luaxml_sty.set_xml()}
}
\directlua{
luaxml_sty.parse_snippet("\luaescapestring{#2}", [[\detokenize{#3}]])
}
}
% Transform file
% \LXMLInputFile* - parse using XML parser
% \LXMLInputFile - parse using HTML parser
% #2 transformer object name -- empty = default
% #3 XML string
\NewDocumentCommand\LXMLInputFile{s O{} m}{
\IfBooleanTF{#1}{
\directlua{luaxml_sty.set_html()}
}{
\directlua{luaxml_sty.set_xml()}
}
\directlua{
luaxml_sty.parse_file("\luaescapestring{#2}", "\luaescapestring{#3}")
}
}
% Transform XML contained in the environment
% #1 transformer object name -- empty = default
\newenvironment{LXMLCode}[1]{
\directlua{
luaxml_sty.set_xml()
luaxml_sty.register_verbatim("LXMLCode")
}
\tl_set:Nn\l_luaxml_transformer{#1}
}{
\directlua{
luaxml_sty.print_verbatim("\l_luaxml_transformer")
}
}
% Transform HTML contained in the environment
% #1 transformer object name -- empty = default
%
% I couldn't process the first line in the environment using the Lua callback with \NewDocumentEnvironment,
% so \newenvironment is used instead.
\newenvironment{LXMLCode*}[1]{
\directlua{
luaxml_sty.set_html()
luaxml_sty.register_verbatim("LXMLCode*")
}
\tl_set:Nn\l_luaxml_transformer{#1}
}{
\directlua{
luaxml_sty.print_verbatim("\l_luaxml_transformer")
}
}
% load default transformation rules
\NewDocumentCommand\LXMLLoadDefaults{}{
\directlua{require "luaxml-htmltemplates"}
}
\AtBeginDocument{
\bool_if:NT \l_luaxml_use_defaults {\LXMLLoadDefaults}
}
\ExplSyntaxOff
\endinput