forked from msva/lua-htmlparser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtmlparser.lua
More file actions
165 lines (134 loc) · 4.52 KB
/
htmlparser.lua
File metadata and controls
165 lines (134 loc) · 4.52 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
-- vim: ft=lua ts=2
local esc = function(s) return string.gsub(s, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%" .. "%1") end
local str = tostring
local char = string.char
local err = function(s) io.stderr:write(s) end
local out = function(s) io.stdout:write(s) end
local ElementNode = require("htmlparser.ElementNode")
local voidelements = require("htmlparser.voidelements")
local HtmlParser = {}
local tpl_rep={
-- Replace table for template engines syntax that can confuse us.
-- Here we're replacing confusing sequences
-- (things looking like tags, but appearing where tags can't)
-- with definitelly invalid utf sequence, and later we'll replace them back
["<%"] = char(208,209),
["%>"] = char(209,208),
}
local tpl_rep_rev = {}
local function parse(text)
local text=str(text)
local limit = limit or htmlparser_looplimit or 1000
local tpl = false
for k,v in pairs(tpl_rep) do
local mtc="("..esc(k)..")"
if text:match(mtc) then
tpl=true
text=text:gsub(mtc,tpl_rep)
tpl_rep_rev[v]=k;
end
end
local index = 0
local root = ElementNode:new(index, str(text))
local node, descend, tpos, opentags = root, true, 1, {}
while true do
if index == limit then
err("[HTMLParser] [ERR] Main loop reached loop limit ("..limit.."). Please, consider increasing it or check the code for errors")
break
end
local openstart, name, textcontent
openstart, tpos, name = root._text:find(
"<" .. -- an uncaptured starting "<"
"([%w-]+)" .. -- name = the first word, directly following the "<"
"[^>]*>", -- include, but not capture everything up to the next ">"
tpos)
if not name then break end
index = index + 1
local tag = ElementNode:new(index, str(name), node, descend, openstart, tpos)
node = tag
local tagloop
local tagst, apos = tag:gettext(), 1
while true do
if tagloop == limit then
err("[HTMLParser] [ERR] tag parsing loop reached loop limit ("..limit.."). Please, consider increasing it or check the code for errors")
break
end
local start, k, eq, quote, v
start, apos, k, eq, quote = tagst:find(
"%s+" .. -- some uncaptured space
"([^%s=/>]+)" .. -- k = an unspaced string up to an optional "=" or the "/" or ">"
"(=?)" .. -- eq = the optional; "=", else ""
"(['\"]?)", -- quote = an optional "'" or '"' following the "=", or ""
apos)
if not k or k == "/>" or k == ">" then break end
if eq == "=" then
local pattern = "=([^%s>]*)"
if quote ~= "" then
pattern = quote .. "([^" .. quote .. "]*)" .. quote
end
start, apos, v = tagst:find(pattern, apos)
end
v=v or ""
if tpl then
for rk,rv in pairs(tpl_rep_rev) do
local mtc="("..esc(rk)..")"
if text:match(mtc) then
v = v:gsub(mtc,tpl_rep_rev)
end
end
end
tag:addattribute(k, v)
tagloop = (tagloop or 0) + 1
end
if voidelements[tag.name:lower()] then
descend = false
tag:close()
else
opentags[tag.name] = opentags[tag.name] or {}
table.insert(opentags[tag.name], tag)
descend = true
end
local closeend = tpos
local textend = tpos
local closingloop
while true do
if closingloop == limit then
err("[HTMLParser] [ERR] tag closing loop reached loop limit ("..limit.."). Please, consider increasing it or check the code for errors")
break
end
local closestart, closing, closename
closestart, closeend, closing, closename = root._text:find("[^<]*<(/?)([%w-]+)", closeend)
-- Feature: Wrap a text node in to current or parent node
-- TODO: Β etc. did not handled yet, create a ElementNode function to handle them?
do
local textstart
textstart , textend, textcontent = root._text:find(">([^<]*)", closestart)
textcontent = string.gsub(textcontent, "[\r\n%s]*", '')
textcontent = string.gsub(textcontent, " ", '')
if textcontent ~= '' then
index = index + 1
local textTag = ElementNode:new(index, 'text', node, descend, textstart+1, textend)
textTag:close()
end
end
if not closing or closing == "" then break end
tag = table.remove(opentags[closename] or {}) or tag -- kludges for the cases of closing void or non-opened tags
closestart = root._text:find("<", closestart)
tag:close(closestart, closeend + 1)
node = tag.parent
descend = true
closingloop = (closingloop or 0) + 1
end
end
if tpl then
for k,v in pairs(tpl_rep_rev) do
local mtc="("..esc(k)..")"
if text:match(mtc) then
root._text = root._text:gsub(mtc,tpl_rep_rev)
end
end
end
return root
end
HtmlParser.parse = parse
return HtmlParser