Class: Temple::Filters::StringSplitter

Inherits:
Temple::Filter show all
Defined in:
lib/temple/filters/string_splitter.rb

Overview

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

Defined Under Namespace

Classes: SyntaxChecker

Constant Summary

Constants included from Utils

Utils::ESCAPE_HTML, Utils::ESCAPE_HTML_PATTERN

Instance Attribute Summary

Attributes included from Mixins::Options

#options

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixins::Options

included, #initialize

Methods included from Mixins::ControlFlowDispatcher

#on_block, #on_case, #on_cond, #on_if

Methods included from Mixins::EscapeDispatcher

#on_escape

Methods included from Mixins::CoreDispatcher

#on_capture, #on_multi

Methods included from Mixins::CompiledDispatcher

#compile

Methods included from Utils

#empty_exp?, #escape_html, #escape_html_safe, #indent_dynamic, #unique_name

Class Method Details

.compile(code) ⇒ Object

‘code` param must be valid string literal



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

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(FilterError, "Expected token size >= 2 but got: #{tokens.size}")
    end
    compile_tokens!(exps, tokens)
  end
end

Instance Method Details

#call(ast) ⇒ Object

Do nothing if ripper is unavailable



136
137
138
# File 'lib/temple/filters/string_splitter.rb', line 136

def call(ast)
  ast
end

#on_dynamic(code) ⇒ Object



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

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