From 1fe3a2f81ad0896ff488ae689c884588f5fdbd61 Mon Sep 17 00:00:00 2001 From: Takashi Kokubun Date: Sun, 25 Oct 2015 16:14:55 +0900 Subject: [PATCH] Support preserve --- lib/hamlit/compiler/script_compiler.rb | 14 ++++++++------ lib/hamlit/parser.rb | 11 ++++++----- test/haml/haml-spec/Rakefile | 2 +- test/haml/haml-spec/pretty_test.rb | 2 +- test/haml/haml-spec/ugly_test.rb | 2 +- test/hamlit/engine/script_test.rb | 3 --- test/test_helper.rb | 1 + 7 files changed, 18 insertions(+), 17 deletions(-) diff --git a/lib/hamlit/compiler/script_compiler.rb b/lib/hamlit/compiler/script_compiler.rb index c7e93778..4eeaeab6 100644 --- a/lib/hamlit/compiler/script_compiler.rb +++ b/lib/hamlit/compiler/script_compiler.rb @@ -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 diff --git a/lib/hamlit/parser.rb b/lib/hamlit/parser.rb index 327d54df..a429e242 100644 --- a/lib/hamlit/parser.rb +++ b/lib/hamlit/parser.rb @@ -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 diff --git a/test/haml/haml-spec/Rakefile b/test/haml/haml-spec/Rakefile index 291fd34c..da45757c 100644 --- a/test/haml/haml-spec/Rakefile +++ b/test/haml/haml-spec/Rakefile @@ -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) diff --git a/test/haml/haml-spec/pretty_test.rb b/test/haml/haml-spec/pretty_test.rb index ad86fb31..647561bb 100644 --- a/test/haml/haml-spec/pretty_test.rb +++ b/test/haml/haml-spec/pretty_test.rb @@ -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) diff --git a/test/haml/haml-spec/ugly_test.rb b/test/haml/haml-spec/ugly_test.rb index 1ee7dbe9..23379dcd 100644 --- a/test/haml/haml-spec/ugly_test.rb +++ b/test/haml/haml-spec/ugly_test.rb @@ -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) diff --git a/test/hamlit/engine/script_test.rb b/test/hamlit/engine/script_test.rb index f4f798b6..188d5187 100644 --- a/test/hamlit/engine/script_test.rb +++ b/test/hamlit/engine/script_test.rb @@ -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) ~ "hello\nworld" HAML diff --git a/test/test_helper.rb b/test/test_helper.rb index 4bfcd784..cd464907 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -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)