Commit 0abe98f0 authored by Stan Hu's avatar Stan Hu

Fix multi-line syntax highlighting

HTML span elements only apply to a single line, and in the case of multi-line
comments, the highlighting was dropped as a result. Ensure that each line has
the proper styling to fix this.

Closes #1577
parent a51a3fb8
Please view this file on the master branch, on stable branches it's out of date. Please view this file on the master branch, on stable branches it's out of date.
v 7.14.0 (unreleased) v 7.14.0 (unreleased)
- Fix multi-line syntax highlighting (Stan Hu)
- Fix network graph when branch name has single quotes (Stan Hu) - Fix network graph when branch name has single quotes (Stan Hu)
- Upgrade gitlab_git to version 7.2.6 to fix Error 500 when creating network graphs (Stan Hu) - Upgrade gitlab_git to version 7.2.6 to fix Error 500 when creating network graphs (Stan Hu)
- Add support for Unicode filenames in relative links (Hiroyuki Sato) - Add support for Unicode filenames in relative links (Hiroyuki Sato)
......
...@@ -148,6 +148,12 @@ module Rouge ...@@ -148,6 +148,12 @@ module Rouge
end end
end end
def wrap_values(val, element)
lines = val.split("\n")
lines = lines.map{ |x| "<span #{element}>#{x}</span>" }
lines.join("\n")
end
def span(tok, val) def span(tok, val)
# http://stackoverflow.com/a/1600584/2587286 # http://stackoverflow.com/a/1600584/2587286
val = CGI.escapeHTML(val) val = CGI.escapeHTML(val)
...@@ -155,11 +161,13 @@ module Rouge ...@@ -155,11 +161,13 @@ module Rouge
if tok.shortname.empty? if tok.shortname.empty?
val val
else else
# In the case of multi-line values (e.g. comments), we need to apply
# styling to each line since span elements are inline.
if @inline_theme if @inline_theme
rules = @inline_theme.style_for(tok).rendered_rules rules = @inline_theme.style_for(tok).rendered_rules
"<span style=\"#{rules.to_a.join(';')}\">#{val}</span>" wrap_values(val, "style=\"#{rules.to_a.join(';')}\"")
else else
"<span class=\"#{tok.shortname}\">#{val}</span>" wrap_values(val, "class=\"#{tok.shortname}\"")
end end
end end
end end
......
...@@ -6,6 +6,14 @@ describe BlobHelper do ...@@ -6,6 +6,14 @@ describe BlobHelper do
let(:no_context_content) { ":type \"assem\"))" } let(:no_context_content) { ":type \"assem\"))" }
let(:blob_content) { "(make-pathname :defaults name\n#{no_context_content}" } let(:blob_content) { "(make-pathname :defaults name\n#{no_context_content}" }
let(:split_content) { blob_content.split("\n") } let(:split_content) { blob_content.split("\n") }
let(:multiline_content) do
%q(
def test(input):
"""This is line 1 of a multi-line comment.
This is line 2.
"""
)
end
it 'should return plaintext for unknown lexer context' do it 'should return plaintext for unknown lexer context' do
result = highlight(blob_name, no_context_content, nowrap: true, continue: false) result = highlight(blob_name, no_context_content, nowrap: true, continue: false)
...@@ -29,5 +37,15 @@ describe BlobHelper do ...@@ -29,5 +37,15 @@ describe BlobHelper do
result = split_content.map{ |content| highlight(blob_name, content, nowrap: true, continue: true) } result = split_content.map{ |content| highlight(blob_name, content, nowrap: true, continue: true) }
expect(result).to eq(expected) expect(result).to eq(expected)
end end
it 'should highlight multi-line comments' do
result = highlight(blob_name, multiline_content, nowrap: true, continue: false)
html = Nokogiri::HTML(result)
lines = html.search('.s')
expect(lines.count).to eq(3)
expect(lines[0].text).to eq('"""This is line 1 of a multi-line comment.')
expect(lines[1].text).to eq(' This is line 2.')
expect(lines[2].text).to eq(' """')
end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment