1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00

Distinguish compilation responsibility

This commit is contained in:
Takashi Kokubun 2015-11-23 17:03:37 +09:00
parent d3ae6ba474
commit 4388c09d50
3 changed files with 43 additions and 25 deletions

View file

@ -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]

View file

@ -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]

View file

@ -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)