Class: Hamlit::StringSplitter

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.compile(code) ⇒ Object

‘code` param must be valid string literal



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

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

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

Instance Method Details

#on_dynamic(code) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/hamlit/string_splitter.rb', line 72

def on_dynamic(code)
  return [:dynamic, code] unless RubyExpression.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