37
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
|
# File 'lib/almirah/doc_fabric.rb', line 37
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]
item = Heading.new(value, level)
item.parent_doc = doc
doc.items.append(item)
doc.headings.append(item)
if level == 1 && doc.title == ""
doc.title = value
end
elsif res = /^\%\s(.*)/.match(s)
title = res[1]
item = Heading.new(title, 1)
item.parent_doc = doc
doc.items.append(item)
doc.headings.append(item)
if doc.title == ""
doc.title = title
end
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]
if tmp = /(.*)\s+>\[(\S*)\]$/.match(text)
text = tmp[1]
up_link = tmp[2]
if tmp = /^([a-zA-Z]+)[-]\d+/.match(up_link)
doc.up_link_doc_id[ tmp[1].downcase.to_s ] = tmp[1].downcase
end
end
item = ControlledParagraph.new( text, id )
item.parent_doc = doc
if up_link
item.up_link = up_link
doc.items_with_uplinks_number += 1
end
doc.items.append(item)
doc.dictionary[ id.to_s ] = item
doc.controlled_items.append(item)
n = /\d+/.match(id)[0].to_i
if n == doc.last_used_id_number
doc.duplicated_ids_number += 1
elsif 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)
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
|