Class: Cheepub::HeadingItem

Inherits:
Object
  • Object
show all
Defined in:
lib/cheepub/heading_item.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#childrenObject (readonly)

Returns the value of attribute children.



4
5
6
# File 'lib/cheepub/heading_item.rb', line 4

def children
  @children
end

#contentObject (readonly)

Returns the value of attribute content.



4
5
6
# File 'lib/cheepub/heading_item.rb', line 4

def content
  @content
end

#filenameObject (readonly)

Returns the value of attribute filename.



4
5
6
# File 'lib/cheepub/heading_item.rb', line 4

def filename
  @filename
end

#idObject (readonly)

Returns the value of attribute id.



4
5
6
# File 'lib/cheepub/heading_item.rb', line 4

def id
  @id
end

#levelObject (readonly)

Returns the value of attribute level.



4
5
6
# File 'lib/cheepub/heading_item.rb', line 4

def level
  @level
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/cheepub/heading_item.rb', line 4

def name
  @name
end

#parentObject

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

#dumpObject



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_olObject



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>&#160;</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