From 4388c09d5054f5743ad005f1c0506301154a4916 Mon Sep 17 00:00:00 2001 From: Takashi Kokubun Date: Mon, 23 Nov 2015 17:03:37 +0900 Subject: [PATCH] Distinguish compilation responsibility --- lib/hamlit/compiler/script_compiler.rb | 23 ++++++++++++++++++++++- lib/hamlit/compiler/tag_compiler.rb | 24 +++++++++++++++++++++--- lib/hamlit/string_interpolation.rb | 21 --------------------- 3 files changed, 43 insertions(+), 25 deletions(-) diff --git a/lib/hamlit/compiler/script_compiler.rb b/lib/hamlit/compiler/script_compiler.rb index 2431a062..df800a59 100644 --- a/lib/hamlit/compiler/script_compiler.rb +++ b/lib/hamlit/compiler/script_compiler.rb @@ -12,7 +12,7 @@ module Hamlit def compile(node, &block) case when node.children.empty? && RubyExpression.string_literal?(node.value[:text]) - StringInterpolation.compile_node(node, :text).push([:newline]) + string_compile(node) when node.children.empty? && StaticAnalyzer.static?(node.value[:text]) static_compile(node) else @@ -22,6 +22,27 @@ module Hamlit private + # String-interpolated plain text must be compiled with this method + def string_compile(node) + temple = [:multi] + StringInterpolation.compile(node.value[:text]).each do |type, value| + case type + when :static + value = Temple::Utils.escape_html(value) if node.value[:escape_html] + temple << [:static, value] + when :dynamic + if Hamlit::StaticAnalyzer.static?(value) + value = eval(value).to_s + value = Temple::Utils.escape_html(value) if node.value[:escape_html] || node.value[:escape_interpolation] + temple << [:static, value] + else + temple << [:escape, node.value[:escape_html] || node.value[:escape_interpolation], [:dynamic, value]] + end + end + end + temple << [:newline] + end + def static_compile(node) str = eval("(#{node.value[:text]}).to_s") if node.value[:escape_html] diff --git a/lib/hamlit/compiler/tag_compiler.rb b/lib/hamlit/compiler/tag_compiler.rb index 989525b3..7b699744 100644 --- a/lib/hamlit/compiler/tag_compiler.rb +++ b/lib/hamlit/compiler/tag_compiler.rb @@ -26,9 +26,7 @@ module Hamlit when node.value[:value].nil? && self_closing?(node) nil when node.value[:parse] - if RubyExpression.string_literal?(node.value[:value]) - return StringInterpolation.compile_node(node, :value).push([:newline]) - end + return compile_string(node) if RubyExpression.string_literal?(node.value[:value]) var = @unique_identifier.generate [:multi, @@ -42,6 +40,26 @@ module Hamlit end end + def compile_string(node) + temple = [:multi] + StringInterpolation.compile(node.value[:value]).each do |type, value| + case type + when :static + value = Temple::Utils.escape_html(value) if node.value[:escape_html] + temple << [:static, value] + when :dynamic + if Hamlit::StaticAnalyzer.static?(value) + value = eval(value).to_s + value = Temple::Utils.escape_html(value) if node.value[:escape_html] || node.value[:escape_interpolation] + temple << [:static, value] + else + temple << [:escape, node.value[:escape_html] || node.value[:escape_interpolation], [:dynamic, value]] + end + end + end + temple << [:newline] + end + def self_closing?(node) return true if @autoclose.include?(node.value[:name]) node.value[:self_closing] diff --git a/lib/hamlit/string_interpolation.rb b/lib/hamlit/string_interpolation.rb index 1278aa7c..1e76dfe8 100644 --- a/lib/hamlit/string_interpolation.rb +++ b/lib/hamlit/string_interpolation.rb @@ -16,27 +16,6 @@ module Hamlit::StringInterpolation end end - # Compile Hamlit::HamlParser::ParseNode into Temple AST. - def compile_node(node, key) - [:multi].tap do |temple| - compile(node.value[key]).each do |type, value| - case type - when :static - value = Temple::Utils.escape_html(value) if node.value[:escape_html] - temple << [:static, value] - when :dynamic - if Hamlit::StaticAnalyzer.static?(value) - value = eval(value).to_s - value = Temple::Utils.escape_html(value) if node.value[:escape_html] || node.value[:escape_interpolation] - temple << [:static, value] - else - temple << [:escape, node.value[:escape_html] || node.value[:escape_interpolation], [:dynamic, value]] - end - end - end - end - end - private def strip_quotes!(tokens)