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
|
# File 'lib/bookit/parser/html.rb', line 15
def walk(element, tree)
return tree if element.nil? || (element.content.strip.empty? if element.name != "img")
tree << case element.name
when "p"
Bookit::Content::Paragraph.new(walk_children(element, []))
when "text"
Bookit::Content::Text.new(element.content.strip)
when "h1", "h2", "h3", "h4"
Bookit::Content::Header.new(element.content.strip)
when "a"
Bookit::Content::Link.new(element.attributes["href"].value, walk_children(element, []))
when "img"
attrs = {}
['width', 'height'].each {|a| attrs[a.to_sym] = element.attributes[a] ? element.attributes[a].value.to_i : nil}
Bookit::Content::Image.new(element.attributes["src"].value, attrs)
when "ul", "ol"
Bookit::Content::List.new(walk_children(element, []))
else
walk_children(element, tree)
nil
end
return tree
end
|