Almirah 0.2.2 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,9 +7,9 @@ class DocFooter < DocItem
7
7
 
8
8
  def to_html
9
9
  s = ''
10
- if @@htmlTableRenderInProgress
10
+ if @@html_table_render_in_progress
11
11
  s += "</table>\n"
12
- @@htmlTableRenderInProgress = false
12
+ @@html_table_render_in_progress = false
13
13
  end
14
14
  return s
15
15
  end
@@ -7,7 +7,7 @@ class DocItem < TextLine
7
7
  @parent_doc = nil
8
8
  @parent_heading = nil
9
9
 
10
- @@htmlTableRenderInProgress = false
10
+ @@html_table_render_in_progress = false
11
11
 
12
12
  def get_url
13
13
  ''
@@ -1,98 +1,93 @@
1
- require_relative "paragraph"
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'paragraph'
2
4
 
3
5
  class Heading < Paragraph
6
+ attr_accessor :level, :anchor_id, :section_number
4
7
 
5
- attr_accessor :level
6
- attr_accessor :anchor_id
7
- attr_accessor :section_number
8
-
9
- @@global_section_number = ""
10
-
11
- def initialize(doc, text, level)
12
- @parent_doc = doc
13
- @text = text
14
- @level = level
15
-
16
- if level != 0 # skip Doc Title
17
- if @@global_section_number == ""
18
- @@global_section_number = "1"
19
- for n in 1..(level-1) do
20
- @@global_section_number += ".1"
21
- end
22
- else
23
- previous_level = @@global_section_number.split(".").length
24
-
25
- if previous_level == level
26
-
27
- a = @@global_section_number.split(".")
28
- a[-1] = (a[-1].to_i() +1).to_s
29
- @@global_section_number = a.join(".")
30
-
31
- elsif level > previous_level
32
-
33
- a = @@global_section_number.split(".")
34
- a.push("1")
35
- @@global_section_number = a.join(".")
36
-
37
- else # level < previous_level
38
-
39
- a = @@global_section_number.split(".")
40
- delta = previous_level - level
41
- a.pop(delta)
42
- @@global_section_number = a.join(".")
43
- # increment
44
- a = @@global_section_number.split(".")
45
- a[-1] = (a[-1].to_i() +1).to_s
46
- @@global_section_number = a.join(".")
47
- end
48
- end
49
- end
50
- @section_number = @@global_section_number
51
- @anchor_id = get_anchor_text()
52
- end
8
+ @@global_section_number = ''
9
+
10
+ def initialize(doc, text, level)
11
+ super(doc, text)
12
+ @level = level
53
13
 
54
- def get_section_info
55
- if level == 0 # Doc Title
56
- s = @text
57
- else
58
- s = @section_number + " " + @text
14
+ if level != 0 # skip Doc Title
15
+ if @@global_section_number == ''
16
+ @@global_section_number = '1'
17
+ (1..(level - 1)).each do |_n|
18
+ @@global_section_number += '.1'
59
19
  end
60
- end
20
+ else
21
+ previous_level = @@global_section_number.split('.').length
61
22
 
62
- def get_anchor_text()
63
- s = @section_number + "-" + getTextWithoutSpaces()
64
- end
23
+ if previous_level == level
65
24
 
66
- def to_html
67
- s = ''
68
- if @@htmlTableRenderInProgress
69
- s += "</table>\n"
70
- @@htmlTableRenderInProgress = false
71
- end
72
- heading_level = level.to_s
73
- heading_text = get_section_info()
74
- if level == 0
75
- heading_level = 1.to_s # Render Doc Title as a regular h1
76
- heading_text = @text # Doc Title does not have a section number
77
- end
78
- s += "<a name=\"#{@anchor_id}\"></a>\n"
79
- s += "<h#{heading_level}> #{heading_text} <a href=\"\##{@anchor_id}\" class=\"heading_anchor\">"
80
- s += "&para;</a></h#{heading_level}>"
81
- return s
82
- end
25
+ a = @@global_section_number.split('.')
26
+ a[-1] = (a[-1].to_i + 1).to_s
27
+ @@global_section_number = a.join('.')
83
28
 
84
- def get_html_link
85
- if (@parent_doc.instance_of? Specification)
86
- heading_text = get_section_info()
87
- s = "<a href= class=\"external\">#{heading_text}</a>"
29
+ elsif level > previous_level
30
+
31
+ a = @@global_section_number.split('.')
32
+ a.push('1')
33
+ @@global_section_number = a.join('.')
34
+
35
+ else # level < previous_level
36
+
37
+ a = @@global_section_number.split('.')
38
+ delta = previous_level - level
39
+ a.pop(delta)
40
+ @@global_section_number = a.join('.')
41
+ # increment
42
+ a = @@global_section_number.split('.')
43
+ a[-1] = (a[-1].to_i + 1).to_s
44
+ @@global_section_number = a.join('.')
88
45
  end
46
+ end
89
47
  end
90
-
91
- def get_url
92
- "./specifications/#{parent_doc.id}/#{parent_doc.id}.html\##{@anchor_id}"
48
+ @section_number = @@global_section_number
49
+ @anchor_id = get_anchor_text
50
+ end
51
+
52
+ def get_section_info
53
+ if level.zero? # Doc Title
54
+ @text
55
+ else
56
+ "#{@section_number} #{@text}"
93
57
  end
58
+ end
94
59
 
95
- def self.reset_global_section_number
96
- @@global_section_number = ""
60
+ def get_anchor_text
61
+ "#{@section_number}-#{getTextWithoutSpaces}"
62
+ end
63
+
64
+ def get_markdown_anchor_text
65
+ getTextWithoutSpaces
66
+ end
67
+
68
+ def to_html
69
+ s = ''
70
+ if @@html_table_render_in_progress
71
+ s += "</table>\n"
72
+ @@html_table_render_in_progress = false
73
+ end
74
+ heading_level = level.to_s
75
+ heading_text = get_section_info
76
+ if level.zero?
77
+ heading_level = 1.to_s # Render Doc Title as a regular h1
78
+ heading_text = @text # Doc Title does not have a section number
97
79
  end
98
- end
80
+ s += "<a name=\"#{@anchor_id}\"></a>\n"
81
+ s += "<h#{heading_level}> #{heading_text} <a href=\"\##{@anchor_id}\" class=\"heading_anchor\">"
82
+ s += "&para;</a></h#{heading_level}>"
83
+ s
84
+ end
85
+
86
+ def get_url
87
+ "./specifications/#{parent_doc.id}/#{parent_doc.id}.html\##{@anchor_id}"
88
+ end
89
+
90
+ def self.reset_global_section_number
91
+ @@global_section_number = ''
92
+ end
93
+ end
@@ -16,9 +16,9 @@ class Image < DocItem
16
16
 
17
17
  def to_html
18
18
  s = ''
19
- if @@htmlTableRenderInProgress
19
+ if @@html_table_render_in_progress
20
20
  s += "</table>\n"
21
- @@htmlTableRenderInProgress = false
21
+ @@html_table_render_in_progress = false
22
22
  end
23
23
 
24
24
  s += "<p style=\"margin-top: 15px;\"><img src=\"#{@path}\" alt=\"#{@text}\" "
@@ -1,167 +1,158 @@
1
- require_relative "doc_item"
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'doc_item'
2
4
 
3
5
  class MarkdownList < DocItem
6
+ attr_accessor :rows, :text, :is_ordered, :indent_position, :current_nesting_level
4
7
 
5
- attr_accessor :rows
6
- attr_accessor :text
7
- attr_accessor :is_ordered
8
- attr_accessor :indent_position
9
- attr_accessor :current_nesting_level
8
+ @@lists_stack = []
10
9
 
11
- @@lists_stack = Array.new
10
+ def initialize(doc, is_ordered)
11
+ super()
12
+ @parent_doc = doc
13
+ @parent_heading = doc.headings[-1]
12
14
 
13
- def initialize(doc, is_ordered)
14
- @parent_doc = doc
15
- @parent_heading = doc.headings[-1]
15
+ @rows = []
16
+ @is_ordered = is_ordered
17
+ @current_nesting_level = 0
18
+ @indent_position = 0
19
+ @text = ''
16
20
 
17
- @rows = Array.new
18
- @is_ordered = is_ordered
19
- @current_nesting_level = 0
20
- @indent_position = 0
21
- @text = ''
21
+ @@lists_stack.push(self)
22
+ end
22
23
 
23
- @@lists_stack.push(self)
24
- end
24
+ def add_row(raw_text)
25
+ pos = calculate_text_position(raw_text)
26
+ row = raw_text[pos..-1]
25
27
 
26
- def addRow(raw_text)
27
- pos = calculate_text_position(raw_text)
28
- row = raw_text[pos..-1]
29
-
30
- pos = calculate_indent_position(raw_text)
31
-
32
- if pos > @@lists_stack[-1].indent_position
33
-
34
- prev_lists_stack_item = @@lists_stack[-1]
35
- # the following line pushes new list to the lists_stack in the constructor!
36
- nested_list = MarkdownList.new( @parent_doc, MarkdownList.ordered_list_item?(raw_text) )
37
- nested_list.current_nesting_level = @current_nesting_level + 1
38
- nested_list.indent_position = pos
39
-
40
- prev_row = prev_lists_stack_item.rows[-1]
41
- if prev_row.is_a?(MarkdownList)
42
- #cannot be there
43
- else
44
- nested_list.text = prev_row
45
- #puts "Length: " + prev_lists_stack_item.rows.length.to_s
46
- prev_lists_stack_item.rows[-1] = nested_list
47
- end
48
-
49
- nested_list.addRow(raw_text)
50
-
51
- elsif pos < @@lists_stack[-1].indent_position
52
-
53
- while pos < @@lists_stack[-1].indent_position
54
- @@lists_stack.pop
55
- end
56
- @@lists_stack[-1].rows.append(row)
57
-
58
- else
59
- @@lists_stack[-1].rows.append(row)
28
+ pos = calculate_indent_position(raw_text)
60
29
 
61
- end
62
- end
30
+ if pos > @@lists_stack[-1].indent_position
63
31
 
64
- def calculate_indent_position(s)
65
- s.downcase
66
- pos = 0
67
- s.each_char do |c|
68
- if c != ' ' && c != '\t'
69
- break
70
- end
71
- pos += 1
72
- end
73
- return pos
74
- end
75
- def calculate_text_position(s)
76
- s.downcase
77
- pos = 0
78
- state = 'looking_for_list_item_marker'
79
- s.each_char do |c|
80
- if state == 'looking_for_list_item_marker'
81
- if c == '*'
82
- state = 'looking_for_space'
83
- elsif numeric?(c)
84
- state = 'looking_for_dot'
85
- end
86
- elsif state == 'looking_for_dot'
87
- if c == '.'
88
- state = 'looking_for_space'
89
- end
90
- elsif state == 'looking_for_space'
91
- if c == ' ' || c == '\t'
92
- state = 'looking_for_non_space'
93
- end
94
- elsif state == 'looking_for_non_space'
95
- if c != ' ' || c != '\t'
96
- state = 'list_item_text_pos_found'
97
- break
98
- end
99
- end
100
- pos += 1
101
- end
102
- return pos
103
- end
32
+ prev_lists_stack_item = @@lists_stack[-1]
33
+ # the following line pushes new list to the lists_stack in the constructor!
34
+ nested_list = MarkdownList.new(@parent_doc, MarkdownList.ordered_list_item?(raw_text))
35
+ nested_list.current_nesting_level = @current_nesting_level + 1
36
+ nested_list.indent_position = pos
104
37
 
105
- def letter?(c)
106
- c.match?(/[[:alpha:]]/)
107
- end
108
-
109
- def numeric?(c)
110
- c.match?(/[[:digit:]]/)
111
- end
38
+ prev_row = prev_lists_stack_item.rows[-1]
39
+ if prev_row.is_a?(MarkdownList)
40
+ # cannot be there
41
+ else
42
+ nested_list.text = prev_row
43
+ # puts "Length: " + prev_lists_stack_item.rows.length.to_s
44
+ prev_lists_stack_item.rows[-1] = nested_list
45
+ end
112
46
 
113
- def non_blank?(c)
114
- c.match?(/[[:graph:]]/)
115
- end
47
+ nested_list.add_row(raw_text)
116
48
 
117
- def self.unordered_list_item?(raw_text)
49
+ elsif pos < @@lists_stack[-1].indent_position
118
50
 
119
- if res = /(\*\s?)(.*)/.match(raw_text)
120
- return true
121
- end
122
- return false
123
- end
51
+ @@lists_stack.pop while pos < @@lists_stack[-1].indent_position
52
+ @@lists_stack[-1].rows.append(row)
124
53
 
125
- def self.ordered_list_item?(raw_text)
54
+ else
55
+ @@lists_stack[-1].rows.append(row)
126
56
 
127
- if res = /\d[.]\s(.*)/.match(raw_text)
128
- return true
129
- end
130
- return false
131
57
  end
58
+ end
132
59
 
133
- def to_html
134
- s = ''
135
- if @@htmlTableRenderInProgress
136
- s += "</table>\n"
137
- @@htmlTableRenderInProgress = false
138
- end
60
+ def calculate_indent_position(s)
61
+ s.downcase
62
+ pos = 0
63
+ s.each_char do |c|
64
+ break if c != ' ' && c != '\t'
139
65
 
140
- if @is_ordered
141
- s += "<ol>\n"
142
- else
143
- s += "<ul>\n"
66
+ pos += 1
67
+ end
68
+ pos
69
+ end
70
+
71
+ def calculate_text_position(s)
72
+ s.downcase
73
+ pos = 0
74
+ state = 'looking_for_list_item_marker'
75
+ s.each_char do |c|
76
+ case state
77
+ when 'looking_for_list_item_marker'
78
+ if c == '*'
79
+ state = 'looking_for_space'
80
+ elsif numeric?(c)
81
+ state = 'looking_for_dot'
144
82
  end
145
-
146
- @rows.each do |r|
147
- if r.is_a?(MarkdownList)
148
- f_text = format_string(r.text)
149
- s += "\t<li>#{f_text}\n"
150
- s += r.to_html()
151
- s += "</li>\n"
152
- else
153
- f_text = format_string(r)
154
- #puts f_text
155
- s += "\t<li>#{f_text}</li>\n"
156
- end
83
+ when 'looking_for_dot'
84
+ state = 'looking_for_space' if c == '.'
85
+ when 'looking_for_space'
86
+ state = 'looking_for_non_space' if [' ', '\t'].include?(c)
87
+ when 'looking_for_non_space'
88
+ if c != ' ' || c != '\t'
89
+ state = 'list_item_text_pos_found'
90
+ break
157
91
  end
92
+ end
93
+ pos += 1
94
+ end
95
+ pos
96
+ end
97
+
98
+ def letter?(c)
99
+ c.match?(/[[:alpha:]]/)
100
+ end
101
+
102
+ def numeric?(c)
103
+ c.match?(/[[:digit:]]/)
104
+ end
105
+
106
+ def non_blank?(c)
107
+ c.match?(/[[:graph:]]/)
108
+ end
109
+
110
+ def self.unordered_list_item?(raw_text)
111
+ res = /(\*\s?)(.*)/.match(raw_text)
112
+ return true if res
158
113
 
159
- if @is_ordered
160
- s += "</ol>\n"
161
- else
162
- s += "</ul>\n"
163
- end
114
+ false
115
+ end
164
116
 
165
- return s
117
+ def self.ordered_list_item?(raw_text)
118
+ res = /\d[.]\s(.*)/.match(raw_text)
119
+ return true if res
120
+
121
+ false
122
+ end
123
+
124
+ def to_html
125
+ s = ''
126
+ if @@html_table_render_in_progress
127
+ s += "</table>\n"
128
+ @@html_table_render_in_progress = false
129
+ end
130
+
131
+ s += if @is_ordered
132
+ "<ol>\n"
133
+ else
134
+ "<ul>\n"
135
+ end
136
+
137
+ @rows.each do |r|
138
+ if r.is_a?(MarkdownList)
139
+ f_text = format_string(r.text)
140
+ s += "\t<li>#{f_text}\n"
141
+ s += r.to_html
142
+ s += "</li>\n"
143
+ else
144
+ f_text = format_string(r)
145
+ # puts f_text
146
+ s += "\t<li>#{f_text}</li>\n"
147
+ end
166
148
  end
167
- end
149
+
150
+ s += if @is_ordered
151
+ "</ol>\n"
152
+ else
153
+ "</ul>\n"
154
+ end
155
+
156
+ s
157
+ end
158
+ end
@@ -1,59 +1,63 @@
1
- require_relative "doc_item"
1
+ # frozen_string_literal: true
2
2
 
3
- class MarkdownTable < DocItem
4
-
5
- attr_accessor :column_names
6
- attr_accessor :rows
3
+ require_relative 'doc_item'
7
4
 
8
- def initialize(heading_row)
9
- @column_names = heading_row.split('|')
10
- @rows = Array.new
5
+ class MarkdownTable < DocItem
6
+ attr_accessor :column_names, :rows, :heading_row, :is_separator_detected
7
+
8
+ def initialize(doc, heading_row)
9
+ super()
10
+ @parent_doc = doc
11
+ @parent_heading = doc.headings[-1]
12
+ @heading_row = heading_row
13
+
14
+ res = /^[|](.*[|])/.match(heading_row)
15
+ @column_names = if res
16
+ res[1].split('|')
17
+ else
18
+ ['# ERROR# ']
19
+ end
20
+ @rows = []
21
+ @is_separator_detected = false
22
+ end
23
+
24
+ def add_row(row)
25
+ columns = row.split('|')
26
+ @rows.append(columns.map!(&:strip))
27
+ true
28
+ end
29
+
30
+ def to_html
31
+ s = ''
32
+ if @@html_table_render_in_progress
33
+ s += "</table>\n"
34
+ @@html_table_render_in_progress = false
11
35
  end
12
36
 
13
- def addRow(row)
14
- #check if row contains a link
15
- if tmp = /(.*)\s+>\[(\S*)\]/.match(row)
16
- return false # this is not a regular Markdown table.
17
- # so the table type shall be changed and this row shall be passed one more time
18
- end
37
+ s += "<table class=\"markdown_table\">\n"
38
+ s += "\t<thead>"
19
39
 
20
- columns = row.split('|')
21
- @rows.append(columns.map!{ |x| x.strip })
22
- return true
40
+ @column_names.each do |h|
41
+ s += " <th>#{h}</th>"
23
42
  end
24
43
 
25
- def to_html
26
- s = ''
27
- if @@htmlTableRenderInProgress
28
- s += "</table>\n"
29
- @@htmlTableRenderInProgress = false
30
- end
31
-
32
- s += "<table class=\"markdown_table\">\n"
33
- s += "\t<thead>"
34
-
35
- @column_names.each do |h|
36
- s += " <th>#{h}</th>"
37
- end
44
+ s += " </thead>\n"
38
45
 
39
- s += " </thead>\n"
40
-
41
- @rows.each do |row|
42
- s += "\t<tr>\n"
43
- row.each do |col|
44
- if col.to_i > 0 && col.to_i.to_s == col # autoalign cells with numbers
45
- s += "\t\t<td style=\"text-align: center;\">#{col}</td>\n"
46
- else
47
- f_text = format_string(col)
48
- s += "\t\t<td>#{f_text}</td>\n"
49
- end
50
- end
51
- s += "\t</tr>\n"
46
+ @rows.each do |row|
47
+ s += "\t<tr>\n"
48
+ row.each do |col|
49
+ if col.to_i.positive? && col.to_i.to_s == col # autoalign cells with numbers
50
+ s += "\t\t<td style=\"text-align: center;\">#{col}</td>\n"
51
+ else
52
+ f_text = format_string(col)
53
+ s += "\t\t<td>#{f_text}</td>\n"
52
54
  end
53
-
54
- s += "</table>\n"
55
-
56
- return s
55
+ end
56
+ s += "\t</tr>\n"
57
57
  end
58
58
 
59
- end
59
+ s += "</table>\n"
60
+
61
+ s
62
+ end
63
+ end
@@ -4,8 +4,10 @@ class Paragraph < DocItem
4
4
 
5
5
  attr_accessor :text
6
6
 
7
- def initialize(text)
8
- @text = text
7
+ def initialize(doc, text)
8
+ @parent_doc = doc
9
+ @parent_heading = doc.headings[-1]
10
+ @text = text.strip
9
11
  end
10
12
 
11
13
  def getTextWithoutSpaces
@@ -14,9 +16,9 @@ class Paragraph < DocItem
14
16
 
15
17
  def to_html
16
18
  s = ''
17
- if @@htmlTableRenderInProgress
19
+ if @@html_table_render_in_progress
18
20
  s += "</table>"
19
- @@htmlTableRenderInProgress = false
21
+ @@html_table_render_in_progress = false
20
22
  end
21
23
 
22
24
  s += "<p>" + format_string(@text)