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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
|
# File 'lib/almirah/doc_fabric.rb', line 38
def self.parse_document(doc)
tempMdTable = nil
tempMdList = nil
file = File.open( doc.path )
file_lines = file.readlines
file.close
file_lines.each do |s|
if s.lstrip != ""
if res = /^([#]{1,})\s(.*)/.match(s)
if tempMdTable
doc.items.append tempMdTable
tempMdTable = nil
end
if tempMdList
doc.items.append tempMdList
tempMdList = nil
end
level = res[1].length
value = res[2]
if level == 1 && doc.title == ""
doc.title = value
Heading.reset_global_section_number()
end
item = Heading.new(value, level)
item.parent_doc = doc
doc.items.append(item)
doc.headings.append(item)
elsif res = /^\%\s(.*)/.match(s)
title = res[1]
if doc.title == ""
doc.title = title
Heading.reset_global_section_number()
end
item = Heading.new(title, 1)
item.parent_doc = doc
doc.items.append(item)
doc.headings.append(item)
Heading.reset_global_section_number()
elsif res = /^\[(\S*)\]\s+(.*)/.match(s)
if tempMdTable
doc.items.append tempMdTable
tempMdTable = nil
end
if tempMdList
doc.items.append tempMdList
tempMdList = nil
end
id = res[1]
text = res[2]
up_links = nil
first_pos = text.length tmp = text.scan( /(>\[(?>[^\[\]]|\g<0>)*\])/ ) if tmp.length >0
up_links = Array.new
tmp.each do |ul|
up_links.append(ul[0])
pos = text.index(ul[0])
if pos < first_pos
first_pos = pos
end
text = text.split(ul[0]).join("")
end
if text.length > first_pos
first_pos -= 1
text = text[0..first_pos].strip
end
end
item = ControlledParagraph.new( text, id )
if up_links
doc.items_with_uplinks_number += 1 up_links.each do |ul|
if tmp = />\[(\S*)\]$/.match(ul) up_link_id = tmp[1]
unless item.up_link_ids
item.up_link_ids = Array.new
end
item.up_link_ids.append(up_link_id)
if tmp = /^([a-zA-Z]+)[-]\d+/.match(up_link_id) doc.up_link_doc_id[ tmp[1].downcase.to_s ] = tmp[1].downcase end
end
end
end
item.parent_doc = doc
item.parent_heading = doc.headings[-1]
doc.items.append(item)
if doc.dictionary.has_key?( id.to_s )
doc.duplicated_ids_number += 1
doc.duplicates_list.append(item)
else
doc.dictionary[ id.to_s ] = item end
doc.controlled_items.append(item)
n = /\d+/.match(id)[0].to_i
if n > doc.last_used_id_number
doc.last_used_id = id
doc.last_used_id_number = n
end
elsif res = /^[!]\[(.*)\]\((.*)\)/.match(s)
if tempMdTable
doc.items.append tempMdTable
tempMdTable = nil
end
if tempMdList
doc.items.append tempMdList
tempMdList = nil
end
img_text = res[1]
img_path = res[2]
item = Image.new( img_text, img_path )
item.parent_doc = doc
doc.items.append(item)
elsif res = /^(\*\s?)(.*)/.match(s)
if tempMdTable
doc.items.append tempMdTable
tempMdTable = nil
end
row = res[2]
if tempMdList
tempMdList.addRow(s)
else
item = MarkdownList.new(false)
item.addRow(s)
item.parent_doc = doc
tempMdList = item
end
elsif res = /^\d[.]\s(.*)/.match(s)
if tempMdTable
doc.items.append tempMdTable
tempMdTable = nil
end
row = res[1]
if tempMdList
tempMdList.addRow(s)
else
item = MarkdownList.new(true)
item.addRow(s)
item.parent_doc = doc
tempMdList = item
end
elsif s[0] == '|'
if tempMdList
doc.items.append tempMdList
tempMdList = nil
end
if res = /^[|](-{3,})[|]/.match(s)
if tempMdTable
else
item = Paragraph.new(s)
item.parent_doc = doc
doc.items.append(item)
end
elsif res = /^[|](.*[|])/.match(s)
row = res[1]
if tempMdTable
unless tempMdTable.addRow(row)
tempMdTable = ControlledTable.new(tempMdTable, doc)
tempMdTable.parent_doc = doc
tempMdTable.addRow(row)
end
else
tempMdTable = MarkdownTable.new(row)
tempMdTable.parent_doc = doc
end
end
elsif res = /^[>](.*)/.match(s)
if tempMdTable
doc.items.append tempMdTable
tempMdTable = nil
end
if tempMdList
doc.items.append tempMdList
tempMdList = nil
end
item = Blockquote.new(res[1])
item.parent_doc = doc
doc.items.append(item)
elsif res = /^TODO\:(.*)/.match(s)
if tempMdTable
doc.items.append tempMdTable
tempMdTable = nil
end
if tempMdList
doc.items.append tempMdList
tempMdList = nil
end
text = "**TODO**: " + res[1]
item = TodoBlock.new(text)
item.parent_doc = doc
doc.items.append(item)
doc.todo_blocks.append(item)
else if tempMdTable
doc.items.append tempMdTable
tempMdTable = nil
end
if tempMdList
if MarkdownList.unordered_list_item?(s) || MarkdownList.ordered_list_item?(s)
tempMdList.addRow(s)
next
else
doc.items.append tempMdList
tempMdList = nil
end
end
item = Paragraph.new(s)
item.parent_doc = doc
doc.items.append(item)
end
else
if tempMdList doc.items.append tempMdList
tempMdList = nil
end
end
end
if tempMdTable
doc.items.append tempMdTable
tempMdTable = nil
end
if tempMdList
doc.items.append tempMdList
tempMdList = nil
end
end
|