Class: Litbuild::BlueprintParser

Inherits:
Object
  • Object
show all
Defined in:
lib/litbuild/blueprint_parser.rb

Overview

This is a kludgy hand-built parser. Blueprint structure is not (at least, at this point) complicated enough that I can see any point in defining a grammar and using a parser generator. The structure of blueprint directives is described informally in ‘doc/blueprints.txt` (and blueprint-type-specific files under `doc` as well).

tl;dr: each paragraph can be either directives or narrative. Narrative is AsciiDoc and does not get parsed or transformed. Directives are basically a simplified version of YAML.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_text) ⇒ BlueprintParser



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

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 = parse_phase(phase_chunks.shift, {}, @base_grafs)
  base['full-name'] ||= base['name']
  @base_directives = base

  # 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, grafs)
    @phase_grafs[phase_name] = grafs
  end

  # Any directives at the beginning of a blueprint are actually a
  # file header that should not be rendered 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

Instance Attribute Details

#base_directivesObject (readonly)

directives are for use in scripts. grafs are for use in documents.



20
21
22
# File 'lib/litbuild/blueprint_parser.rb', line 20

def base_directives
  @base_directives
end

#base_grafsObject (readonly)

directives are for use in scripts. grafs are for use in documents.



20
21
22
# File 'lib/litbuild/blueprint_parser.rb', line 20

def base_grafs
  @base_grafs
end

#phase_directivesObject (readonly)

directives are for use in scripts. grafs are for use in documents.



20
21
22
# File 'lib/litbuild/blueprint_parser.rb', line 20

def phase_directives
  @phase_directives
end

#phase_grafsObject (readonly)

directives are for use in scripts. grafs are for use in documents.



20
21
22
# File 'lib/litbuild/blueprint_parser.rb', line 20

def phase_grafs
  @phase_grafs
end