mirror of
https://github.com/msva/lua-htmlparser.git
synced 2024-11-04 23:34:20 +00:00
closes #7
Extracting microdata is fairly tedious compared to microformats, due to its generality. Should probably be included as a standard function of the parser, or even better, as a separate module, which then can concern itself with being fully standards compliant.
This commit is contained in:
parent
78d99a61f6
commit
f9b04866b4
35
test.html
35
test.html
@ -39,23 +39,24 @@
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
<section itemscope itemtype="http://schema.org/Person">
|
||||
Hello, my name is
|
||||
<span itemprop="name">John Doe</span>,
|
||||
I am a
|
||||
<span itemprop="jobTitle">graduate research assistant</span>
|
||||
at the
|
||||
<span itemprop="affiliation">University of Dreams</span>.
|
||||
My friends call me
|
||||
<span itemprop="additionalName">Johnny</span>.
|
||||
You can visit my homepage at
|
||||
<a href="http://www.JohnnyD.com" itemprop="url">www.JohnnyD.com</a>.
|
||||
<section itemprop="address" itemscope itemtype="http://schema.org/PostalAddress">
|
||||
I live at
|
||||
<span itemprop="streetAddress">1234 Peach Drive</span>,
|
||||
<span itemprop="addressLocality">Warner Robins</span>,
|
||||
<span itemprop="addressRegion">Georgia</span>.
|
||||
<h1>About me</h1>
|
||||
<section itemscope itemtype="http://schema.org/Person">
|
||||
Hello, my name is
|
||||
<span itemprop="name">John Doe</span>,
|
||||
I am a
|
||||
<span itemprop="jobTitle">graduate research assistant</span>
|
||||
at the
|
||||
<span itemprop="affiliation">University of Dreams</span>.
|
||||
My friends call me
|
||||
<span itemprop="additionalName">Johnny</span>.
|
||||
You can visit my homepage at
|
||||
<a href="http://www.JohnnyD.com" itemprop="url">www.JohnnyD.com</a>.
|
||||
<section itemprop="address" itemscope itemtype="http://schema.org/PostalAddress">
|
||||
I live at
|
||||
<span itemprop="streetAddress">1234 Peach Drive</span>,
|
||||
<span itemprop="addressLocality">Warner Robins</span>,
|
||||
<span itemprop="addressRegion">Georgia</span>.
|
||||
</section>
|
||||
</section>
|
||||
</section>
|
||||
</body>
|
||||
</html>
|
56
test.lua
56
test.lua
@ -51,22 +51,24 @@ select("ul > *")
|
||||
select("body [class]")
|
||||
select("body > [class]")
|
||||
|
||||
print("\nchapters")
|
||||
local sel, chapters = root("ol.chapters > li"), {}
|
||||
for _,v in ipairs(sel.nodes) do
|
||||
table.insert(chapters, v:getcontent())
|
||||
end
|
||||
print("\nchapters")
|
||||
-- print
|
||||
for i,v in ipairs(chapters) do
|
||||
print(i, v)
|
||||
end
|
||||
|
||||
print("\ncontacts")
|
||||
local sel, contacts = root("ul.contacts > li")("span[class]"), {}
|
||||
for _,v in ipairs(sel.nodes) do
|
||||
local id = v.parent.parent.id -- li > a > span
|
||||
contacts[id] = contacts[id] or {}
|
||||
contacts[id][v.classes[1]] = v:getcontent()
|
||||
end
|
||||
print("\ncontacts")
|
||||
-- print
|
||||
for k,v in pairs(contacts) do
|
||||
print(k)
|
||||
for fk,fv in pairs(v) do
|
||||
@ -75,20 +77,40 @@ for k,v in pairs(contacts) do
|
||||
end
|
||||
|
||||
print("\nmicrodata")
|
||||
local sel, scopes = root("[itemscope]"), {}
|
||||
for i,v in ipairs(sel.nodes) do
|
||||
local type = v.attributes["itemtype"]
|
||||
if not v.attributes["itemprop"] then
|
||||
scopes[type] = scopes[type] or {}
|
||||
local item = {}
|
||||
local sel = sel("[itemprop]")
|
||||
for i,v in ipairs(sel.nodes) do
|
||||
-- TODO
|
||||
print("prop", v.attributes["itemprop"])
|
||||
end
|
||||
table.insert(scopes[type], item)
|
||||
local sel, scopes = root("[itemprop]"), {}
|
||||
for _,prop in ipairs(sel.nodes) do
|
||||
if prop.attributes["itemscope"] then goto nextprop end
|
||||
local descendantscopes, scope = {}, prop
|
||||
while true do
|
||||
repeat
|
||||
scope = scope.parent
|
||||
until scope.attributes["itemscope"]
|
||||
if not scope.attributes["itemprop"] then break end
|
||||
table.insert(descendantscopes, 1, scope)
|
||||
end
|
||||
scopes[scope] = scopes[scope] or {}
|
||||
local entry = scopes[scope]
|
||||
for _,v in ipairs(descendantscopes) do
|
||||
entry[v] = entry[v] or {}
|
||||
entry = entry[v]
|
||||
end
|
||||
local k, v = prop.attributes["itemprop"], prop:getcontent()
|
||||
entry[k] = v
|
||||
::nextprop::
|
||||
end
|
||||
-- print
|
||||
local function printscope(node, table, level)
|
||||
level = level or 1
|
||||
local scopeprop = node.attributes["itemprop"] or ""
|
||||
print(string.rep(" ", level - 1) .. node.attributes["itemtype"], scopeprop)
|
||||
for prop,v in pairs(table) do
|
||||
if type(prop) == "table" then
|
||||
printscope(prop, v, level + 1)
|
||||
else
|
||||
print(string.rep(" ", level) .. prop .. "=[" .. v .. "]")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
for node,table in pairs(scopes) do
|
||||
printscope(node, table)
|
||||
end
|
Loading…
Reference in New Issue
Block a user