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']]

Defined Under Namespace

Classes: SyntaxChecker

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.compile(code)

code param must be valid string literal



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/haml/string_splitter.rb', line 13

def compile(code)
  [].tap do |exps|
    tokens = Ripper.lex(code.strip)
    tokens.pop while tokens.last && [:on_comment, :on_sp].include?(tokens.last[1])

    if tokens.size < 2
      raise(Haml::InternalError, "Expected token size >= 2 but got: #{tokens.size}")
    end
    compile_tokens!(exps, tokens)
  end
end

Instance Method Details

#call(ast)

Do nothing if ripper is unavailable



135
136
137
# File 'lib/haml/string_splitter.rb', line 135

def call(ast)
  ast
end

#on_dynamic(code)



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/haml/string_splitter.rb', line 88

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

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