Debian Codegraph @

texmf-dist/tex/luatex/luaxml/luaxml-mod-handler.lua

raw
1 --..module(...,package.seeall)
2 --
3 -- Overview:
4 -- =========
5 -- Standard XML event handler(s) for XML parser module (xml.lua)
6 --
7 -- Features:
8 -- =========
9 -- printHandler - Generate XML event trace
10 -- domHandler - Generate DOM-like node tree
11 -- simpleTreeHandler - Generate 'simple' node tree
12 -- simpleTeXhandler - SAX like handler with support for CSS selectros
13 --
14 -- API:
15 -- ====
16 -- Must be called as handler function from xmlParser
17 -- and implement XML event callbacks (see xmlParser.lua
18 -- for callback API definition)
19 --
20 -- printHandler:
21 -- -------------
22 --
23 -- printHandler prints event trace for debugging
24 --
25 -- domHandler:
26 -- -----------
27 --
28 -- domHandler generates a DOM-like node tree structure with
29 -- a single ROOT node parent - each node is a table comprising
30 -- fields below.
31 --
32 -- node = { _name = <Element Name>,
33 -- _type = ROOT|ELEMENT|TEXT|COMMENT|PI|DECL|DTD,
34 -- _attr = { Node attributes - see callback API },
35 -- _parent = <Parent Node>
36 -- _children = { List of child nodes - ROOT/NODE only }
37 -- }
38 --
39 -- The dom structure is capable of representing any valid XML document
40 --
41 -- simpleTreeHandler
42 -- -----------------
43 --
44 -- simpleTreeHandler is a simplified handler which attempts
45 -- to generate a more 'natural' table based structure which
46 -- supports many common XML formats.
47 --
48 -- The XML tree structure is mapped directly into a recursive
49 -- table structure with node names as keys and child elements
50 -- as either a table of values or directly as a string value
51 -- for text. Where there is only a single child element this
52 -- is inserted as a named key - if there are multiple
53 -- elements these are inserted as a vector (in some cases it
54 -- may be preferable to always insert elements as a vector
55 -- which can be specified on a per element basis in the
56 -- options). Attributes are inserted as a child element with
57 -- a key of '_attr'.
58 --
59 -- Only Tag/Text & CDATA elements are processed - all others
60 -- are ignored.
61 --
62 -- This format has some limitations - primarily
63 --
64 -- * Mixed-Content behaves unpredictably - the relationship
65 -- between text elements and embedded tags is lost and
66 -- multiple levels of mixed content does not work
67 -- * If a leaf element has both a text element and attributes
68 -- then the text must be accessed through a vector (to
69 -- provide a container for the attribute)
70 --
71 -- In general however this format is relatively useful.
72 --
73 -- It is much easier to understand by running some test
74 -- data through 'textxml.lua -simpletree' than to read this)
75 --
76 -- Options
77 -- =======
78 -- simpleTreeHandler.options.noReduce = { <tag> = bool,.. }
79 --
80 -- - Nodes not to reduce children vector even if only
81 -- one child
82 --
83 -- domHandler.options.(comment|pi|dtd|decl)Node = bool
84 --
85 -- - Include/exclude given node types
86 --
87 -- Usage
88 -- =====
89 -- Passed as delegate in xmlParser constructor and called
90 -- as callback by xmlParser:parse(xml) method.
91 --
92 -- See textxml.lua for examples
93 -- License:
94 -- ========
95 --
96 -- This code is freely distributable under the terms of the Lua license
97 -- (<a href="http://www.lua.org/copyright.html">http://www.lua.org/copyright.html</a>)
98 --
99 -- History
100 -- =======
101 -- $Id: handler.lua,v 1.1.1.1 2001/11/28 06:11:33 paulc Exp $
102 --
103 -- $Log: handler.lua,v $
104 -- Revision 1.1.1.1 2001/11/28 06:11:33 paulc
105 -- Initial Import
106 --@author Paul Chakravarti ([email protected])<p/>
107
108
109 ---Handler to generate a string prepresentation of a table
110 --Convenience function for printHandler (Does not support recursive tables).
111 --@param t Table to be parsed
112 --@returns Returns a string representation of table
113
114 local M = {}
115
116 local stack
117 local entities
118 if kpse then
119 stack = require("luaxml-stack")
120 entities = require("luaxml-entities")
121 else
122 stack = require("luaxml.stack")
123 entities = require("luaxml.entities")
124 end
125
126 local function showTable(t)
127 local sep = ''
128 local res = ''
129 if type(t) ~= 'table' then
130 return t
131 end
132 for k,v in pairs(t) do
133 if type(v) == 'table' then
134 v = showTable(v)
135 end
136 res = res .. sep .. string.format("%s=%s",k,v)
137 sep = ','
138 end
139 res = '{'..res..'}'
140 return res
141 end
142
143
144 M.showTable = showTable
145
146 ---Handler to generate a simple event trace
147 local printHandler = function()
148 local obj = {}
149 obj.starttag = function(self,t,a,s,e)
150 io.write("Start : "..t.."\n")
151 if a then
152 for k,v in pairs(a) do
153 io.write(string.format(" + %s='%s'\n",k,v))
154 end
155 end
156 end
157 obj.endtag = function(self,t,s,e)
158 io.write("End : "..t.."\n")
159 end
160 obj.text = function(self,t,s,e)
161 io.write("Text : "..t.."\n")
162 end
163 obj.cdata = function(self,t,s,e)
164 io.write("CDATA : "..t.."\n")
165 end
166 obj.comment = function(self,t,s,e)
167 io.write("Comment : "..t.."\n")
168 end
169 obj.dtd = function(self,t,a,s,e)
170 io.write("DTD : "..t.."\n")
171 if a then
172 for k,v in pairs(a) do
173 io.write(string.format(" + %s='%s'\n",k,v))
174 end
175 end
176 end
177 obj.pi = function(self,t,a,s,e)
178 io.write("PI : "..t.."\n")
179 if a then
180 for k,v in pairs(a) do
181 io. write(string.format(" + %s='%s'\n",k,v))
182 end
183 end
184 end
185 obj.decl = function(self,t,a,s,e)
186 io.write("XML Decl : "..t.."\n")
187 if a then
188 for k,v in pairs(a) do
189 io.write(string.format(" + %s='%s'\n",k,v))
190 end
191 end
192 end
193 return obj
194 end
195 M.printHandler = printHandler
196 ---Handler to generate a lua table from a XML content string
197 local function simpleTreeHandler()
198 local obj = {}
199
200 obj.root = {}
201 obj.stack = {obj.root;n=1}
202 obj.options = {noreduce = {}}
203
204 obj.reduce = function(self,node,key,parent)
205 -- Recursively remove redundant vectors for nodes
206 -- with single child elements
207 for k,v in pairs(node) do
208 if type(v) == 'table' then
209 self:reduce(v,k,node)
210 end
211 end
212 if #node == 1 and not self.options.noreduce[key] and
213 node._attr == nil then
214 parent[key] = node[1]
215 else
216 node.n = nil
217 end
218 end
219
220 obj.starttag = function(self,t,a)
221 local node = {}
222 if self.parseAttributes == true then
223 node._attr=a
224 end
225
226 local current = self.stack[#self.stack]
227 if current[t] then
228 table.insert(current[t],node)
229 else
230 current[t] = {node;n=1}
231 end
232 table.insert(self.stack,node)
233 end
234
235 obj.endtag = function(self,t,s)
236 local current = self.stack[#self.stack]
237 local prev = self.stack[#self.stack-1]
238 if not prev[t] then
239 error("XML Error - Unmatched Tag ["..s..":"..t.."]\n")
240 end
241 if prev == self.root then
242 -- Once parsing complete recursively reduce tree
243 self:reduce(prev,nil,nil)
244 end
245 table.remove(self.stack)
246 end
247
248 obj.text = function(self,t)
249 local current = self.stack[#self.stack]
250 table.insert(current,t)
251 end
252
253 obj.cdata = obj.text
254
255 return obj
256 end
257
258 M.simpleTreeHandler = simpleTreeHandler
259
260 --- domHandler
261 local function domHandler()
262 local obj = {}
263 local decode = entities.decode
264 obj.options = {commentNode=1,piNode=1,dtdNode=1,declNode=1, voidElements = {}}
265 obj.root = { _children = {n=0}, _type = "ROOT" }
266 obj.current = obj.root
267 obj.starttag = function(self,t,a)
268 local newattr
269 if a then
270 newattr = {}
271 for k,v in pairs(a) do
272 newattr[k] = decode(v)
273 end
274 end
275 local node = { _type = 'ELEMENT',
276 _name = t,
277 _attr = newattr,
278 _parent = self.current,
279 _children = {n=0} }
280 table.insert(self.current._children,node)
281 -- close void element
282 if not self.options.voidElements[t] then
283 self.current = node
284 else
285 table.remove(self._xml._stack)
286 end
287 end
288 obj.endtag = function(self,t,s)
289 if not self.options.voidElements[t] then
290 if t ~= self.current._name then
291 error("XML Error - Unmatched Tag ["..s..":"..t.."]\n")
292 end
293 self.current = self.current._parent
294 end
295 end
296 obj.text = function(self,t)
297 local text = decode(t)
298 local node = {
299 _type = "TEXT",
300 _parent = self.current,
301 _text = text
302 }
303 table.insert(self.current._children,node)
304 end
305 obj.comment = function(self,t)
306 if self.options.commentNode then
307 local node = { _type = "COMMENT",
308 _parent = self.current,
309 _text = t }
310 table.insert(self.current._children,node)
311 end
312 end
313 obj.pi = function(self,t,a)
314 if self.options.piNode then
315 local node = { _type = "PI",
316 _name = t,
317 _attr = a,
318 _parent = self.current }
319 table.insert(self.current._children,node)
320 end
321 end
322 obj.decl = function(self,t,a)
323 if self.options.declNode then
324 local node = { _type = "DECL",
325 _name = t,
326 _attr = a,
327 _parent = self.current }
328 table.insert(self.current._children,node)
329 end
330 end
331 obj.dtd = function(self,t,a)
332 if self.options.dtdNode then
333 local node = { _type = "DTD",
334 _name = t,
335 _attr = a,
336 _parent = self.current }
337 table.insert(self.current._children,node)
338 end
339 end
340 obj.cdata = function(self,t)
341 local node = { _type = "CDATA",
342 _parent = self.current,
343 _text = decode(t) }
344 table.insert(self.current._children,node)
345 end
346 return obj
347 end
348 M.domHandler = domHandler
349
350 --
351 local simpleTeXhandler=function()
352 local obj={}
353 local _stack=stack.Stack:Create()
354 obj.starttag = function(self,t,a,s,e)
355 local tag = {t}
356 local getAtt = function(att)
357 if a[att] then
358 return att.."="..a[att]
359 end
360 return nil
361 end
362 if type(a) == "table" then
363 table.insert(tag,getAtt("id"))
364 table.insert(tag,getAtt("class"))
365 end
366 _stack:push("<"..table.concat(tag," ")..">")
367 io.write(_stack:join("").."\n")
368 -- io.write("Start "..t.."\n" )
369 end
370 obj.endtag = function(self,t,s,e)
371 _stack:pop()
372 -- io.write("End : "..t.."\n")
373 end
374 return obj
375 end
376 M.simpleTeXhandler = simpleTeXhandler
377 return M