diff --git a/lib/hamlit/compiler.rb b/lib/hamlit/compiler.rb index a7789c54..c9ff0126 100644 --- a/lib/hamlit/compiler.rb +++ b/lib/hamlit/compiler.rb @@ -20,6 +20,8 @@ module Hamlit case node.type when :root compile_children(node) + when :comment + compile_comment(node) when :doctype compile_doctype(node) when :tag @@ -39,6 +41,13 @@ module Hamlit @doctype_compiler.compile(node) end + def compile_comment(node) + if node.children.empty? + return [:html, :comment, [:static, " #{node.value[:text]} "]] + end + [:html, :comment, compile_children(node)] + end + def compile_tag(node) @tag_compiler.compile(node) { |n| compile_children(n) } end diff --git a/lib/hamlit/whitespace_handler.rb b/lib/hamlit/whitespace_handler.rb index 20278aa9..7a765ca4 100644 --- a/lib/hamlit/whitespace_handler.rb +++ b/lib/hamlit/whitespace_handler.rb @@ -28,7 +28,7 @@ module Hamlit end def prepend_whitespace?(node) - return false if node.type != :tag + return false unless %i[comment tag].include?(node.type) !nuke_inner_whitespace?(node) end @@ -54,7 +54,7 @@ module Hamlit case node.type when :doctype node.value[:type] != 'xml' - when :plain, :tag + when :plain, :tag, :comment true else false diff --git a/test/haml-spec/tests.yml b/test/haml-spec/tests.yml index a1c3192a..20020fef 100644 --- a/test/haml-spec/tests.yml +++ b/test/haml-spec/tests.yml @@ -335,20 +335,20 @@ silent comments: %div foo html: '' -# markup comments: -# an inline markup comment: -# haml: "/ comment" -# html: "" -# a nested markup comment: -# haml: |- -# / -# comment -# comment2 -# html: |- -# +markup comments: + an inline markup comment: + haml: "/ comment" + html: "" + a nested markup comment: + haml: |- + / + comment + comment2 + html: |- + # conditional comments: # a conditional comment: # haml: |-