Class: Haml::StringSplitter

Inherits:
Temple::Filter
  • Object
show all
Defined in:
lib/haml/string_splitter.rb

Overview

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.compile(code, node: RubyExpression.string_literal_node(code))

code param must be a string literal, as RubyExpression.string_literal? defines it.



10
11
12
13
14
15
16
17
18
19
# File 'lib/haml/string_splitter.rb', line 10

def compile(code, node: RubyExpression.string_literal_node(code))
  case node
  when Prism::StringNode
    node.unescaped.empty? ? [] : [[:static, node.unescaped]]
  when Prism::InterpolatedStringNode
    compile_parts(node.parts, code)
  else
    raise(Haml::InternalError, "Expected a string literal but got: #{code}")
  end
end

.try_compile(code)

Like compile, but nil instead of raising, for callers that built code themselves and would rather emit it untouched than fail the whole compilation.



23
24
25
26
# File 'lib/haml/string_splitter.rb', line 23

def try_compile(code)
  node = RubyExpression.string_literal_node(code)
  compile(code, node: node) unless node.nil?
end

Instance Method Details

#on_dynamic(code)



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/haml/string_splitter.rb', line 59

def on_dynamic(code)
  return [:dynamic, code] if code.include?("\n")

  node = RubyExpression.string_literal_node(code)
  return [:dynamic, code] if node.nil?

  temple = [:multi]
  StringSplitter.compile(code, node: node).each do |type, content|
    case type
    when :static
      temple << [:static, content]
    when :dynamic
      temple << on_dynamic(content)
    end
  end
  temple
end