mirror of
https://github.com/haml/haml.git
synced 2022-11-09 12:33:31 -05:00
Implement whitespace
This commit is contained in:
parent
1cc916909a
commit
5f3064e5f0
2 changed files with 77 additions and 50 deletions
|
@ -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 confirm_whitespace(temple)
|
||||
temple.map do |exp|
|
||||
case exp
|
||||
when :whitespace
|
||||
[:static, "\n"]
|
||||
else
|
||||
exp
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def prepend_whitespace?(node)
|
||||
case node.type
|
||||
when :tag
|
||||
true
|
||||
else
|
||||
false
|
||||
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'
|
||||
|
|
|
@ -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
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>
|
||||
|
|
Loading…
Add table
Reference in a new issue