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



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

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

#on_dynamic(code)



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/haml/string_splitter.rb', line 84

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