summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDouwe Maan <douwe@gitlab.com>2015-08-10 15:55:44 +0000
committerDouwe Maan <douwe@gitlab.com>2015-08-10 15:55:44 +0000
commit6e7bf50ba6d0d9eb896c1abef8dda8a4997c3088 (patch)
tree86471a94af7ad743e4c540c14b53a96cda21c38a /lib
parent39bda5d05e96ff9b2b8a89852b9520e8f262051e (diff)
parent15d5dfdb33c94ccfd7843f2be35bac2d74abe03e (diff)
downloadgitlab-ce-6e7bf50ba6d0d9eb896c1abef8dda8a4997c3088.tar.gz
Merge branch 'fix-diff-highlighting' into 'master'
Fix diff syntax highlighting ### What does this MR do? This MR fixes a syntax highlighting issue that that manifested itself when a user tried the following: ``` +aaa +bbb ccc ``` However, a regression in 0abe98f0 caused the last line to be put on the second to last line: ![image](https://gitlab.com/stanhu/gitlab-ce/uploads/a16515499d5e3fd9737370d89c73f671/image.png) Instead of this: ![image](https://gitlab.com/gitlab-org/gitlab-ce/uploads/59eca6efd4c290aa633b9fb54c98a70e/image.png) This bug occurred because of some error in the logic of handling new lines in the content. Refactored HTML parser to avoid duplication of newline parsing. ### What are the relevant issue numbers? Closes #2235 See merge request !1126
Diffstat (limited to 'lib')
-rw-r--r--lib/rouge/formatters/html_gitlab.rb41
1 files changed, 21 insertions, 20 deletions
diff --git a/lib/rouge/formatters/html_gitlab.rb b/lib/rouge/formatters/html_gitlab.rb
index 3f92212243d..6762ca47c32 100644
--- a/lib/rouge/formatters/html_gitlab.rb
+++ b/lib/rouge/formatters/html_gitlab.rb
@@ -93,16 +93,27 @@ module Rouge
end
def process_tokens(tokens)
- num_lines = 0
- last_val = ''
- rendered = ''
+ rendered = []
+ current_line = ''
tokens.each do |tok, val|
- last_val = val
- num_lines += val.scan(/\n/).size
- rendered << span(tok, val)
+ # In the case of multi-line values (e.g. comments), we need to apply
+ # styling to each line since span elements are inline.
+ val.lines.each do |line|
+ stripped = line.chomp
+ current_line << span(tok, stripped)
+
+ if line.end_with?("\n")
+ rendered << current_line
+ current_line = ''
+ end
+ end
end
+ # Add leftover text
+ rendered << current_line if current_line.present?
+
+ num_lines = rendered.size
numbers = (@linenostart..num_lines + @linenostart - 1).to_a
{ numbers: numbers, code: rendered }
@@ -117,9 +128,8 @@ module Rouge
numbers.join("\n")
end
- def wrap_lines(rendered)
+ def wrap_lines(lines)
if @lineanchors
- lines = rendered.split("\n")
lines = lines.each_with_index.map do |line, index|
number = index + @linenostart
@@ -136,24 +146,17 @@ module Rouge
lines.join("\n")
else
if @linenos == 'inline'
- lines = rendered.split("\n")
lines = lines.each_with_index.map do |line, index|
number = index + @linenostart
"<span class=\"linenos\">#{number}</span>#{line}"
end
lines.join("\n")
else
- rendered
+ lines.join("\n")
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)
# http://stackoverflow.com/a/1600584/2587286
val = CGI.escapeHTML(val)
@@ -161,13 +164,11 @@ module Rouge
if tok.shortname.empty?
val
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
rules = @inline_theme.style_for(tok).rendered_rules
- wrap_values(val, "style=\"#{rules.to_a.join(';')}\"")
+ "<span style=\"#{rules.to_a.join(';')}\"#{val}</span>"
else
- wrap_values(val, "class=\"#{tok.shortname}\"")
+ "<span class=\"#{tok.shortname}\">#{val}</span>"
end
end
end