Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions lib/html2slim/nokogiri_monkeypatches.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion test/fixtures/div_with_class_without_id.slim
Original file line number Diff line number Diff line change
@@ -1 +1 @@
div.test
.test
2 changes: 1 addition & 1 deletion test/fixtures/div_with_id_without_class.slim
Original file line number Diff line number Diff line change
@@ -1 +1 @@
div#test
#test
18 changes: 18 additions & 0 deletions test/fixtures/html_comments.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!doctype html>
<!-- this is a single line comment -->
<!--
This is a
multi-line
comment
-->
<h1>HTML comments</h1>

<p>
<!-- this is a single line comment -->
<!--
This is a
multi-line
comment
-->
Comments
</p>
17 changes: 17 additions & 0 deletions test/fixtures/html_comments.slim
Original file line number Diff line number Diff line change
@@ -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
16 changes: 13 additions & 3 deletions test/test_html2slim.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down