1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00
haml--haml/lib/hamlit/script_compiler.rb
2015-03-17 23:54:47 +09:00

26 lines
604 B
Ruby

require 'hamlit/filter'
require 'hamlit/concerns/escapable'
module Hamlit
class ScriptCompiler < Hamlit::Filter
include Concerns::Escapable
def on_haml_script(*exps)
exps = exps.dup
variable = result_identifier
code = exps.shift
assign = [:code, "#{variable} = #{code}"]
result = escape_html([:dynamic, variable])
[:multi, assign, *exps.map { |exp| compile(exp) }, result]
end
private
def result_identifier
@id_auto_increment ||= -1
@id_auto_increment += 1
"_hamlit_compiler#{@id_auto_increment}"
end
end
end