mirror of
https://github.com/TangentFoxy/lua-htmlparser.git
synced 2025-07-29 03:22:17 +00:00
improved not and lost method chaining
fixed :not() in that it filters after all matches, preventing later selection of elements that shouldn't have been there Also, ditched the idea of returning a container node, since it was complex and didn't add much. The functionality could be reintroduced by having Set implement the __call or maybe even __index to return the combined results of all its elements.
This commit is contained in:
@@ -17,9 +17,7 @@ function ElementNode:new(nameortext, node, descend, openstart, openend)
|
|||||||
deepernodes = Set:new(),
|
deepernodes = Set:new(),
|
||||||
deeperelements = {}, deeperattributes = {}, deeperids = {}, deeperclasses = {}
|
deeperelements = {}, deeperattributes = {}, deeperids = {}, deeperclasses = {}
|
||||||
}
|
}
|
||||||
if nameortext == "container" then
|
if not node then
|
||||||
instance.root = node
|
|
||||||
elseif not node then
|
|
||||||
instance.name = "root"
|
instance.name = "root"
|
||||||
instance.root = instance
|
instance.root = instance
|
||||||
instance._text = nameortext
|
instance._text = nameortext
|
||||||
@@ -90,58 +88,41 @@ function ElementNode:close(closestart, closeend)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function select(self, s)
|
local function select(self, s)
|
||||||
if not s or type(s) ~= "string" then return {} end
|
if not s or type(s) ~= "string" then return Set:new() end
|
||||||
local subjects = Set:new({self})
|
local subjects, resultset, childrenonly = Set:new({self})
|
||||||
local resultset
|
local sets = {
|
||||||
local childrenonly
|
[""] = self.deeperelements,
|
||||||
|
["["] = self.deeperattributes,
|
||||||
|
["#"] = self.deeperids,
|
||||||
|
["."] = self.deeperclasses
|
||||||
|
}
|
||||||
for part in string.gmatch(s, "%S+") do
|
for part in string.gmatch(s, "%S+") do
|
||||||
if part == ">" then childrenonly = true goto nextpart end
|
if part == ">" then childrenonly = true goto nextpart end
|
||||||
resultset = Set:new()
|
resultset = Set:new()
|
||||||
for subject in pairs(subjects) do
|
for subject in pairs(subjects) do
|
||||||
local init = subject.deepernodes
|
local star = subject.deepernodes
|
||||||
if childrenonly then init = Set:new(subject.nodes) childrenonly = false end
|
if childrenonly then star = Set:new(subject.nodes) end
|
||||||
resultset = resultset + init
|
childrenonly = false
|
||||||
|
resultset = resultset + star
|
||||||
end
|
end
|
||||||
if part == "*" then goto nextpart end
|
if part == "*" then goto nextpart end
|
||||||
local match, filter
|
local excludes, filter = Set:new()
|
||||||
for t, w in string.gmatch(part, "([:%[#.]?)([^:%(%[#.%]%)]+)%]?%)?") do
|
for t, w in string.gmatch(part, "([:%[#.]?)([^:%(%[#.%]%)]+)%]?%)?") do
|
||||||
-- TODO tidy up
|
|
||||||
if t == ":" then filter = w goto nextw end
|
if t == ":" then filter = w goto nextw end
|
||||||
if t == "" then match = self.deeperelements[w]
|
local match = sets[t][w]
|
||||||
elseif t == "[" then match = self.deeperattributes[w]
|
|
||||||
elseif t == "#" then match = self.deeperids[w]
|
|
||||||
elseif t == "." then match = self.deeperclasses[w]
|
|
||||||
end
|
|
||||||
if filter == "not" then
|
if filter == "not" then
|
||||||
resultset = resultset - match
|
excludes = excludes + match
|
||||||
else
|
else
|
||||||
resultset = resultset * match
|
resultset = resultset * match
|
||||||
end
|
end
|
||||||
filter = nil
|
filter = nil
|
||||||
::nextw::
|
::nextw::
|
||||||
end
|
end
|
||||||
|
resultset = resultset - excludes
|
||||||
subjects = Set:new(resultset)
|
subjects = Set:new(resultset)
|
||||||
::nextpart::
|
::nextpart::
|
||||||
end
|
end
|
||||||
-- construct a container node for the resultset, so that we can :select() on it
|
return resultset
|
||||||
local ret = ElementNode:new("container", self)
|
|
||||||
for node in pairs(resultset) do
|
|
||||||
table.insert(ret.nodes, node)
|
|
||||||
ret.deepernodes = ret.deepernodes + node.deepernodes
|
|
||||||
for listname,list in pairs({
|
|
||||||
deeperelements = node.deeperelements,
|
|
||||||
deeperattributes = node.deeperattributes,
|
|
||||||
deeperids = node.deeperids,
|
|
||||||
deeperclasses = node.deeperclasses
|
|
||||||
}) do
|
|
||||||
local target = ret[listname]
|
|
||||||
for k,set in pairs(list) do
|
|
||||||
-- Set.__add will create an empty Set if not target[k]
|
|
||||||
target[k] = target[k] + set
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return ret
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function ElementNode:select(s) return select(self, s) end
|
function ElementNode:select(s) return select(self, s) end
|
||||||
|
42
test.lua
42
test.lua
@@ -26,11 +26,11 @@ p(root)
|
|||||||
local function select( s )
|
local function select( s )
|
||||||
print ""
|
print ""
|
||||||
print("->", s)
|
print("->", s)
|
||||||
local tags = root:select(s)
|
local sel = root:select(s)
|
||||||
for i,t in ipairs(tags.nodes) do
|
for element in pairs(sel) do
|
||||||
print(t.name)
|
print(element.name)
|
||||||
end
|
end
|
||||||
print(# tags.nodes)
|
print(sel:len())
|
||||||
end
|
end
|
||||||
select("*")
|
select("*")
|
||||||
select("link")
|
select("link")
|
||||||
@@ -53,8 +53,8 @@ select("body > [class]")
|
|||||||
|
|
||||||
print("\nchapters")
|
print("\nchapters")
|
||||||
local sel, chapters = root("ol.chapters > li"), {}
|
local sel, chapters = root("ol.chapters > li"), {}
|
||||||
for _,v in ipairs(sel.nodes) do
|
for e in pairs(sel) do
|
||||||
table.insert(chapters, v:getcontent())
|
table.insert(chapters, e:getcontent())
|
||||||
end
|
end
|
||||||
-- print
|
-- print
|
||||||
for i,v in ipairs(chapters) do
|
for i,v in ipairs(chapters) do
|
||||||
@@ -62,11 +62,11 @@ for i,v in ipairs(chapters) do
|
|||||||
end
|
end
|
||||||
|
|
||||||
print("\ncontacts")
|
print("\ncontacts")
|
||||||
local sel, contacts = root("ul.contacts > li")("span[class]"), {}
|
local sel, contacts = root("ul.contacts span[class]"), {}
|
||||||
for _,v in ipairs(sel.nodes) do
|
for e in pairs(sel) do
|
||||||
local id = v.parent.parent.id -- li > a > span
|
local id = e.parent.parent.id -- li > a > span
|
||||||
contacts[id] = contacts[id] or {}
|
contacts[id] = contacts[id] or {}
|
||||||
contacts[id][v.classes[1]] = v:getcontent()
|
contacts[id][e.classes[1]] = e:getcontent()
|
||||||
end
|
end
|
||||||
-- print
|
-- print
|
||||||
for k,v in pairs(contacts) do
|
for k,v in pairs(contacts) do
|
||||||
@@ -78,7 +78,7 @@ end
|
|||||||
|
|
||||||
print("\nmicrodata")
|
print("\nmicrodata")
|
||||||
local sel, scopes = root("[itemprop]"), {}
|
local sel, scopes = root("[itemprop]"), {}
|
||||||
for _,prop in ipairs(sel.nodes) do
|
for prop in pairs(sel) do
|
||||||
if prop.attributes["itemscope"] then goto nextprop end
|
if prop.attributes["itemscope"] then goto nextprop end
|
||||||
local descendantscopes, scope = {}, prop
|
local descendantscopes, scope = {}, prop
|
||||||
while true do
|
while true do
|
||||||
@@ -115,12 +115,20 @@ for node,table in pairs(scopes) do
|
|||||||
printscope(node, table)
|
printscope(node, table)
|
||||||
end
|
end
|
||||||
|
|
||||||
local sel = root("[itemscope]:not([itemprop])")
|
print("\nnot firstname")
|
||||||
for i,v in ipairs(sel.nodes) do
|
local sel = root(".contacts span:not(.firstname)")
|
||||||
print(v.name)
|
for e in pairs(sel) do
|
||||||
|
print(e.classes[1], e:getcontent())
|
||||||
end
|
end
|
||||||
|
|
||||||
local sel = root("[href]:not(a)")
|
print("\nnot a hrefs")
|
||||||
for i,v in ipairs(sel.nodes) do
|
local sel = root(":not(a)[href]")
|
||||||
print(v.name)
|
for e in pairs(sel) do
|
||||||
|
print(e.name, e.attributes["href"])
|
||||||
|
end
|
||||||
|
|
||||||
|
print("\ntop itemscopes")
|
||||||
|
local sel = root("[itemscope]:not([itemprop])")
|
||||||
|
for e in pairs(sel) do
|
||||||
|
print(e.name, e.attributes["itemtype"])
|
||||||
end
|
end
|
Reference in New Issue
Block a user