Method: Litbuild::BlueprintParser#initialize

Defined in:
lib/litbuild/blueprint_parser.rb

#initialize(file_text) ⇒ BlueprintParser

Returns a new instance of BlueprintParser.



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
# File 'lib/litbuild/blueprint_parser.rb', line 20

def initialize(file_text)
  @text = file_text
  @phase_directives = {}
  @phase_grafs = {}
  phase_chunks = @text.split(/^phase: ([a-z -_]+)$/)

  # The first part of the blueprint, before any phase directive,
  # becomes the base directives and base narrative for the
  # blueprint. If there are no phase directives, this is the entire
  # blueprint, obvs.
  @base_grafs = []
  @base_directives = parse_phase(phase_chunks.shift, {}, @base_grafs)
  @base_directives['full-name'] ||= @base_directives['name']

  # The rest of the blueprint, if any, consists of directives and
  # narrative for specific phases. The directives for each phase
  # include all the base directives, as well as those specific to
  # the phase.
  until phase_chunks.empty?
    phase_name = phase_chunks.shift
    phase_contents = phase_chunks.shift
    grafs = []
    @phase_directives[phase_name] = parse_phase(phase_contents,
                                                @base_directives,
                                                grafs)
    @phase_grafs[phase_name] = grafs
  end

  # Any directives at the beginning of a blueprint are actually a
  # file header that should not be considered as part of the
  # narrative for it.
  @base_grafs.shift while @base_grafs[0].is_a?(Hash)
rescue StandardError => e
  msg = "Cannot parse blueprint starting: #{@text.lines[0..3].join}"
  raise(Litbuild::ParseError, "#{msg} -- #{e}")
end