2015-12-30 05:52:50 +00:00
|
|
|
module Gitlab
|
|
|
|
module Diff
|
|
|
|
class Highlight
|
2016-01-22 00:43:06 +00:00
|
|
|
attr_reader :diff_file, :diff_lines, :raw_lines
|
2016-01-08 03:37:01 +00:00
|
|
|
|
2016-01-14 21:38:37 +00:00
|
|
|
delegate :old_path, :new_path, :old_ref, :new_ref, to: :diff_file, prefix: :diff
|
2016-01-08 03:37:01 +00:00
|
|
|
|
2016-01-22 00:43:06 +00:00
|
|
|
def initialize(diff_lines)
|
|
|
|
if diff_lines.is_a?(Gitlab::Diff::File)
|
|
|
|
@diff_file = diff_file
|
|
|
|
@diff_lines = diff_file.diff_lines
|
|
|
|
else
|
|
|
|
@diff_lines = diff_lines
|
|
|
|
end
|
2016-01-14 21:28:07 +00:00
|
|
|
@raw_lines = @diff_lines.map(&:text)
|
2015-12-30 05:52:50 +00:00
|
|
|
end
|
|
|
|
|
2016-01-14 21:28:07 +00:00
|
|
|
def highlight
|
|
|
|
@diff_lines.each_with_index do |diff_line, i|
|
2015-12-31 02:44:12 +00:00
|
|
|
# ignore highlighting for "match" lines
|
2016-01-20 18:20:13 +00:00
|
|
|
next if diff_line.type == 'match' || diff_line.type == 'nonewline'
|
2016-01-14 21:28:07 +00:00
|
|
|
|
|
|
|
rich_line = highlight_line(diff_line, i)
|
2016-01-15 13:39:43 +00:00
|
|
|
|
|
|
|
if line_inline_diffs = inline_diffs[i]
|
|
|
|
rich_line = InlineDiffMarker.new(diff_line.text, rich_line).mark(line_inline_diffs)
|
|
|
|
end
|
|
|
|
|
2016-01-14 21:28:07 +00:00
|
|
|
diff_line.text = rich_line.html_safe
|
|
|
|
end
|
2016-01-15 13:39:43 +00:00
|
|
|
|
|
|
|
@diff_lines
|
2016-01-14 21:28:07 +00:00
|
|
|
end
|
|
|
|
|
2016-01-15 13:39:43 +00:00
|
|
|
private
|
|
|
|
|
2016-01-14 21:28:07 +00:00
|
|
|
def highlight_line(diff_line, index)
|
2016-01-22 00:43:06 +00:00
|
|
|
return html_escape(diff_line.text) unless diff_file && diff_file.diff_refs
|
2016-01-20 17:44:27 +00:00
|
|
|
|
2016-01-20 18:20:13 +00:00
|
|
|
line_prefix = diff_line.text.match(/\A(.)/) ? $1 : ' '
|
2016-01-14 21:28:07 +00:00
|
|
|
|
|
|
|
case diff_line.type
|
|
|
|
when 'new', nil
|
|
|
|
rich_line = new_lines[diff_line.new_pos - 1]
|
|
|
|
when 'old'
|
|
|
|
rich_line = old_lines[diff_line.old_pos - 1]
|
|
|
|
end
|
|
|
|
|
|
|
|
# Only update text if line is found. This will prevent
|
|
|
|
# issues with submodules given the line only exists in diff content.
|
2016-01-20 17:44:27 +00:00
|
|
|
rich_line ? line_prefix + rich_line : html_escape(diff_line.text)
|
2016-01-14 21:28:07 +00:00
|
|
|
end
|
|
|
|
|
2016-01-15 13:39:43 +00:00
|
|
|
def inline_diffs
|
|
|
|
@inline_diffs ||= InlineDiff.new(@raw_lines).inline_diffs
|
2015-12-30 05:52:50 +00:00
|
|
|
end
|
|
|
|
|
2016-01-08 03:37:01 +00:00
|
|
|
def old_lines
|
2016-01-22 00:43:06 +00:00
|
|
|
return unless diff_file
|
2016-01-15 13:11:36 +00:00
|
|
|
@old_lines ||= Gitlab::Highlight.highlight_lines(*processing_args(:old))
|
2016-01-08 03:37:01 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def new_lines
|
2016-01-22 00:43:06 +00:00
|
|
|
return unless diff_file
|
2016-01-15 13:11:36 +00:00
|
|
|
@new_lines ||= Gitlab::Highlight.highlight_lines(*processing_args(:new))
|
2016-01-14 21:28:07 +00:00
|
|
|
end
|
|
|
|
|
2016-01-14 21:38:37 +00:00
|
|
|
def processing_args(version)
|
|
|
|
ref = send("diff_#{version}_ref")
|
|
|
|
path = send("diff_#{version}_path")
|
|
|
|
|
|
|
|
[ref.project.repository, ref.id, path]
|
|
|
|
end
|
2016-01-20 17:44:27 +00:00
|
|
|
|
|
|
|
def html_escape(str)
|
|
|
|
replacements = { '&' => '&', '>' => '>', '<' => '<', '"' => '"', "'" => ''' }
|
|
|
|
str.gsub(/[&"'><]/, replacements)
|
|
|
|
end
|
2015-12-30 05:52:50 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|