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

Compile static script statically

This commit is contained in:
Takashi Kokubun 2015-11-14 02:33:06 +09:00
parent 6b3c46267b
commit 2c23f95fbe
3 changed files with 33 additions and 2 deletions

View file

@ -1,2 +1,4 @@
= ['&', '"', "'", '<', '>'] = ['&', '"', "'", '<', '>']
= "hello#{ ' ' }world" = "hello#{ ' ' }world"
- dynamic = 'dynamic'
= "hello #{ dynamic } world"

View file

@ -1,3 +1,5 @@
require 'hamlit/static_analyzer'
module Hamlit module Hamlit
class Compiler class Compiler
class ScriptCompiler class ScriptCompiler
@ -6,13 +8,31 @@ module Hamlit
end end
def compile(node, &block) def compile(node, &block)
if node.children.empty? && StaticAnalyzer.static?(node.value[:text])
static_compile(node)
else
dynamic_compile(node, &block)
end
end
private
def static_compile(node)
str = eval("(#{node.value[:text]}).to_s")
if node.value[:escape_html]
str = Temple::Utils.escape_html(str)
elsif node.value[:preserve]
str = Haml::Helpers.find_and_preserve(str, %w(textarea pre code))
end
[:static, str]
end
def dynamic_compile(node, &block)
var = unique_identifier var = unique_identifier
temple = compile_script_assign(var, node, &block) temple = compile_script_assign(var, node, &block)
temple << compile_script_result(var, node) temple << compile_script_result(var, node)
end end
private
def compile_script_assign(var, node, &block) def compile_script_assign(var, node, &block)
if node.children.empty? if node.children.empty?
[:multi, [:multi,

View file

@ -12,6 +12,15 @@ describe Hamlit::Engine do
HTML HTML
end end
it 'renders dynamic interpolated string' do
assert_render(<<-'HAML', <<-HTML)
- nya = 'nya'
= "hello #{nya} world"
HAML
hello nya world
HTML
end
it 'renders array with escape_html: false' do it 'renders array with escape_html: false' do
assert_render(<<-HAML, <<-HTML, escape_html: false) assert_render(<<-HAML, <<-HTML, escape_html: false)
= ['<', '>'] = ['<', '>']