Class: Slimi::Filters::Interpolation

Inherits:
Base
  • Object
show all
Defined in:
lib/slimi/filters/interpolation.rb

Instance Method Summary collapse

Methods inherited from Base

#on_slimi_control, #on_slimi_embedded, #on_slimi_output, #on_slimi_position, #on_slimi_text

Instance Method Details

#on_slimi_interpolate(begin_, _end_, string) ⇒ Array

Returns S-expression.

Parameters:

  • begin_ (Integer)
  • end_ (Integer)

Returns:

  • (Array)

    S-expression.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/slimi/filters/interpolation.rb', line 11

def on_slimi_interpolate(begin_, _end_, string)
  block = [:multi]
  scanner = ::StringScanner.new(string)
  until scanner.eos?
    charpos = scanner.charpos
    if (value = scanner.scan("\\\#{"))
      block << [:static, value]
    elsif scanner.scan(/#\{((?>[^{}]|(\{(?>[^{}]|\g<1>)*\}))*)\}/)
      code = scanner[1]
      begin2 = begin_ + charpos + 2
      if code.start_with?('{') && code.end_with?('}')
        escape = true
        code = code[1..-2]
        begin2 += 1
      else
        escape = false
      end
      block << [:slimi, :position, begin2, begin2 + code.length, [:slimi, :output, escape, code, [:multi]]]
    elsif (value = scanner.scan(/([#\\]?[^#\\]*([#\\][^\\\#{][^#\\]*)*)/)) # rubocop:disable Lint/DuplicateBranch
      block << [:static, value]
    end
  end
  block
end