Almirah 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c28aa00ca57d213d56625d51372284eaff2668db942bb08fccd4d2c0bae10a72
4
- data.tar.gz: ce278d1ab81147f9bddae86230da83575921ef26fcd849133e7d138433a6a7fd
3
+ metadata.gz: e9e49efacc05e9b7d14bb1aa9acd2797de76395eb7c21ae40242b6319e59a55b
4
+ data.tar.gz: 6cdba1580e4c9501de9c3e8cf2d1e009a127014a67d0c0fe79d142bb1d8ac662
5
5
  SHA512:
6
- metadata.gz: 73fb32081f15b4314046976855e4474edf94cca23e146507210a6e232418e6b844d1d51c6bf1d7cbaad0b32a203abbc24cf38c1608e400a68485f58d7911ae2d
7
- data.tar.gz: 71155f2804459d0ac0d82777f070e8628f3d1d8d3949bab4468bae35f9d04f495d78523b239a8ed5fff2df16ba98cf789c1b97017deb3b92d01a58ef81a5fe49
6
+ metadata.gz: 62705f4cd221a3964cb579309a09dadcda38688f1df974f026e4957dc3d2380089c690a2b95b7395ec1cdd64eeebb8fdb7ac94f869fb3e348814fba795aec1f6
7
+ data.tar.gz: 53aa6e81cbe3b3156107e3a71fc3c95e9bc8920dfe1615a43d0a9f2de6f0d04fe4325368b9f5563b2833b7f9dfa1a76186a4df18ec223e8a140ccabae18c6dbd
@@ -1,139 +1,270 @@
1
- class TextLine
1
+ class TextLineToken
2
+ attr_accessor :value
3
+ def initialize()
4
+ @value = ""
5
+ end
6
+ end
2
7
 
3
- @@lazy_doc_id_dict = Hash.new
8
+ class ItalicToken < TextLineToken
9
+ attr_accessor :value
10
+ def initialize()
11
+ @value = "*"
12
+ end
13
+ end
4
14
 
5
- def self.add_lazy_doc_id(id)
6
- doc_id = id.to_s.downcase
7
- @@lazy_doc_id_dict[doc_id] = doc_id
15
+ class BoldToken < TextLineToken
16
+ attr_accessor :value
17
+ def initialize()
18
+ @value = "**"
8
19
  end
20
+ end
9
21
 
10
- def format_string(str)
11
- state = 'default'
12
- prev_state = 'default'
13
- result = ''
14
- stack = ''
15
- prev_c = ''
16
- link_text = ''
17
- link_url = ''
18
- str.each_char do |c|
19
- if c == '*'
20
- if state == 'default'
21
- prev_state, state = change_state(c, state, 'first_asterisk_detected')
22
-
23
- elsif state == 'first_asterisk_detected'
24
- prev_state, state = change_state(c, state, 'second_asterisk_detected')
25
-
26
- elsif state == 'second_asterisk_detected'
27
- prev_state, state = change_state(c, state, 'third_asterisk_detected')
28
-
29
- elsif state == 'second_asterisk_detected'
30
- prev_state, state = change_state(c, state, 'third_asterisk_detected')
31
-
32
- elsif state == 'italic_started'
33
- prev_state, state = change_state(c, state, 'default')
34
- result += italic(stack)
35
-
36
- elsif state == 'bold_started'
37
- prev_state, state = change_state(c, state, 'first_asterisk_after_bold_detected')
38
-
39
- elsif state == 'first_asterisk_after_bold_detected'
40
- prev_state, state = change_state(c, state, 'default')
41
- result += bold(stack)
42
-
43
- elsif state == 'bold_and_italic_started'
44
- prev_state, state = change_state(c, state, 'first_asterisk_after_bold_and_italic_detected')
45
-
46
- elsif state == 'first_asterisk_after_bold_and_italic_detected'
47
- prev_state, state = change_state(c, state, 'second_asterisk_after_bold_and_italic_detected')
48
-
49
- elsif state == 'second_asterisk_after_bold_and_italic_detected'
50
- prev_state, state = change_state(c, state, 'default')
51
- result += bold_and_italic(stack)
22
+ class BoldAndItalicToken < TextLineToken
23
+ attr_accessor :value
24
+ def initialize()
25
+ @value = "***"
26
+ end
27
+ end
52
28
 
53
- else
54
- end
55
- elsif c == '['
56
- if state == 'default'
57
- prev_state, state = change_state(c, state, 'square_bracket_left_detected')
58
- else
59
- end
60
- elsif c == ']'
61
- if state == 'square_bracket_left_detected'
62
- prev_state, state = change_state(c, state, 'default')
63
- result += '[]'
29
+ class ParentheseLeft < TextLineToken
30
+ attr_accessor :value
31
+ def initialize()
32
+ @value = "("
33
+ end
34
+ end
64
35
 
65
- elsif state == 'link_text_started'
66
- prev_state, state = change_state(c, state, 'square_bracket_right_detected')
67
- link_text = stack
36
+ class ParentheseRight < TextLineToken
37
+ attr_accessor :value
38
+ def initialize()
39
+ @value = ")"
40
+ end
41
+ end
68
42
 
69
- else
70
- end
71
- elsif c == '('
72
- if state == 'square_bracket_right_detected'
73
- prev_state, state = change_state(c, state, 'brace_left_detected')
74
- else
75
- result += c
76
- end
77
- elsif c == ')'
78
- if state == 'brace_left_detected'
79
- prev_state, state = change_state(c, state, 'default')
80
- result += '()'
43
+ class SquareBracketLeft < TextLineToken
44
+ attr_accessor :value
45
+ def initialize()
46
+ @value = "["
47
+ end
48
+ end
49
+
50
+ class SquareBracketRight < TextLineToken
51
+ attr_accessor :value
52
+ def initialize()
53
+ @value = "]"
54
+ end
55
+ end
81
56
 
82
- elsif state == 'link_url_started'
83
- prev_state, state = change_state(c, state, 'default')
84
- link_url = stack
85
- result += link(link_text, link_url)
57
+ class SquareBracketRightAndParentheseLeft < TextLineToken
58
+ attr_accessor :value
59
+ def initialize()
60
+ @value = "]("
61
+ end
62
+ end
63
+
64
+ 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
86
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
87
98
  else
88
- result += c
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
89
108
  end
90
- else
91
- if state == 'default'
92
- result += c
93
- else
94
- if state == 'first_asterisk_detected'
95
- prev_state, state = change_state(c, state, 'italic_started')
96
- stack = ''
109
+ end
110
+ end
111
+ return result
112
+ end
113
+ end
97
114
 
98
- elsif state == 'second_asterisk_detected'
99
- prev_state, state = change_state(c, state, 'bold_started')
100
- stack = ''
115
+ class TextLineBuilderContext
101
116
 
102
- elsif state == 'third_asterisk_detected'
103
- prev_state, state = change_state(c, state, 'bold_and_italic_started')
104
- stack = ''
105
-
106
- elsif state == 'first_asterisk_after_bold_detected'
107
- prev_state, state = change_state(c, state, 'bold_started')
117
+ def italic(str)
118
+ return str
119
+ end
108
120
 
109
- elsif state == 'first_asterisk_after_bold_and_italic_detected'
110
- prev_state, state = change_state(c, state, 'bold_and_italic_started')
121
+ def bold(str)
122
+ return str
123
+ end
111
124
 
112
- elsif state == 'second_asterisk_after_bold_and_italic_detected'
113
- prev_state, state = change_state(c, state, 'bold_and_italic_started')
125
+ def bold_and_italic(str)
126
+ return str
127
+ end
114
128
 
115
- elsif state == 'square_bracket_left_detected'
116
- prev_state, state = change_state(c, state, 'link_text_started')
117
- stack = ''
129
+ def link(link_text, link_url)
130
+ return link_url
131
+ end
132
+ end
118
133
 
119
- elsif state == 'square_bracket_right_detected'
120
- prev_state, state = change_state(c, state, 'default')
121
- result += stack + c
122
- c = ''
134
+ class TextLineBuilder
135
+ attr_accessor :builder_context
123
136
 
124
- elsif state == 'brace_left_detected'
125
- prev_state, state = change_state(c, state, 'link_url_started')
126
- stack = ''
137
+ def initialize(builder_context)
138
+ @builder_context = builder_context
139
+ end
127
140
 
128
- else
129
- end
130
- stack += c
141
+ def restore(token_list)
142
+ result = ""
143
+ if token_list == nil
144
+ return ""
145
+ 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
131
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
132
248
  end
133
- prev_c = c
134
249
  end
135
250
  return result
136
251
  end
252
+ end
253
+
254
+ 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
137
268
 
138
269
  def change_state(c, cur_state, new_state)
139
270
  # puts "[#{c}] Transition: #{cur_state} --> #{new_state}"
@@ -153,7 +284,6 @@ class TextLine
153
284
  end
154
285
 
155
286
  def link(link_text, link_url)
156
-
157
287
  # define default result first
158
288
  result = "<a target=\"_blank\" rel=\"noopener\" href=\"#{link_url}\" class=\"external\">#{link_text}</a>"
159
289
 
@@ -12,7 +12,7 @@ class DocSection
12
12
 
13
13
  def to_html
14
14
  s = ''
15
- s += "\t<li><span class=\"fa-li\"><i class=\"fa fa-plus-square-o\"> </i></span>"
15
+ s += "\t<li><span class=\"fa-li\"><i class=\"fa fa-sticky-note-o\"> </i></span>"
16
16
  s += "<a href=\"\#" + @heading.anchor_id.to_s + "\">" + @heading.get_section_info + "</a>\n"
17
17
  if @sections.length >0
18
18
  s += "\t\t<ul class=\"fa-ul\">\n"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: Almirah
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oleksandr Ivanov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-05-07 00:00:00.000000000 Z
11
+ date: 2024-05-13 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: The software part of the Almirah framework
14
14
  email: oleksandr.ivanov.development@gmail.com