class Creq::Parser

Public Class Methods

call(text) click to toggle source
# File lib/creq/parser.rb, line 8
def self.call(text)
  text += "\n" unless text.end_with?("\n")
  regxp = /^(\#+)[ ]*(\[([^\[\]\s]*)\][ ]*)?([\s\S]*?)\n({{([\s\S]*?)}})?(.*)$/m
  parts = regxp.match(text)
  level, id, title, body = parts[1], parts[3], parts[4], parts[7] || ""
  attrs = {id: id, title: title.strip, body: body.strip}
  attrs.merge!(parse_attributes(parts[6])) if parts[6]
  [Requirement.new(attrs), level.size]
rescue StandardError
  puts "Requirement format error for:\n#{text}"
  [nil, nil]
end
parse_attributes(text) click to toggle source
# File lib/creq/parser.rb, line 21
def self.parse_attributes(text)
  text.strip.split(/[;\n]/).inject({}) do |h, i|
    pair = /\s?(\w*):\s*(.*)/.match(i)
    h.merge(pair[1].to_sym => pair[2])
  end || {}
rescue StandardError
  puts "Attributes format error for:\n{{#{text}}}"
  {}
end