Skip to content

Commit

Permalink
Fix conversion of multi-line method arguments
Browse files Browse the repository at this point in the history
The regexps responsible for converting method calls with blocks did not
account for arguments split across multiple lines.  This patch enables
multi-line method arguments, as long as every line except the last ends
with a comma (and zero or more spaces).
  • Loading branch information
jonathanhefner committed Jun 2, 2018
1 parent 3b0acf0 commit 0eb8d76
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/html2slim/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ def initialize(file)
erb.gsub!(/<%(.+?)\s*\{\s*(\|.+?\|)?\s*%>/){ %(<%#{$1} do #{$2}%>) }

# case, if, for, unless, until, while, and blocks...
erb.gsub!(/<%(-\s+)?((\s*(case|if|for|unless|until|while) .+?)|.+?do\s*(\|.+?\|)?\s*)-?%>/){ %(<ruby code="#{$2.gsub(/"/, '&quot;')}">) }
statement_start = /\s*(?:case|if|for|unless|until|while) .+?/
method_start = /.+?(?:,\s*\n.+?)*do\s*(?:\|.+?\|)?\s*/
erb.gsub!(/<%(?:-\s+)?(#{statement_start}|#{method_start})-?%>/){ %(<ruby code="#{$1.gsub(/"/, '&quot;')}">) }
# else
erb.gsub!(/<%-?\s*else\s*-?%>/, %(</ruby><ruby code="else">))
# elsif
Expand Down
2 changes: 1 addition & 1 deletion lib/html2slim/hpricot_monkeypatches.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def to_slim(lvl=0)
private

def slim_ruby_code(r)
(code.strip[0] == "=" ? "" : "- ") + code.strip.gsub(/\n/, "\n#{r}- ")
(code.strip[0] == "=" ? "" : "- ") + code.strip.gsub(/(?<!,)([ ]*\n)/, "\\1#{r}- ")
end

def code
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/multiline_method_args.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<%= form_with url: search_path,
class: "search-form",
method: :get do |form| %>
<div class="form-group">
<%= form.text_field :query,
class: "form-control" %>
</div>
<%= form.submit "Search",
class: "btn" %>
<% end %>
8 changes: 8 additions & 0 deletions test/fixtures/multiline_method_args.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
= form_with url: search_path,
class: "search-form",
method: :get do |form|
.form-group
= form.text_field :query,
class: "form-control"
= form.submit "Search",
class: "btn"
6 changes: 6 additions & 0 deletions test/test_html2slim.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ def test_convert_multiline_block
end
end

def test_convert_multiline_method_args
IO.popen("bin/erb2slim test/fixtures/multiline_method_args.erb -", "r") do |f|
assert_equal File.read("test/fixtures/multiline_method_args.slim"), f.read
end
end

def test_convert_elsif_block
IO.popen("bin/erb2slim test/fixtures/erb_elsif.erb -", "r") do |f|
assert_equal File.read("test/fixtures/erb_elsif.slim"), f.read
Expand Down

0 comments on commit 0eb8d76

Please sign in to comment.