Almirah 0.2.2 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,63 +1,72 @@
1
- require_relative "doc_section"
1
+ require_relative 'doc_section'
2
2
 
3
3
  class Document
4
-
5
- attr_accessor :root_section
6
-
7
- @@sections_stack = Array.new
8
-
9
- def initialize(headings_list)
10
- @root_section = nil
11
-
12
- build_sections_tree(headings_list)
13
- end
14
-
15
- def build_sections_tree(headings_list)
16
-
17
- headings_list.each do |h|
18
-
19
- if @root_section == nil
20
-
21
- s = DocSection.new(h)
22
- s.parent_section = nil
23
- @root_section = s
24
- @@sections_stack.push s
25
-
26
- elsif @@sections_stack[-1].heading.level == h.level
27
-
28
- s = DocSection.new(h)
29
- @@sections_stack[-2].sections.append(s)
30
- @@sections_stack[-1] = s
31
-
32
- elsif h.level > @@sections_stack[-1].heading.level
33
-
34
- s = DocSection.new(h)
35
- @@sections_stack[-1].sections.append(s)
36
- @@sections_stack.push s
37
-
38
- else
39
- while h.level < @@sections_stack[-1].heading.level
40
- @@sections_stack.pop
41
- end
42
-
43
- s = DocSection.new(h)
44
- @@sections_stack[-2].sections.append(s)
45
- @@sections_stack[-1] = s
46
- end
4
+ attr_accessor :root_section
5
+
6
+ def initialize(headings_list)
7
+ @root_section = nil
8
+
9
+ build_sections_tree(headings_list)
10
+ end
11
+
12
+ def build_sections_tree(headings_list)
13
+ sections_stack = []
14
+ headings_list.each do |h|
15
+ if @root_section.nil?
16
+
17
+ s = DocSection.new(h)
18
+ s.parent_section = nil
19
+ @root_section = s
20
+ sections_stack.push s
21
+ # one more artificial section copy if root is not a Document Title (level 0)
22
+ if h.level.positive?
23
+ a = DocSection.new(h)
24
+ a.parent_section = @root_section
25
+ @root_section.sections.append(a)
26
+ sections_stack.push a
47
27
  end
48
- end
49
28
 
50
- def section_tree_to_html
51
- s = ''
52
- s += "<a href=\"\#" + @root_section.heading.anchor_id.to_s + "\">" + @root_section.heading.get_section_info + "</a>\n"
53
- if @root_section.sections.length >0
54
- s += "\t<ul class=\"fa-ul\" style=\"margin-top: 2px;\">\n"
55
- @root_section.sections.each do |sub_section|
56
- s += sub_section.to_html()
57
- end
58
- s += "\t</ul>\n"
29
+ elsif sections_stack[-1].heading.level == h.level
30
+
31
+ s = DocSection.new(h)
32
+ s.parent_section = sections_stack[-1].parent_section
33
+ sections_stack[-1].parent_section.sections.append(s)
34
+ sections_stack[-1] = s
35
+
36
+ elsif h.level > sections_stack[-1].heading.level
37
+
38
+ s = DocSection.new(h)
39
+ s.parent_section = sections_stack[-1]
40
+ sections_stack[-1].sections.append(s)
41
+ sections_stack.push s
42
+
43
+ else
44
+ sections_stack.pop while h.level < sections_stack[-1].heading.level
45
+ sections_stack.push @root_section if sections_stack.empty?
46
+ s = DocSection.new(h)
47
+ if h.level == sections_stack[-1].heading.level
48
+ s.parent_section = sections_stack[-1].parent_section
49
+ sections_stack[-1].parent_section.sections.append(s)
50
+ else
51
+ s.parent_section = sections_stack[-1]
52
+ sections_stack[-1].sections.append(s)
59
53
  end
60
-
61
- return s
54
+ sections_stack[-1] = s
55
+ end
62
56
  end
63
- end
57
+ end
58
+
59
+ def section_tree_to_html
60
+ s = ''
61
+ s += "<a href=\"##{@root_section.heading.anchor_id}\">#{@root_section.heading.get_section_info}</a>\n"
62
+ unless @root_section.sections.empty?
63
+ s += "\t<ul class=\"fa-ul\" style=\"margin-top: 2px;\">\n"
64
+ @root_section.sections.each do |sub_section|
65
+ s += sub_section.to_html
66
+ end
67
+ s += "\t</ul>\n"
68
+ end
69
+
70
+ s
71
+ end
72
+ end