5 strip_escapes
= function (self
, s
)
6 s
= string.gsub(s
, '>', '>')
7 s
= string.gsub(s
, '<', '<')
11 parseargs
= function (self
, s
)
13 string.gsub(s
, "([%w_]+)=([\"'])(.-)%2", function (w
, _
, a
)
14 arg
[self
:strip_escapes(w
)] = self
:strip_escapes(a
)
19 collect
= function (self
, s
)
22 table.insert(stack
, top
)
23 local ni
,c
,label
,xarg
, empty
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
)
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
)}
39 table.insert(stack
, top
) -- new level
41 local toclose
= table.remove(stack
) -- remove top
44 error("nothing to close with "..label
)
46 if toclose
.tag ~= label
then
47 error("trying to close "..toclose
.label
.." with "..label
)
49 table.insert(top
, toclose
)
52 for a
, v
in pairs(inserted
.attr
) do
53 if type(self
[a
])=='table' then
60 local text
= string.sub(s
, i
)
61 if not string.find(text
, "^%s*$") then
62 table.insert(stack
[stack
.n
], text
)
65 error("unclosed "..stack
[stack
.n
].label
)