1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00

Implement whitespace

This commit is contained in:
Takashi Kokubun 2015-10-12 00:16:37 +09:00
parent 1cc916909a
commit 5f3064e5f0
2 changed files with 77 additions and 50 deletions

View file

@ -4,26 +4,53 @@ module Hamlit
temple = [:multi] temple = [:multi]
return temple if node.children.empty? return temple if node.children.empty?
temple << [:static, "\n"] if prepend_whitespace?(node) temple << :whitespace if prepend_whitespace?(node)
node.children.each do |n| node.children.each do |n|
rstrip_whitespace!(temple) if nuke_outer_whitespace?(n)
temple << yield(n) temple << yield(n)
temple << [:static, "\n"] if insert_whitespace?(n) temple << :whitespace if insert_whitespace?(n)
end end
temple rstrip_whitespace!(temple) if nuke_inner_whitespace?(node)
confirm_whitespace(temple)
end end
private private
def prepend_whitespace?(node) def confirm_whitespace(temple)
case node.type temple.map do |exp|
when :tag case exp
true when :whitespace
[:static, "\n"]
else else
false exp
end
end
end
def prepend_whitespace?(node)
return false if node.type != :tag
!nuke_inner_whitespace?(node)
end
def nuke_inner_whitespace?(node)
return false if node.type != :tag
node.value[:nuke_inner_whitespace]
end
def nuke_outer_whitespace?(node)
return false if node.type != :tag
node.value[:nuke_outer_whitespace]
end
def rstrip_whitespace!(temple)
if temple[-1] == :whitespace
temple.delete_at(-1)
end end
end end
def insert_whitespace?(node) def insert_whitespace?(node)
return false if nuke_outer_whitespace?(node)
case node.type case node.type
when :doctype when :doctype
node.value[:type] != 'xml' node.value[:type] != 'xml'

View file

@ -488,51 +488,51 @@ boolean attributes:
html: "<input checked>" html: "<input checked>"
config: config:
format: html5 format: html5
# whitespace preservation: whitespace preservation:
# following the '~' operator: # following the '~' operator:
# haml: ~ "Foo\n<pre>Bar\nBaz</pre>" # haml: ~ "Foo\n<pre>Bar\nBaz</pre>"
# html: |- # html: |-
# Foo # Foo
# <pre>Bar&#x000A;Baz</pre> # <pre>Bar&#x000A;Baz</pre>
# optional: true # optional: true
# inside a textarea tag: inside a textarea tag:
# haml: |- haml: |-
# %textarea %textarea
# hello hello
# hello hello
# html: |- html: |-
# <textarea>hello <textarea>hello
# hello</textarea> hello</textarea>
# inside a pre tag: inside a pre tag:
# haml: |- haml: |-
# %pre %pre
# hello hello
# hello hello
# html: |- html: |-
# <pre>hello <pre>hello
# hello</pre> hello</pre>
# whitespace removal: whitespace removal:
# a tag with '>' appended and inline content: a tag with '>' appended and inline content:
# haml: |- haml: |-
# %li hello %li hello
# %li> world %li> world
# %li again %li again
# html: "<li>hello</li><li>world</li><li>again</li>" html: "<li>hello</li><li>world</li><li>again</li>"
# a tag with '>' appended and nested content: a tag with '>' appended and nested content:
# haml: |- haml: |-
# %li hello %li hello
# %li> %li>
# world world
# %li again %li again
# html: |- html: |-
# <li>hello</li><li> <li>hello</li><li>
# world world
# </li><li>again</li> </li><li>again</li>
# a tag with '<' appended: a tag with '<' appended:
# haml: |- haml: |-
# %p< %p<
# hello hello
# world world
# html: |- html: |-
# <p>hello <p>hello
# world</p> world</p>