Class: Hamlit::DynamicMerger

Inherits:
Temple::Filter
  • Object
show all
Defined in:
lib/hamlit/dynamic_merger.rb

Overview

Compile [:multi, [:static, 'foo'], [:dynamic, 'bar']] to [:dynamic, '“foo#bar”']

Instance Method Summary collapse

Instance Method Details

#on_multi(*exps) ⇒ Object


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/hamlit/dynamic_merger.rb', line 5

def on_multi(*exps)
  exps = exps.dup
  result = [:multi]
  buffer = []

  until exps.empty?
    type, arg = exps.first
    if type == :dynamic && arg.count("\n") == 0
      buffer << exps.shift
    elsif type == :static && exps.size > (count = arg.count("\n")) &&
          exps[1, count].all? { |e| e == [:newline] }
      (1 + count).times { buffer << exps.shift }
    elsif type == :newline && exps.size > (count = count_newline(exps)) &&
          exps[count].first == :static && count == exps[count].last.count("\n")
      (count + 1).times { buffer << exps.shift }
    else
      result.concat(merge_dynamic(buffer))
      buffer = []
      result << compile(exps.shift)
    end
  end
  result.concat(merge_dynamic(buffer))

  result.size == 2 ? result[1] : result
end