Merge pull request #9 from wscherphof/issue-#8

closes #8
This commit is contained in:
Wouter Scherphof 2013-03-20 01:42:15 -07:00
commit 67be34285e
2 changed files with 34 additions and 15 deletions

View File

@ -17,7 +17,9 @@ function ElementNode:new(nameortext, node, descend, openstart, openend)
deepernodes = Set:new(), deepernodes = Set:new(),
deeperelements = {}, deeperattributes = {}, deeperids = {}, deeperclasses = {} deeperelements = {}, deeperattributes = {}, deeperids = {}, deeperclasses = {}
} }
if not node then if nameortext == "container" 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
@ -60,9 +62,7 @@ function ElementNode:addattribute(k, v)
end end
local function insert(list, name, node) local function insert(list, name, node)
if not list[name] then list[name] = list[name] or Set:new()
list[name] = Set:new()
end
list[name]:add(node) list[name]:add(node)
end end
@ -113,7 +113,25 @@ local function select(self, s)
subjects = Set:new(resultset) subjects = Set:new(resultset)
::nextpart:: ::nextpart::
end end
return resultset:tolist() -- construct a container node for the resultset, so that we can :select() on it
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

View File

@ -27,10 +27,10 @@ local function select( s )
print "" print ""
print("->", s) print("->", s)
local tags = root:select(s) local tags = root:select(s)
for i,t in ipairs(tags) do for i,t in ipairs(tags.nodes) do
print(t.name) print(t.name)
end end
print(# tags) print(# tags.nodes)
end end
select("*") select("*")
select("link") select("link")
@ -52,7 +52,7 @@ select("body [class]")
select("body > [class]") select("body > [class]")
local sel, chapters = root("ol.chapters > li"), {} local sel, chapters = root("ol.chapters > li"), {}
for i,v in ipairs(sel) do for _,v in ipairs(sel.nodes) do
table.insert(chapters, v:getcontent()) table.insert(chapters, v:getcontent())
end end
print("\nchapters") print("\nchapters")
@ -60,13 +60,11 @@ for i,v in ipairs(chapters) do
print(i, v) print(i, v)
end end
local sel, contacts = root("ul.contacts > li"), {} local sel, contacts = root("ul.contacts > li")("span[class]"), {}
for i,v in ipairs(sel) do for _,v in ipairs(sel.nodes) do
local c = {} local id = v.parent.parent.id -- li > a > span
for fi,fv in ipairs(v("span[class]")) do contacts[id] = contacts[id] or {}
c[fv.classes[1]] = fv:getcontent() contacts[id][v.classes[1]] = v:getcontent()
end
contacts[v.id] = c
end end
print("\ncontacts") print("\ncontacts")
for k,v in pairs(contacts) do for k,v in pairs(contacts) do
@ -75,3 +73,6 @@ for k,v in pairs(contacts) do
print(fk, fv) print(fk, fv)
end end
end end