diff --git a/lib/hamlit/engine.rb b/lib/hamlit/engine.rb index feca1add..68de0c01 100644 --- a/lib/hamlit/engine.rb +++ b/lib/hamlit/engine.rb @@ -11,6 +11,7 @@ module Hamlit html_type: nil, attr_quote: "'", escape_html: true, + buffer: '_buf', autoclose: %w(area base basefont br col command embed frame hr img input isindex keygen link menuitem meta param source track wbr), diff --git a/lib/pretty_hamlit/compiler.rb b/lib/pretty_hamlit/compiler.rb index 2abcf597..c8b47264 100644 --- a/lib/pretty_hamlit/compiler.rb +++ b/lib/pretty_hamlit/compiler.rb @@ -25,6 +25,10 @@ module PrettyHamlit @indent_level -= 1 end + def compile_filter(node) + @filter_compiler.compile(node, @indent_level) + end + def compile_tag(node) @indent_level += 1 super diff --git a/lib/pretty_hamlit/dynamic_indentation.rb b/lib/pretty_hamlit/dynamic_indentation.rb new file mode 100644 index 00000000..900bd01a --- /dev/null +++ b/lib/pretty_hamlit/dynamic_indentation.rb @@ -0,0 +1,11 @@ +module PrettyHamlit + class DynamicIndentation + class << self + def indent_with(indent_level) + indent = "\n" << ' ' * indent_level + text = yield('') + text.gsub("\n", indent) + end + end + end +end diff --git a/lib/pretty_hamlit/engine.rb b/lib/pretty_hamlit/engine.rb index 782057e3..fa6fa08e 100644 --- a/lib/pretty_hamlit/engine.rb +++ b/lib/pretty_hamlit/engine.rb @@ -1,6 +1,7 @@ require 'temple' require 'hamlit/parser' require 'pretty_hamlit/compiler' +require 'pretty_hamlit/dynamic_indentation' module PrettyHamlit class Engine < Temple::Engine @@ -11,6 +12,7 @@ module PrettyHamlit attr_quote: "'", escape_html: true, pretty: true, + buffer: '_buf', autoclose: %w(area base basefont br col command embed frame hr img input isindex keygen link menuitem meta param source track wbr), diff --git a/lib/pretty_hamlit/filters.rb b/lib/pretty_hamlit/filters.rb index 88e69361..744c44d9 100644 --- a/lib/pretty_hamlit/filters.rb +++ b/lib/pretty_hamlit/filters.rb @@ -21,8 +21,13 @@ module PrettyHamlit @options = options end - def compile(node) - find_compiler(node.value[:name]).compile(node) + def compile(node, indent_level) + content = find_compiler(node.value[:name]).compile(node) + [:multi, + [:code, "#{@options[:buffer]} << ::PrettyHamlit::DynamicIndentation.indent_with(#{indent_level}) do |#{@options[:buffer]}|"], + content, + [:code, 'end'], + ] end private diff --git a/test/engine_test.rb b/test/engine_test.rb index 0fbe6eb6..1378ccca 100644 --- a/test/engine_test.rb +++ b/test/engine_test.rb @@ -126,7 +126,7 @@ class EngineTest < Haml::TestCase assert_equal "", render("") end - def test_flexible_tabulation; skip # filter + def test_flexible_tabulation assert_equal("

\n foo\n

\n\n bar\n \n baz\n \n\n", render("%p\n foo\n%q\n bar\n %a\n baz")) assert_equal("

\n foo\n

\n\n bar\n \n baz\n \n\n",