Almirah 0.2.2 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,313 +1,296 @@
1
1
  class TextLineToken
2
- attr_accessor :value
3
- def initialize()
4
- @value = ""
5
- end
2
+ attr_accessor :value
3
+
4
+ def initialize
5
+ @value = ''
6
+ end
6
7
  end
7
8
 
8
9
  class ItalicToken < TextLineToken
9
- attr_accessor :value
10
- def initialize()
11
- @value = "*"
12
- end
10
+ def initialize # rubocop:disable Lint/MissingSuper
11
+ @value = '*'
12
+ end
13
13
  end
14
14
 
15
15
  class BoldToken < TextLineToken
16
- attr_accessor :value
17
- def initialize()
18
- @value = "**"
19
- end
16
+ def initialize # rubocop:disable Lint/MissingSuper
17
+ @value = '**'
18
+ end
20
19
  end
21
20
 
22
21
  class BoldAndItalicToken < TextLineToken
23
- attr_accessor :value
24
- def initialize()
25
- @value = "***"
26
- end
22
+ def initialize # rubocop:disable Lint/MissingSuper
23
+ @value = '***'
24
+ end
27
25
  end
28
26
 
29
27
  class ParentheseLeft < TextLineToken
30
- attr_accessor :value
31
- def initialize()
32
- @value = "("
33
- end
28
+ def initialize # rubocop:disable Lint/MissingSuper
29
+ @value = '('
30
+ end
34
31
  end
35
32
 
36
33
  class ParentheseRight < TextLineToken
37
- attr_accessor :value
38
- def initialize()
39
- @value = ")"
40
- end
34
+ def initialize
35
+ @value = ')'
36
+ end
41
37
  end
42
38
 
43
39
  class SquareBracketLeft < TextLineToken
44
- attr_accessor :value
45
- def initialize()
46
- @value = "["
47
- end
40
+ def initialize # rubocop:disable Lint/MissingSuper
41
+ @value = '['
42
+ end
48
43
  end
49
44
 
50
45
  class SquareBracketRight < TextLineToken
51
- attr_accessor :value
52
- def initialize()
53
- @value = "]"
54
- end
46
+ def initialize
47
+ @value = ']'
48
+ end
55
49
  end
56
50
 
57
51
  class SquareBracketRightAndParentheseLeft < TextLineToken
58
- attr_accessor :value
59
- def initialize()
60
- @value = "]("
61
- end
52
+ def initialize
53
+ @value = ']('
54
+ end
62
55
  end
63
56
 
64
57
  class TextLineParser
65
- attr_accessor :supported_tokens
66
-
67
- def initialize()
68
- @supported_tokens = Array.new
69
- @supported_tokens.append(BoldAndItalicToken.new)
70
- @supported_tokens.append(BoldToken.new)
71
- @supported_tokens.append(ItalicToken.new)
72
- @supported_tokens.append(SquareBracketRightAndParentheseLeft.new)
73
- @supported_tokens.append(ParentheseLeft.new)
74
- @supported_tokens.append(ParentheseRight.new)
75
- @supported_tokens.append(SquareBracketLeft.new)
76
- @supported_tokens.append(SquareBracketRight.new)
77
- @supported_tokens.append(TextLineToken.new)
78
- end
79
-
80
- def tokenize(str)
81
- result = Array.new
82
- sl = str.length
83
- si = 0
84
- while si < sl
85
- @supported_tokens.each do |t|
86
- tl = t.value.length
87
- if tl != 0 # literal is the last supported token in the list
88
- projected_end_position = si+tl-1
89
- if projected_end_position > sl
90
- next
91
- end
92
- buf = str[si..projected_end_position]
93
- if buf == t.value
94
- result.append(t)
95
- si = projected_end_position +1
96
- break
97
- end
98
- else
99
- if (result.length > 0) and (result[-1].instance_of? TextLineToken)
100
- literal = result[-1]
101
- literal.value += str[si]
102
- else
103
- literal = TextLineToken.new
104
- literal.value = str[si]
105
- result.append(literal)
106
- end
107
- si += 1
108
- end
109
- end
58
+ attr_accessor :supported_tokens
59
+
60
+ def initialize # rubocop:disable Metrics/AbcSize
61
+ @supported_tokens = []
62
+ @supported_tokens.append(BoldAndItalicToken.new)
63
+ @supported_tokens.append(BoldToken.new)
64
+ @supported_tokens.append(ItalicToken.new)
65
+ @supported_tokens.append(SquareBracketRightAndParentheseLeft.new)
66
+ @supported_tokens.append(ParentheseLeft.new)
67
+ @supported_tokens.append(ParentheseRight.new)
68
+ @supported_tokens.append(SquareBracketLeft.new)
69
+ @supported_tokens.append(SquareBracketRight.new)
70
+ @supported_tokens.append(TextLineToken.new)
71
+ end
72
+
73
+ def tokenize(str)
74
+ result = []
75
+ sl = str.length
76
+ si = 0
77
+ while si < sl
78
+ @supported_tokens.each do |t|
79
+ tl = t.value.length
80
+ if tl != 0 # literal is the last supported token in the list
81
+ projected_end_position = si + tl - 1
82
+ next if projected_end_position > sl
83
+
84
+ buf = str[si..projected_end_position]
85
+ if buf == t.value
86
+ result.append(t)
87
+ si = projected_end_position + 1
88
+ break
89
+ end
90
+ else
91
+ if result.length.positive? && (result[-1].instance_of? TextLineToken)
92
+ literal = result[-1]
93
+ literal.value += str[si]
94
+ else
95
+ literal = TextLineToken.new
96
+ literal.value = str[si]
97
+ result.append(literal)
98
+ end
99
+ si += 1
110
100
  end
111
- return result
101
+ end
112
102
  end
103
+ result
104
+ end
113
105
  end
114
106
 
115
107
  class TextLineBuilderContext
108
+ def italic(str)
109
+ str
110
+ end
116
111
 
117
- def italic(str)
118
- return str
119
- end
120
-
121
- def bold(str)
122
- return str
123
- end
112
+ def bold(str)
113
+ str
114
+ end
124
115
 
125
- def bold_and_italic(str)
126
- return str
127
- end
116
+ def bold_and_italic(str)
117
+ str
118
+ end
128
119
 
129
- def link(link_text, link_url)
130
- return link_url
131
- end
120
+ def link(_link_text, link_url)
121
+ link_url
122
+ end
132
123
  end
133
124
 
134
125
  class TextLineBuilder
135
- attr_accessor :builder_context
136
-
137
- def initialize(builder_context)
138
- @builder_context = builder_context
139
- end
140
-
141
- def restore(token_list)
142
- result = ""
143
- if token_list == nil
144
- return ""
126
+ attr_accessor :builder_context
127
+
128
+ def initialize(builder_context)
129
+ @builder_context = builder_context
130
+ end
131
+
132
+ def restore(token_list)
133
+ result = ''
134
+ return '' if token_list.nil?
135
+
136
+ sub_list_url_text = nil
137
+ sub_list_url_address = nil
138
+ tl = token_list.length
139
+ ti = 0
140
+ while ti < tl
141
+ case token_list[ti].class.name
142
+ when 'ItalicToken'
143
+ is_found = false
144
+ ti_starting_position = ti
145
+ # try to find closing part
146
+ tii = ti + 1
147
+ while tii < tl
148
+ if token_list[tii].instance_of? ItalicToken
149
+ sub_list = token_list[(ti + 1)..(tii - 1)]
150
+ result += @builder_context.italic(restore(sub_list))
151
+ ti = tii + 1
152
+ is_found = true
153
+ break
154
+ end
155
+ tii += 1
156
+ end
157
+ unless is_found
158
+ result += '*'
159
+ ti = ti_starting_position + 1
160
+ end
161
+ when 'BoldToken'
162
+ is_found = false
163
+ ti_starting_position = ti
164
+ # try to find closing part
165
+ tii = ti + 1
166
+ while tii < tl
167
+ if token_list[tii].instance_of? BoldToken
168
+ sub_list = token_list[(ti + 1)..(tii - 1)]
169
+ result += @builder_context.bold(restore(sub_list))
170
+ ti = tii + 1
171
+ is_found = true
172
+ break
173
+ end
174
+ tii += 1
175
+ end
176
+ unless is_found
177
+ result += '**'
178
+ ti = ti_starting_position + 1
179
+ end
180
+ when 'BoldAndItalicToken'
181
+ is_found = false
182
+ ti_starting_position = ti
183
+ # try to find closing part
184
+ tii = ti + 1
185
+ while tii < tl
186
+ if token_list[tii].instance_of? BoldAndItalicToken
187
+ sub_list = token_list[(ti + 1)..(tii - 1)]
188
+ result += @builder_context.bold_and_italic(restore(sub_list))
189
+ ti = tii + 1
190
+ is_found = true
191
+ break
192
+ end
193
+ tii += 1
145
194
  end
146
- sub_list_url_text = nil
147
- sub_list_url_address = nil
148
- tl = token_list.length
149
- ti = 0
150
- while ti < tl
151
- case token_list[ti].class.name
152
- when "ItalicToken"
153
- is_found = false
154
- ti_starting_position = ti
155
- # try to find closing part
156
- tii = ti + 1
157
- while tii < tl
158
- if token_list[tii].instance_of? ItalicToken
159
- sub_list = token_list[(ti+1)..(tii-1)]
160
- result += @builder_context.italic(restore(sub_list))
161
- ti = tii +1
162
- is_found = true
163
- break
164
- end
165
- tii += 1
166
- end
167
- unless is_found
168
- result += "*"
169
- ti = ti_starting_position + 1
170
- end
171
- when "BoldToken"
172
- is_found = false
173
- ti_starting_position = ti
174
- # try to find closing part
175
- tii = ti + 1
176
- while tii < tl
177
- if token_list[tii].instance_of? BoldToken
178
- sub_list = token_list[(ti+1)..(tii-1)]
179
- result += @builder_context.bold(restore(sub_list))
180
- ti = tii +1
181
- is_found = true
182
- break
183
- end
184
- tii += 1
185
- end
186
- unless is_found
187
- result += "**"
188
- ti = ti_starting_position + 1
189
- end
190
- when "BoldAndItalicToken"
191
- is_found = false
192
- ti_starting_position = ti
193
- # try to find closing part
194
- tii = ti + 1
195
- while tii < tl
196
- if token_list[tii].instance_of? BoldAndItalicToken
197
- sub_list = token_list[(ti+1)..(tii-1)]
198
- result += @builder_context.bold_and_italic(restore(sub_list))
199
- ti = tii +1
200
- is_found = true
201
- break
202
- end
203
- tii += 1
204
- end
205
- unless is_found
206
- result += "***"
207
- ti = ti_starting_position + 1
208
- end
209
- when "SquareBracketLeft"
210
- # try to find closing part
211
- is_found = false
212
- tii = ti + 1
213
- ti_starting_position = ti
214
- while tii < tl
215
- case token_list[tii].class.name
216
- when "SquareBracketRightAndParentheseLeft"
217
- sub_list_url_text = token_list[(ti+1)..(tii-1)]
218
- ti = tii +1
219
- tiii = ti
220
- while tiii < tl
221
- case token_list[tiii].class.name
222
- when "ParentheseRight"
223
- sub_list_url_address = token_list[(tii+1)..(tiii-1)]
224
- ti = tiii +1
225
- is_found = true
226
- break
227
- end
228
- tiii += 1
229
- end
230
- break
231
- when "SquareBracketRight"
232
- break
233
- end
234
- tii += 1
235
- end
236
- if is_found
237
- result += @builder_context.link(restore(sub_list_url_text), restore(sub_list_url_address))
238
- else
239
- result += "["
240
- ti = ti_starting_position + 1
241
- end
242
-
243
- when "TextLineToken", "ParentheseLeft", "ParentheseRight", "SquareBracketLeft", "SquareBracketRight"
244
- result += token_list[ti].value
245
- ti += 1
246
- else
247
- ti += 1
195
+ unless is_found
196
+ result += '***'
197
+ ti = ti_starting_position + 1
198
+ end
199
+ when 'SquareBracketLeft'
200
+ # try to find closing part
201
+ is_found = false
202
+ tii = ti + 1
203
+ ti_starting_position = ti
204
+ while tii < tl
205
+ case token_list[tii].class.name
206
+ when 'SquareBracketRightAndParentheseLeft'
207
+ sub_list_url_text = token_list[(ti + 1)..(tii - 1)]
208
+ ti = tii + 1
209
+ tiii = ti
210
+ while tiii < tl
211
+ case token_list[tiii].class.name
212
+ when 'ParentheseRight'
213
+ sub_list_url_address = token_list[(tii + 1)..(tiii - 1)]
214
+ ti = tiii + 1
215
+ is_found = true
216
+ break
217
+ end
218
+ tiii += 1
248
219
  end
220
+ break
221
+ when 'SquareBracketRight'
222
+ break
223
+ end
224
+ tii += 1
225
+ end
226
+ if is_found
227
+ result += @builder_context.link(restore(sub_list_url_text), restore(sub_list_url_address))
228
+ else
229
+ result += '['
230
+ ti = ti_starting_position + 1
249
231
  end
250
- return result
232
+
233
+ when 'TextLineToken', 'ParentheseLeft', 'ParentheseRight', 'SquareBracketRight'
234
+ result += token_list[ti].value
235
+ ti += 1
236
+ else
237
+ ti += 1
238
+ end
251
239
  end
240
+ result
241
+ end
252
242
  end
253
243
 
254
244
  class TextLine < TextLineBuilderContext
255
-
256
- @@lazy_doc_id_dict = Hash.new
257
-
258
- def self.add_lazy_doc_id(id)
259
- doc_id = id.to_s.downcase
260
- @@lazy_doc_id_dict[doc_id] = doc_id
261
- end
262
-
263
- def format_string(str)
264
- tlp = TextLineParser.new
265
- tlb = TextLineBuilder.new(self)
266
- tlb.restore(tlp.tokenize(str))
267
- end
268
-
269
- def change_state(c, cur_state, new_state)
270
- # puts "[#{c}] Transition: #{cur_state} --> #{new_state}"
271
- return cur_state, new_state
272
- end
273
-
274
- def italic(str)
275
- "<i>#{str}</i>"
276
- end
277
-
278
- def bold(str)
279
- "<b>#{str}</b>"
280
- end
281
-
282
- def bold_and_italic(str)
283
- "<b><i>#{str}</i></b>"
245
+ @@lazy_doc_id_dict = {}
246
+
247
+ def self.add_lazy_doc_id(id)
248
+ doc_id = id.to_s.downcase
249
+ @@lazy_doc_id_dict[doc_id] = doc_id
250
+ end
251
+
252
+ def format_string(str)
253
+ tlp = TextLineParser.new
254
+ tlb = TextLineBuilder.new(self)
255
+ tlb.restore(tlp.tokenize(str))
256
+ end
257
+
258
+ def italic(str)
259
+ "<i>#{str}</i>"
260
+ end
261
+
262
+ def bold(str)
263
+ "<b>#{str}</b>"
264
+ end
265
+
266
+ def bold_and_italic(str)
267
+ "<b><i>#{str}</i></b>"
268
+ end
269
+
270
+ def link(link_text, link_url)
271
+ # define default result first
272
+ result = "<a target=\"_blank\" rel=\"noopener\" href=\"#{link_url}\" class=\"external\">#{link_text}</a>"
273
+
274
+ lazy_doc_id = nil
275
+ anchor = nil
276
+
277
+ if res = /(\w+)[.]md$/.match(link_url) # link
278
+ lazy_doc_id = res[1].to_s.downcase
279
+
280
+ elsif res = /(\w*)[.]md(#.*)$/.match(link_url) # link with anchor
281
+ if res && res.length > 2
282
+ lazy_doc_id = res[1]
283
+ anchor = res[2]
284
+ end
284
285
  end
285
286
 
286
- def link(link_text, link_url)
287
- # define default result first
288
- result = "<a target=\"_blank\" rel=\"noopener\" href=\"#{link_url}\" class=\"external\">#{link_text}</a>"
289
-
290
- lazy_doc_id, anchor = nil, nil
291
-
292
- if res = /(\w+)[.]md$/.match(link_url) #link
293
- lazy_doc_id = res[1].to_s.downcase
294
-
295
- elsif res = /(\w*)[.]md(#.*)$/.match(link_url) # link with anchor
296
- if res && res.length > 2
297
- lazy_doc_id = res[1]
298
- anchor = res[2]
299
- end
300
- end
301
-
302
- if lazy_doc_id
303
- if @@lazy_doc_id_dict.has_key?(lazy_doc_id)
304
- if anchor
305
- result = "<a href=\".\\..\\#{lazy_doc_id}\\#{lazy_doc_id}.html#{anchor}\" class=\"external\">#{link_text}</a>"
306
- else
307
- result = "<a href=\".\\..\\#{lazy_doc_id}\\#{lazy_doc_id}.html\" class=\"external\">#{link_text}</a>"
308
- end
309
- end
310
- end
311
- return result
287
+ if lazy_doc_id && @@lazy_doc_id_dict.key?(lazy_doc_id)
288
+ result = if anchor
289
+ "<a href=\".\\..\\#{lazy_doc_id}\\#{lazy_doc_id}.html#{anchor}\" class=\"external\">#{link_text}</a>"
290
+ else
291
+ "<a href=\".\\..\\#{lazy_doc_id}\\#{lazy_doc_id}.html\" class=\"external\">#{link_text}</a>"
292
+ end
312
293
  end
313
- end
294
+ result
295
+ end
296
+ end
@@ -11,9 +11,9 @@ class TodoBlock < DocItem
11
11
  def to_html
12
12
  s = ''
13
13
  f_text = format_string(@text)
14
- if @@htmlTableRenderInProgress
14
+ if @@html_table_render_in_progress
15
15
  s += "</table>\n"
16
- @@htmlTableRenderInProgress = false
16
+ @@html_table_render_in_progress = false
17
17
  end
18
18
 
19
19
  s += "<div class=\"todoblock\"><p>#{f_text}</div>\n"