Class: Twofold::Interpolation

Inherits:
Filter
  • Object
show all
Defined in:
lib/twofold/interpolation.rb

Instance Method Summary collapse

Methods inherited from Filter

#on_twofold_indented

Instance Method Details

#on_twofold_interpolate(input) ⇒ Object

Works like Temple::Filters::StringSplitter but does not require ripper



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

def on_twofold_interpolate(input)
  # Interpolate ruby code in text (#{variable}).
  # Split the text into multiple dynamic and static parts.
  block = [:multi]
  string = input
  begin
    case string
    when /\A\\#\{/
      # Escaped interpolation
      block << [:static, '#{']
      string = $'
    when /\A#\{((?>[^{}]|(\{(?>[^{}]|\g<1>)*\}))*)\}/
      # Interpolation
      string, code = $', $1
      escape = code !~ /\A\{.*\}\Z/
      block << [:dynamic, escape ? code : code[1..-2]]
    when /\A([#\\]?[^#\\]*([#\\][^\\#\{][^#\\]*)*)/
      # Static text
      block << [:static, $&]
      string = $'
    end
  end until string.empty?
  block
end