FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
lua-htmlparser/src/htmlparser.lua at master · lineCode/lua-htmlparser · GitHub
lineCode
/
lua-htmlparser
Public
forked from
msva/lua-htmlparser
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
lua-htmlparser
/
src
/
htmlparser.lua
Copy path
More file actions
More file actions
Latest commit
History
History
History
163 lines (132 loc) · 4 KB
Breadcrumbs
lua-htmlparser
/
src
/
htmlparser.lua
Copy path
File metadata and controls
163 lines (132 loc) · 4 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
--
vim: ft=lua ts=2 sw=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
tpr
=
{
--
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
,
208
,
209
),
[
"
>
"
]
=
char
(
209
,
208
,
209
,
208
),
}
local
function
parse
(
text
,
limit
)
local
text
=
str
(
text
)
local
limit
=
limit
or
htmlparser_looplimit
or
1000
local
tpl
=
false
local
function
g
(
id
,...)
local
arg
=
{
...
}
arg
[
id
]
=
tpr
[
arg
[
id
]]
tpl
=
true
return
table.concat
(
arg
)
end
text
=
text
:
gsub
(
"
(<)
"
..
"
([^>]-)
"
..
"
(<)
"
,
function
(...)
return
g
(
3
,
...
)
end
):
gsub
(
"
(
"
..
tpr
[
"
<
"
]
..
"
)
"
..
"
([^%w%s])
"
..
"
([^%2]-)
"
..
"
(%2)
"
..
"
(>)
"
..
"
([^>]-)
"
..
"
(>)
"
,
function
(...)
return
g
(
5
,
...
)
end
):
gsub
(
[=[
(['"])
]=]
..
[=[
([^'">%s]-)
]=]
..
"
(>)
"
..
[=[
([^'">%s]-)
]=]
..
[=[
(['"])
]=]
,
function
(...)
return
g
(
3
,
...
)
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
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
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
(
tpr
)
do
v
=
v
:
gsub
(
rv
,
rk
)
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
)
end
local
closeend
=
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
)
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
(
tpr
)
do
root
.
_text
=
root
.
_text
:
gsub
(
v
,
k
)
end
end
return
root
end
HtmlParser
.
parse
=
parse
return
HtmlParser
Back
|
FazBrowse Home
|
New Git URL