Skip to content

Commit

Permalink
Fix multiline Ruby code in Slim lexer (#1130)
Browse files Browse the repository at this point in the history
This change fixes the lexing of multiline strings using `\` at the end
of the line.
  • Loading branch information
reneklacan authored and pyrmont committed May 28, 2019
1 parent fddb314 commit 23755bc
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/rouge/lexers/slim.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@ def filters
# Need at top
mixin :indented_block

rule(/,\s*\n/) { delegate ruby }
rule(/[,\\]\s*\n/) { delegate ruby }
rule /[ ]\|[ \t]*\n/, Str::Escape
rule(/.*?(?=(,$| \|)?[ \t]*$)/) { delegate ruby }
rule(/.*?(?=([,\\]$| \|)?[ \t]*$)/) { delegate ruby }
end

state :filter_block do
Expand Down
24 changes: 24 additions & 0 deletions spec/lexers/slim_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,28 @@
end
end

describe 'multi line ruby code' do
it 'handles comma at the end of the line' do
assert_tokens_equal "= puts 1,\n2",
['Punctuation', '='],
['Text', ' '],
['Name.Builtin', 'puts'],
['Text', ' '],
['Literal.Number.Integer', '1'],
['Punctuation', ","],
['Text', "\n"],
['Literal.Number.Integer', '2']
end

it 'handles backslash at the end of the line' do
assert_tokens_equal "= puts \\\n1",
['Punctuation', '='],
['Text', ' '],
['Name.Builtin', 'puts'],
['Text', ' '],
['Punctuation', "\\"],
['Text', "\n"],
['Literal.Number.Integer', '1']
end
end
end
10 changes: 9 additions & 1 deletion spec/visual/samples/slim
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ html

/ Inline nested ruby expressions
p Nested interp #{list.map { |x| x + 1 }.inject(&:+)} parses ok

/! HTML comment
#content
p This example shows you how a basic Slim file looks like.
Expand Down Expand Up @@ -61,6 +61,14 @@ html
tr
td.name = item.name
td.price = item.price
td.spec
= render( \
partial: 'item_spec',
locals: { \
item: item,
},
)

- else
p<
| No items found. Please add some inventory.
Expand Down

0 comments on commit 23755bc

Please sign in to comment.