1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00
haml--haml/lib/hamlit/whitespace/indented_compiler.rb
2015-10-24 20:32:25 +09:00

57 lines
1.5 KiB
Ruby

require 'hamlit/whitespace/compiler'
module Hamlit
module Whitespace
class IndentedCompiler < Compiler
def compile_children(node, indent_level, &block)
temple = [:multi]
return temple if node.children.empty?
temple << :whitespace if prepend_whitespace?(node)
node.children.each do |n|
rstrip_whitespace!(temple) if nuke_outer_whitespace?(n)
temple << yield(n)
if insert_whitespace?(n)
if nuke_inner_whitespace?(node)
temple << :weak_whitespace
else
temple << :whitespace
end
end
end
rstrip_whitespace!(temple) if nuke_inner_whitespace?(node)
weaken_last_whitespace!(temple)
confirm_whitespace(temple, indent_level)
end
private
def rstrip_whitespace!(temple)
if %i[whitespace weak_whitespace].include?(temple[-1])
temple.delete_at(-1)
end
end
def weaken_last_whitespace!(temple)
if temple[-1] == :whitespace
temple.delete_at(-1)
temple << :weak_whitespace
end
end
def confirm_whitespace(temple, indent_level)
temple.map do |exp|
case exp
when :whitespace
[:static, "\n" + (' ' * indent_level)]
when :weak_whitespace
level = [0, indent_level - 1].max
[:static, "\n" + (' ' * level)]
else
exp
end
end
end
end
end
end