Module: Precedent::Inline::Inline2

Defined in:
lib/precedent/grammar/inline.rb

Instance Method Summary collapse

Instance Method Details

#buildObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/precedent/grammar/inline.rb', line 31

def build
  elems = subsequent.elements.map(&:build).flatten
  # Members of `subsequent` come in [nil, Node] lists when there
  # is no preceding line break. The car values can't be ignored,
  # as we need to convert newlines to spaces when they occur.
  ret = elems.reduce([first.build]) do |mem, e|
    last = mem.last
    # Start the output list with the first element
    if e.nil?
      mem
    # Concatenate contiguous strings
    elsif last.is_a?(String) && e.is_a?(String)
      mem + [mem.pop + e]
    else # Hash
      mem + [e]
    end
  end
  # If there is just one content element, give the element
  # rather than a one-element list.
  ret.count == 1 ? ret.first : ret
end