diff --git a/lib/html2slim/nokogiri_monkeypatches.rb b/lib/html2slim/nokogiri_monkeypatches.rb index 5141b48..72ee115 100644 --- a/lib/html2slim/nokogiri_monkeypatches.rb +++ b/lib/html2slim/nokogiri_monkeypatches.rb @@ -136,3 +136,19 @@ def to_slim end end end + +class Nokogiri::XML::Comment + def to_slim(lvl = 0) + r = ' ' * lvl + + # Render as a Slim comment, multiline if necessary + str = content.strip + return nil if str.empty? + + if str.include?("\n") + "#{r}/!\n#{r} " + str.gsub("\n", "\n#{r} ") + else + "#{r}/! #{str}" + end + end +end diff --git a/test/fixtures/div_with_class_without_id.slim b/test/fixtures/div_with_class_without_id.slim index 38a0e8f..960dd8a 100644 --- a/test/fixtures/div_with_class_without_id.slim +++ b/test/fixtures/div_with_class_without_id.slim @@ -1 +1 @@ -div.test +.test diff --git a/test/fixtures/div_with_id_without_class.slim b/test/fixtures/div_with_id_without_class.slim index df82e81..9f4e8d7 100644 --- a/test/fixtures/div_with_id_without_class.slim +++ b/test/fixtures/div_with_id_without_class.slim @@ -1 +1 @@ -div#test +#test diff --git a/test/fixtures/html_comments.html b/test/fixtures/html_comments.html new file mode 100644 index 0000000..a2c0417 --- /dev/null +++ b/test/fixtures/html_comments.html @@ -0,0 +1,18 @@ + + + +

HTML comments

+ +

+ + + Comments +

\ No newline at end of file diff --git a/test/fixtures/html_comments.slim b/test/fixtures/html_comments.slim new file mode 100644 index 0000000..620e795 --- /dev/null +++ b/test/fixtures/html_comments.slim @@ -0,0 +1,17 @@ +doctype html +/! this is a single line comment +/! + This is a + multi-line + comment +html + body + h1 + | HTML comments + p + /! this is a single line comment + /! + This is a + multi-line + comment + | Comments diff --git a/test/test_html2slim.rb b/test/test_html2slim.rb index a2033ee..6d1c202 100644 --- a/test/test_html2slim.rb +++ b/test/test_html2slim.rb @@ -11,13 +11,23 @@ def teardown end Dir.glob("test/fixtures/*.html").each do |file| - define_method("test_template_#{File.basename(file, '.html')}") do + basename = File.basename(file, '.html') + + define_method("test_template_#{basename}") do assert_valid_from_html?(file) end + + define_method("test_convert_#{basename}") do + IO.popen("bin/html2slim #{file} -", "r") do |f| + assert_equal File.read("test/fixtures/#{basename}.slim"), f.read + end + end end - Dir.glob("test/fixtures/*.html.erb").each do |file| - define_method("test_template_#{File.basename(file, '.html')}") do + Dir.glob("test/fixtures/*.erb").each do |file| + basename = File.basename(file, '.erb') + + define_method("test_template_#{basename}") do assert_valid_from_erb?(file) end end