Implement dynamic indentation

This commit is contained in:
Takashi Kokubun 2015-10-24 14:44:59 +09:00
parent 0b7677f1c8
commit 0e721ef075
6 changed files with 26 additions and 3 deletions

View File

@ -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),

View File

@ -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

View File

@ -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

View File

@ -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),

View File

@ -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

View File

@ -126,7 +126,7 @@ class EngineTest < Haml::TestCase
assert_equal "", render("")
end
def test_flexible_tabulation; skip # filter
def test_flexible_tabulation
assert_equal("<p>\n foo\n</p>\n<q>\n bar\n <a>\n baz\n </a>\n</q>\n",
render("%p\n foo\n%q\n bar\n %a\n baz"))
assert_equal("<p>\n foo\n</p>\n<q>\n bar\n <a>\n baz\n </a>\n</q>\n",