Class: Document

Inherits:
Object
  • Object
show all
Defined in:
lib/almirah/dom/document.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(headings_list) ⇒ Document

Returns a new instance of Document.



6
7
8
9
10
# File 'lib/almirah/dom/document.rb', line 6

def initialize(headings_list)
  @root_section = nil

  build_sections_tree(headings_list)
end

Instance Attribute Details

#root_sectionObject

Returns the value of attribute root_section.



4
5
6
# File 'lib/almirah/dom/document.rb', line 4

def root_section
  @root_section
end

Instance Method Details

#build_sections_tree(headings_list) ⇒ Object



12
13
14
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/almirah/dom/document.rb', line 12

def build_sections_tree(headings_list)
  sections_stack = []
  headings_list.each do |h|
    if @root_section.nil?

      s = DocSection.new(h)
      s.parent_section = nil
      @root_section = s
      sections_stack.push s
      # one more artificial section copy if root is not a Document Title (level 0)
      if h.level.positive?
        a = DocSection.new(h)
        a.parent_section = @root_section
        @root_section.sections.append(a)
        sections_stack.push a
      end

    elsif sections_stack[-1].heading.level == h.level

      s = DocSection.new(h)
      s.parent_section = sections_stack[-1].parent_section
      sections_stack[-1].parent_section.sections.append(s)
      sections_stack[-1] = s
      # for search engine
      s.heading.parent_heading = s.parent_section.heading

    elsif h.level > sections_stack[-1].heading.level

      s = DocSection.new(h)
      s.parent_section = sections_stack[-1]
      sections_stack[-1].sections.append(s)
      sections_stack.push s
      # for search engine
      s.heading.parent_heading = s.parent_section.heading

    else
      sections_stack.pop while h.level < sections_stack[-1].heading.level
      sections_stack.push @root_section if sections_stack.empty?
      s = DocSection.new(h)
      if h.level == sections_stack[-1].heading.level
        s.parent_section = sections_stack[-1].parent_section
        sections_stack[-1].parent_section.sections.append(s)
      else
        s.parent_section = sections_stack[-1]
        sections_stack[-1].sections.append(s)
      end
      sections_stack[-1] = s
      # for search engine
      s.heading.parent_heading = s.parent_section.heading
    end
  end
end

#section_tree_to_htmlObject



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/almirah/dom/document.rb', line 65

def section_tree_to_html
  s = ''
  s += "<a href=\"##{@root_section.heading.anchor_id}\">#{@root_section.heading.}</a>\n"
  unless @root_section.sections.empty?
    s += "\t<ul class=\"fa-ul\" style=\"margin-top: 2px;\">\n"
    @root_section.sections.each do |sub_section|
      s += sub_section.to_html
    end
    s += "\t</ul>\n"
  end

  s
end