Class: DocFabric

Inherits:
Object
  • Object
show all
Defined in:
lib/almirah/doc_fabric.rb

Class Method Summary collapse

Class Method Details

.add_lazy_doc_id(path) ⇒ Object



20
21
22
23
24
# File 'lib/almirah/doc_fabric.rb', line 20

def self.add_lazy_doc_id(path)
    if res = /(\w+)[.]md$/.match(path)
        TextLine.add_lazy_doc_id(res[1])
    end
end

.create_protocol(path) ⇒ Object



32
33
34
35
36
# File 'lib/almirah/doc_fabric.rb', line 32

def self.create_protocol(path)
    doc = Protocol.new path
    DocFabric.parse_document doc
    return doc
end

.create_specification(path) ⇒ Object



26
27
28
29
30
# File 'lib/almirah/doc_fabric.rb', line 26

def self.create_specification(path)
    doc = Specification.new path
    DocFabric.parse_document doc
    return doc
end

.parse_document(doc) ⇒ Object



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)     # Heading    
                
                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)     # Pandoc Document Title

                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()   # Pandoc Document Title is not a section, so it shall not be taken into account in numbering
                
            elsif res = /^\[(\S*)\]\s+(.*)/.match(s)     # Controlled Paragraph

                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

                #check if it contains the uplink (one or many)
                #TODO: check this regular expression
                first_pos = text.length # for trailing commas
                tmp =  text.scan( /(>\[(?>[^\[\]]|\g<0>)*\])/ )           # >[SRS-001], >[SYS-002]
                if tmp.length >0
                    up_links = Array.new
                    tmp.each do |ul|
                        up_links.append(ul[0])
                        # try to find the real end of text
                        pos = text.index(ul[0])
                        if pos < first_pos
                            first_pos = pos
                        end
                        # remove uplink from text
                        text = text.split(ul[0]).join("")
                    end
                    # remove trailing commas and spaces
                    if text.length > first_pos
                        first_pos -= 1
                        text = text[0..first_pos].strip
                    end
                end

                # since we already know id and text 
                item = ControlledParagraph.new( text, id )

                if up_links
                    doc.items_with_uplinks_number += 1     #for statistics
                    up_links.each do |ul|
                        if tmp = />\[(\S*)\]$/.match(ul)                    # >[SRS-001]
                            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) # SRS
                                doc.up_link_doc_id[ tmp[1].downcase.to_s ] = tmp[1].downcase       # multiple documents could be up-linked                            
                            end
                        end
                    end
                end

                
                item.parent_doc = doc
                item.parent_heading = doc.headings[-1]

                doc.items.append(item)
                #for statistics
                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       #for fast search
                end
                doc.controlled_items.append(item)      #for fast search

                #for statistics
                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)     # Image

                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)   #check if unordered list start
                
                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)   #check if ordered list start
                
                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] == '|'   #check if table

                if tempMdList
                    doc.items.append tempMdList
                    tempMdList = nil
                end

                if res = /^[|](-{3,})[|]/.match(s) #check if it is a separator first

                    if tempMdTable 
                        #separator is found after heading - just skip it
                    else
                        #separator out of table scope consider it just as a regular paragraph
                        item = Paragraph.new(s)
                        item.parent_doc = doc
                        doc.items.append(item)
                    end

                elsif res = /^[|](.*[|])/.match(s) #check if it looks as a table

                    row = res[1]

                    if tempMdTable
                        # check if it is a controlled table
                        unless tempMdTable.addRow(row)
                            tempMdTable = ControlledTable.new(tempMdTable, doc)
                            tempMdTable.parent_doc = doc
                            tempMdTable.addRow(row)
                        end
                    else
                        #start table from heading
                        tempMdTable = MarkdownTable.new(row)
                        tempMdTable.parent_doc = doc
                    end
                end

            elsif res = /^[>](.*)/.match(s)   #check if blockquote

                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)   #check if TODO block

                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 # Reqular Paragraph
                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   # lists are separated by emty line from each other
                doc.items.append tempMdList
                tempMdList = nil
            end
        end
    end
    # Finalize non-closed elements
    if tempMdTable
        doc.items.append tempMdTable
        tempMdTable = nil
    end
    if tempMdList
        doc.items.append tempMdList
        tempMdList = nil
    end
end