class Hamlit::Compiler::ScriptCompiler
Public Class Methods
new(identity)
click to toggle source
# File lib/hamlit/compiler/script_compiler.rb, line 8 def initialize(identity) @identity = identity end
Public Instance Methods
compile(node, &block)
click to toggle source
# File lib/hamlit/compiler/script_compiler.rb, line 12 def compile(node, &block) no_children = node.children.empty? case when no_children && node.value[:escape_interpolation] compile_interpolated_plain(node) when no_children && RubyExpression.string_literal?(node.value[:text]) delegate_optimization(node) when no_children && StaticAnalyzer.static?(node.value[:text]) static_compile(node) else dynamic_compile(node, &block) end end
Private Instance Methods
compile_interpolated_plain(node)
click to toggle source
String-interpolated plain text must be compiled with this method because we have to escape only interpolated values.
# File lib/hamlit/compiler/script_compiler.rb, line 30 def compile_interpolated_plain(node) temple = [:multi] StringSplitter.compile(node.value[:text]).each do |type, value| case type when :static temple << [:static, value] when :dynamic temple << [:escape, node.value[:escape_interpolation], [:dynamic, value]] end end temple << [:newline] end
compile_script_assign(var, node) { |node| ... }
click to toggle source
# File lib/hamlit/compiler/script_compiler.rb, line 67 def compile_script_assign(var, node, &block) if node.children.empty? [:multi, [:code, "#{var} = (#{node.value[:text]}"], [:newline], [:code, ')'], ] else [:multi, [:block, "#{var} = #{node.value[:text]}", [:multi, [:newline], yield(node)], ], ] end end
compile_script_result(result, node)
click to toggle source
# File lib/hamlit/compiler/script_compiler.rb, line 83 def compile_script_result(result, node) if !node.value[:escape_html] && node.value[:preserve] result = find_and_preserve(result) else result = "(#{result}).to_s" end [:escape, node.value[:escape_html], [:dynamic, result]] end
delegate_optimization(node)
click to toggle source
:dynamic is optimized in other filter: StringSplitter
# File lib/hamlit/compiler/script_compiler.rb, line 44 def delegate_optimization(node) [:multi, [:escape, node.value[:escape_html], [:dynamic, node.value[:text]]], [:newline], ] end
dynamic_compile(node, &block)
click to toggle source
# File lib/hamlit/compiler/script_compiler.rb, line 61 def dynamic_compile(node, &block) var = @identity.generate temple = compile_script_assign(var, node, &block) temple << compile_script_result(var, node) end
escape_html(temple)
click to toggle source
# File lib/hamlit/compiler/script_compiler.rb, line 96 def escape_html(temple) [:escape, true, temple] end
find_and_preserve(code)
click to toggle source
# File lib/hamlit/compiler/script_compiler.rb, line 92 def find_and_preserve(code) %Q[::Hamlit::HamlHelpers.find_and_preserve(#{code}, %w(textarea pre code))] end
static_compile(node)
click to toggle source
# File lib/hamlit/compiler/script_compiler.rb, line 51 def static_compile(node) str = eval(node.value[:text]).to_s if node.value[:escape_html] str = Hamlit::Utils.escape_html(str) elsif node.value[:preserve] str = ::Hamlit::HamlHelpers.find_and_preserve(str, %w(textarea pre code)) end [:multi, [:static, str], [:newline]] end