Module: Bob::Compiler::Substitution

Included in:
Walker
Defined in:
lib/bob/compiler/substitution.rb

Constant Summary collapse

SUBSTITUTION_SCANNER =
/(\\?{{.+?}})/
SUBSTITUTION_PARSER =
/
  \A
    {{
      [[:blank:]]*
      (?<path> [0-9a-z_]+ (?:\.[0-9a-z_]+)* )
      [[:blank:]]*
      (?:
        \|\|
        [[:blank:]]*
        (?:
          (?: ' (?<fallback> (?:[^'\\{}]|\\.)*) ' ) |
          (?: " (?<fallback> (?:[^"\\{}]|\\.)*) " )
        )
        [[:blank:]]*
      )?
    }}
  \z
/xi

Instance Method Summary collapse

Instance Method Details

#quote_and_substitute(arg, node) ⇒ Object



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
# File 'lib/bob/compiler/substitution.rb', line 25

def quote_and_substitute(arg, node)
  parts = arg.split(SUBSTITUTION_SCANNER).reject(&:empty?)

  if parts.empty?
    %{""}
  else
    parts.map { |str|

      if SUBSTITUTION_SCANNER =~ str
        if str[0] == "\\"
          quote(str[1..-1] || "")
        else
          if (parsed = SUBSTITUTION_PARSER.match(str))
            if parsed[:fallback]
              "env.get(#{quote parsed[:path]}, #{quote unescape parsed[:fallback]})"
            else
              "env.get(#{quote parsed[:path]})"
            end
          else
            warn "Treating invalid substitution as string: #{str.inspect}", node.parent.line
            quote str
          end
        end
      else
        quote str
      end

    }.join(" + ")
  end
end

#unescape(str) ⇒ Object



56
57
58
# File 'lib/bob/compiler/substitution.rb', line 56

def unescape(str)
  str.gsub(/\\(.)/, "\\1")
end