Class: Temple::Filters::StringSplitter
- Inherits:
-
Temple::Filter
- Object
- Temple::Filter
- Temple::Filters::StringSplitter
- 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
Class Method Summary collapse
-
.compile(code) ⇒ Object
codeparam must be valid string literal. - .try_compile(code) ⇒ Object
Instance Method Summary collapse
-
#call(ast) ⇒ Object
Do nothing if ripper is unavailable.
- #on_dynamic(code) ⇒ Object
Methods included from Mixins::Options
Methods included from Mixins::ControlFlowDispatcher
#on_block, #on_case, #on_cond, #on_if
Methods included from Mixins::EscapeDispatcher
Methods included from Mixins::CoreDispatcher
Methods included from Mixins::CompiledDispatcher
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
11 12 13 14 15 |
# File 'lib/temple/filters/string_splitter.rb', line 11 def compile(code) compiled = try_compile(code) raise(FilterError, "Expected string literal") unless compiled compiled end |
.try_compile(code) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/temple/filters/string_splitter.rb', line 17 def try_compile(code) result = Prism.parse(code) return unless result.success? stmts = result.value.statements.body return if stmts.size != 1 stack = [stmts.first] compiled = [] until stack.empty? case (node = stack.pop).type when :interpolated_string_node stack.concat(node.parts.reverse) when :string_node if !(unescaped = node.unescaped).empty? compiled << [:static, unescaped] end when :embedded_statements_node if (stmts = node.statements) && !(slice = stmts.slice).empty? compiled << [:dynamic, slice] end when :embedded_variable_node compiled << [:dynamic, node.variable.slice] else return end end compiled end |
Instance Method Details
#call(ast) ⇒ Object
Do nothing if ripper is unavailable
192 193 194 |
# File 'lib/temple/filters/string_splitter.rb', line 192 def call(ast) ast end |
#on_dynamic(code) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/temple/filters/string_splitter.rb', line 50 def on_dynamic(code) return [:dynamic, code] if code.include?("\n") compiled = StringSplitter.try_compile(code) return [:dynamic, code] unless compiled temple = [:multi] compiled.each do |type, content| case type when :static temple << [:static, content] when :dynamic temple << on_dynamic(content) end end temple end |