4 Copyright (c) 2007-2008 Mauro Iazzi
6 Permission is hereby granted, free of charge, to any person
7 obtaining a copy of this software and associated documentation
8 files (the "Software"), to deal in the Software without
9 restriction, including without limitation the rights to use,
10 copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the
12 Software is furnished to do so, subject to the following
15 The above copyright notice and this permission notice shall be
16 included in all copies or substantial portions of the Software.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 OTHER DEALINGS IN THE SOFTWARE.
32 strip_escapes
= function (self
, s
)
33 s
= string.gsub(s
, '>', '>')
34 s
= string.gsub(s
, '<', '<')
38 parseargs
= function (self
, s
)
40 string.gsub(s
, "([%w_]+)=([\"'])(.-)%2", function (w
, _
, a
)
41 arg
[self
:strip_escapes(w
)] = self
:strip_escapes(a
)
46 collect
= function (self
, s
)
49 table.insert(stack
, top
)
50 local ni
,c
,label
,xarg
, empty
53 ni
,j
,c
,label
,xarg
, empty
= string.find(s
, "<(%/?)(%w+)(.-)(%/?)>", j
)
54 if not ni
then break end
55 local text
= string.sub(s
, i
, ni
-1)
56 if not string.find(text
, "^%s*$") then
57 table.insert(top
, text
)
60 if empty
== "/" then -- empty element tag
61 inserted
= {tag=label
, attr
=self
:parseargs(xarg
), empty
=1}
62 table.insert(top
, inserted
)
63 elseif c
== "" then -- start tag
64 top
= {tag=label
, attr
=self
:parseargs(xarg
)}
66 table.insert(stack
, top
) -- new level
68 local toclose
= table.remove(stack
) -- remove top
71 error("nothing to close with "..label
)
73 if toclose
.tag ~= label
then
74 error("trying to close "..toclose
.label
.." with "..label
)
76 table.insert(top
, toclose
)
79 for a
, v
in pairs(inserted
.attr
) do
80 if type(self
[a
])=='table' then
87 local text
= string.sub(s
, i
)
88 if not string.find(text
, "^%s*$") then
89 table.insert(stack
[stack
.n
], text
)
92 error("unclosed "..stack
[stack
.n
].label
)