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]
return temple if node.children.empty?
temple << [:static, "\n"] if prepend_whitespace?(node)
temple << :whitespace if prepend_whitespace?(node)
node.children.each do |n|
rstrip_whitespace!(temple) if nuke_outer_whitespace?(n)
temple << yield(n)
temple << [:static, "\n"] if insert_whitespace?(n)
temple << :whitespace if insert_whitespace?(n)
end
temple
rstrip_whitespace!(temple) if nuke_inner_whitespace?(node)
confirm_whitespace(temple)
end
private
def prepend_whitespace?(node)
case node.type
when :tag
true
def confirm_whitespace(temple)
temple.map do |exp|
case exp
when :whitespace
[:static, "\n"]
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
def insert_whitespace?(node)
return false if nuke_outer_whitespace?(node)
case node.type
when :doctype
node.value[:type] != 'xml'

View file

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