Class: SourceFileParser

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

Overview

rubocop:disable Style/Documentation

Class Method Summary collapse

Class Method Details

.parse(doc, file_lines) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
# File 'lib/almirah/source_file_parser.rb', line 6

def self.parse(doc, file_lines)
  # restart section numbering for each new document
  Heading.reset_global_section_number

  # There is no document without heading
  title = "[#{doc.repository}] : #{doc.id}"
  item = Heading.new(doc, title, 0)
  doc.items.append(item)
  doc.headings.append(item)
  doc.title = title

  item = Heading.new(doc, 'References', 2)
  doc.items.append(item)
  doc.headings.append(item)

  # main loop
  file_lines.each do |s| # rubocop:disable Metrics/BlockLength
    res = %r{<REQ>(.*)</REQ>}.match(s) # Document Referece Item
    next unless res

    # extract text between <REQ> and </REQ>
    # id will be generated automatically
    text = res[1].strip
    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.positive?
      up_links = []
      tmp.each do |ul|
        lnk = ul[0]
        #
        doc_id = /([a-zA-Z]+)-\d+/.match(lnk) # SRS
        up_links << lnk.upcase if doc_id
        # try to find the real end of text
        pos = text.index(lnk)
        first_pos = pos if pos < first_pos
        # remove uplink from text
        text = text.split(lnk, 1).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 = SourceCodeParagraph.new(doc, text)

    if up_links
      up_links.uniq! # remove duplicates
      doc.items_with_uplinks_number += 1 # for statistics
      up_links.each do |ul|
        next unless tmp = />\[(\S*)\]$/.match(ul) # >[(SRS-001)]

        up_link_id = tmp[1]

        item.up_link_ids = [] unless item.up_link_ids

        item.up_link_ids.append(up_link_id)

        if tmp = /^([a-zA-Z]+)-\d+/.match(up_link_id) # SRS
          doc.up_link_docs[tmp[1].downcase.to_s] = tmp[1].downcase # multiple documents could be up-linked
        end
      end
    end

    doc.items.append(item)
    # for statistics
    if doc.dictionary.has_key?(item.id.to_s)
      doc.duplicated_ids_number += 1
      doc.duplicates_list.append(item)
    else
      doc.dictionary[item.id.to_s] = item # for fast search
    end
    doc.controlled_items.append(item) # for fast search
  end

  item = Heading.new(doc, 'Source Code', 2)
  doc.items.append(item)
  doc.headings.append(item)
end