Class: Cheepub::HeadingParser
- Inherits:
-
Object
- Object
- Cheepub::HeadingParser
- Defined in:
- lib/cheepub/heading_parser.rb,
lib/cheepub/heading_parser/handler.rb
Defined Under Namespace
Classes: Handler
Instance Method Summary collapse
- #concat_node(prev, node) ⇒ Object
-
#initialize ⇒ HeadingParser
constructor
A new instance of HeadingParser.
- #layering_heading(list) ⇒ Object
- #parse_files(files) ⇒ Object
- #parse_html_heading(filename, content) ⇒ Object
Constructor Details
#initialize ⇒ HeadingParser
Returns a new instance of HeadingParser.
7 8 |
# File 'lib/cheepub/heading_parser.rb', line 7 def initialize end |
Instance Method Details
#concat_node(prev, node) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/cheepub/heading_parser.rb', line 37 def concat_node(prev, node) if node.level == prev.level + 1 prev.add_child(node) elsif node.level == prev.level prev.add_sibling(node) elsif node.level > prev.level prev.add_descendant(node) else ## node.level < prev.level prev.add_ancestor(node) end end |
#layering_heading(list) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/cheepub/heading_parser.rb', line 22 def layering_heading(list) root = HeadingItem.new(level: 0, children: []) prev = root list.each do |item| next if !item[:id] || item[:content].empty? node = HeadingItem.new(id: item[:id], level: item[:level], name: item[:name], content: item[:content], filename: item[:filename], children: []) concat_node(prev, node) prev = node end root end |
#parse_files(files) ⇒ Object
10 11 12 13 14 |
# File 'lib/cheepub/heading_parser.rb', line 10 def parse_files(files) heading_list = files.map{ |filename, content| parse_html_heading(filename, content) }.flatten root = layering_heading(heading_list) root end |
#parse_html_heading(filename, content) ⇒ Object
16 17 18 19 20 |
# File 'lib/cheepub/heading_parser.rb', line 16 def parse_html_heading(filename, content) handler = Cheepub::HeadingParser::Handler.new(filename) Oga::HTML::SaxParser.new(handler, content).parse handler.result end |