Class: Cheepub::HeadingItem
- Inherits:
-
Object
- Object
- Cheepub::HeadingItem
- Defined in:
- lib/cheepub/heading_item.rb
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#level ⇒ Object
readonly
Returns the value of attribute level.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parent ⇒ Object
Returns the value of attribute parent.
Instance Method Summary collapse
- #add_ancestor(node) ⇒ Object
- #add_child(node) ⇒ Object
- #add_descendant(node) ⇒ Object
- #add_sibling(node) ⇒ Object
- #dump ⇒ Object
-
#initialize(id: nil, level:, name: nil, filename: nil, content: nil, children: nil, parent: nil) ⇒ HeadingItem
constructor
A new instance of HeadingItem.
- #to_html_ol ⇒ Object
Constructor Details
#initialize(id: nil, level:, name: nil, filename: nil, content: nil, children: nil, parent: nil) ⇒ HeadingItem
Returns a new instance of HeadingItem.
7 8 9 10 11 12 13 14 15 |
# File 'lib/cheepub/heading_item.rb', line 7 def initialize(id:nil, level:, name:nil, filename:nil, content:nil, children:nil, parent:nil) @id = id @level = level @name = name @filename = filename @content = content @children = children @parent = parent end |
Instance Attribute Details
#children ⇒ Object (readonly)
Returns the value of attribute children.
4 5 6 |
# File 'lib/cheepub/heading_item.rb', line 4 def children @children end |
#content ⇒ Object (readonly)
Returns the value of attribute content.
4 5 6 |
# File 'lib/cheepub/heading_item.rb', line 4 def content @content end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
4 5 6 |
# File 'lib/cheepub/heading_item.rb', line 4 def filename @filename end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
4 5 6 |
# File 'lib/cheepub/heading_item.rb', line 4 def id @id end |
#level ⇒ Object (readonly)
Returns the value of attribute level.
4 5 6 |
# File 'lib/cheepub/heading_item.rb', line 4 def level @level end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
4 5 6 |
# File 'lib/cheepub/heading_item.rb', line 4 def name @name end |
#parent ⇒ Object
Returns the value of attribute parent.
5 6 7 |
# File 'lib/cheepub/heading_item.rb', line 5 def parent @parent end |
Instance Method Details
#add_ancestor(node) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/cheepub/heading_item.rb', line 37 def add_ancestor(node) current = self while current.level != node.level current = current.parent end current.add_sibling(node) end |
#add_child(node) ⇒ Object
17 18 19 20 21 |
# File 'lib/cheepub/heading_item.rb', line 17 def add_child(node) children << node node.parent = self node end |
#add_descendant(node) ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/cheepub/heading_item.rb', line 27 def add_descendant(node) current = self while current.level + 1 != node.level item = HeadingItem.new(level: current.level+1, children: []) current.add_child(item) current = item end current.add_child(node) end |
#add_sibling(node) ⇒ Object
23 24 25 |
# File 'lib/cheepub/heading_item.rb', line 23 def add_sibling(node) parent.add_child(node) end |
#dump ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/cheepub/heading_item.rb', line 45 def dump print "*" * level print "{id:#{id}} content: #{content}\n" children.each do |child| child.dump end end |
#to_html_ol ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/cheepub/heading_item.rb', line 53 def to_html_ol indent = " " * (2 * level) buf = "" if level > 0 buf << "#{indent}<li>\n" end if id && content.length > 0 buf << " #{indent}<a href=\"#{filename}\##{id}\">#{content}</a>\n" elsif level > 0 buf << " #{indent}<span> </span>\n" end if !children.empty? buf << " #{indent}<ol>\n" + children.map(&:to_html_ol).join("") + " #{indent}</ol>\n" end if level > 0 buf << "#{indent}</li>\n" end buf end |