Support preserve

This commit is contained in:
Takashi Kokubun 2015-10-25 16:14:55 +09:00
parent 8866da50e9
commit 1fe3a2f81a
7 changed files with 18 additions and 17 deletions

View File

@ -8,24 +8,21 @@ module Hamlit
def compile(node, &block)
var = unique_identifier
temple = compile_script_assign(var, node, &block)
temple << [:escape, node.value[:escape_html], [:dynamic, var]]
temple << compile_script_result(var, node)
end
private
def compile_script_assign(var, node, &block)
code = node.value[:text]
code = find_and_preserve(code) if node.value[:preserve]
if node.children.empty?
[:multi,
[:code, "#{var} = (#{code}"],
[:code, "#{var} = (#{node.value[:text]}"],
[:newline],
[:code, ')'.freeze],
]
else
[:multi,
[:code, "#{var} = #{code}"],
[:code, "#{var} = #{node.value[:text]}"],
[:newline],
yield(node),
[:code, 'end'.freeze],
@ -33,6 +30,11 @@ module Hamlit
end
end
def compile_script_result(result, node)
result = find_and_preserve(result) if !node.value[:escape_html] && node.value[:preserve]
[:escape, node.value[:escape_html], [:dynamic, result]]
end
def unique_identifier
@unique_id += 1
['_hamlit_compiler'.freeze, @unique_id].join

View File

@ -2,14 +2,15 @@ require 'haml'
module Hamlit
class Parser
OPTION_MAPS = {
autoclose: :autoclose,
}.freeze
AVAILABLE_OPTIONS = %i[
autoclose
escape_html
].freeze
def initialize(options = {})
@options = Haml::Options.defaults.dup
OPTION_MAPS.each do |temple, haml|
@options[haml] = options[temple]
AVAILABLE_OPTIONS.each do |key|
@options[key] = options[key]
end
end

View File

@ -16,7 +16,7 @@ def generate_spec(mode)
# This is a spec converted by haml-spec.
# See: https://github.com/haml/haml-spec
class #{mode.capitalize}Test < MiniTest::Test
DEFAULT_OPTIONS = { ugly: #{mode == :ugly} }.freeze
DEFAULT_OPTIONS = { ugly: #{mode == :ugly}, escape_html: true }.freeze
def self.haml_result(haml, options, locals)
Haml::Engine.new(haml, DEFAULT_OPTIONS.merge(options)).render(Object.new, locals)

View File

@ -6,7 +6,7 @@ require 'minitest/autorun'
# This is a spec converted by haml-spec.
# See: https://github.com/haml/haml-spec
class PrettyTest < MiniTest::Test
DEFAULT_OPTIONS = { ugly: false }.freeze
DEFAULT_OPTIONS = { ugly: false, escape_html: true }.freeze
def self.haml_result(haml, options, locals)
Haml::Engine.new(haml, DEFAULT_OPTIONS.merge(options)).render(Object.new, locals)

View File

@ -6,7 +6,7 @@ require 'minitest/autorun'
# This is a spec converted by haml-spec.
# See: https://github.com/haml/haml-spec
class UglyTest < MiniTest::Test
DEFAULT_OPTIONS = { ugly: true }.freeze
DEFAULT_OPTIONS = { ugly: true, escape_html: true }.freeze
def self.haml_result(haml, options, locals)
Haml::Engine.new(haml, DEFAULT_OPTIONS.merge(options)).render(Object.new, locals)

View File

@ -13,7 +13,6 @@ describe Hamlit::Engine do
end
it 'renders one-line script with comment' do
skip
assert_render(<<-HAML, <<-HTML)
= # comment_only
= '#' + "#" # = 3 #
@ -97,7 +96,6 @@ describe Hamlit::Engine do
end
it 'renders &=' do
skip
assert_render(<<-HAML, <<-HTML.rstrip, escape_html: false)
&= '<"&>'
&= '<"&>'.tap do |str|
@ -109,7 +107,6 @@ describe Hamlit::Engine do
end
it 'regards ~ operator as =' do
skip
assert_render(<<-'HAML', <<-HTML)
~ "<code>hello\nworld</code>"
HAML

View File

@ -45,6 +45,7 @@ class Haml::TestCase < BASE_TEST_CLASS
extend Declarative
def render(text, options = {}, base = nil, &block)
options = { escape_html: false }.merge(options) # incompatible default
scope = options.delete(:scope) || Object.new
locals = options.delete(:locals) || {}
engine = Hamlit::HamlEngine.new(text, options)