changed macros in lqt_common.hpp to reflect new header name
[lqt.git] / xml.lua
blob9d182080ff7c3799157d66a9f48e3d0a4d9e08ab
1 #!/usr/bin/lua
3 local xml_parser = {
5 strip_escapes = function (self, s)
6 s = string.gsub(s, '>', '>')
7 s = string.gsub(s, '&lt;', '<')
8 return s
9 end,
11 parseargs = function (self, s)
12 local arg = {}
13 string.gsub(s, "([%w_]+)=([\"'])(.-)%2", function (w, _, a)
14 arg[self:strip_escapes(w)] = self:strip_escapes(a)
15 end)
16 return arg
17 end,
19 collect = function (self, s)
20 local stack = {}
21 local top = {}
22 table.insert(stack, top)
23 local ni,c,label,xarg, empty
24 local i, j = 1, 1
25 while true do
26 ni,j,c,label,xarg, empty = string.find(s, "<(%/?)(%w+)(.-)(%/?)>", j)
27 if not ni then break end
28 local text = string.sub(s, i, ni-1)
29 if not string.find(text, "^%s*$") then
30 table.insert(top, text)
31 end
32 local inserted = nil
33 if empty == "/" then -- empty element tag
34 inserted = {tag=label, attr=self:parseargs(xarg), empty=1}
35 table.insert(top, inserted)
36 elseif c == "" then -- start tag
37 top = {tag=label, attr=self:parseargs(xarg)}
38 inserted = top
39 table.insert(stack, top) -- new level
40 else -- end tag
41 local toclose = table.remove(stack) -- remove top
42 top = stack[#stack]
43 if #stack < 1 then
44 error("nothing to close with "..label)
45 end
46 if toclose.tag ~= label then
47 error("trying to close "..toclose.label.." with "..label)
48 end
49 table.insert(top, toclose)
50 end
51 if inserted then
52 for a, v in pairs(inserted.attr) do
53 if type(self[a])=='table' then
54 self[a][v] = inserted
55 end
56 end
57 end
58 i = j+1
59 end
60 local text = string.sub(s, i)
61 if not string.find(text, "^%s*$") then
62 table.insert(stack[stack.n], text)
63 end
64 if #stack > 1 then
65 error("unclosed "..stack[stack.n].label)
66 end
67 return stack[1]
68 end,
71 return xml_parser