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



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

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



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

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

.create_specification(path) ⇒ Object



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

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

.parse_document(doc) ⇒ Object



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


                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)     # 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]

                #check if it contains the uplink

                if tmp = /(.*)\s+>\[(\S*)\]$/.match(text)           # >[SRS-001]


                    text = tmp[1]
                    up_link = tmp[2]
                    
                    if tmp = /^([a-zA-Z]+)[-]\d+/.match(up_link)    # SRS

                        doc.up_link_doc_id[ tmp[1].downcase.to_s ] = tmp[1].downcase       # multiple documents could be up-linked                            

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

                end

                doc.items.append(item)
                doc.dictionary[ id.to_s ] = item       #for fast search

                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.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)     # 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)

            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