2015-12-30 00:52:50 -05:00
|
|
|
module Gitlab
|
|
|
|
module Diff
|
|
|
|
class Highlight
|
2016-01-07 22:37:01 -05:00
|
|
|
attr_reader :diff_file
|
|
|
|
|
|
|
|
delegate :repository, :old_path, :new_path, :old_ref, :new_ref,
|
|
|
|
to: :diff_file, prefix: :diff
|
|
|
|
|
2015-12-31 01:05:52 -05:00
|
|
|
# Apply syntax highlight to provided source code
|
|
|
|
#
|
2016-01-07 22:37:01 -05:00
|
|
|
# diff_file - an instance of Gitlab::Diff::File
|
2015-12-31 01:05:52 -05:00
|
|
|
#
|
|
|
|
# Returns an Array with the processed items.
|
2016-01-07 22:37:01 -05:00
|
|
|
def self.process_diff_lines(diff_file)
|
|
|
|
processor = new(diff_file)
|
2015-12-30 00:52:50 -05:00
|
|
|
processor.highlight
|
|
|
|
end
|
|
|
|
|
2016-01-07 22:37:01 -05:00
|
|
|
def self.process_file(repository, ref, file_name)
|
|
|
|
blob = repository.blob_at(ref, file_name)
|
|
|
|
return [] unless blob
|
|
|
|
|
|
|
|
content = blob.data
|
|
|
|
lexer = Rouge::Lexer.guess(filename: file_name, source: content).new rescue Rouge::Lexers::PlainText.new
|
|
|
|
formatter.format(lexer.lex(content)).lines
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.formatter
|
|
|
|
@formatter ||= Rouge::Formatters::HTMLGitlab.new(
|
2016-01-08 19:05:55 -05:00
|
|
|
nowrap: true,
|
|
|
|
cssclass: 'code highlight',
|
|
|
|
lineanchors: true,
|
|
|
|
lineanchorsid: 'LC'
|
|
|
|
)
|
2016-01-07 22:37:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(diff_file)
|
|
|
|
@diff_file = diff_file
|
|
|
|
@file_name = diff_file.new_path
|
|
|
|
@lines = diff_file.diff_lines
|
2015-12-30 00:52:50 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def highlight
|
2015-12-31 01:05:52 -05:00
|
|
|
return [] if @lines.empty?
|
|
|
|
|
|
|
|
extract_line_prefixes
|
|
|
|
|
|
|
|
@code = unescape_html(raw_content)
|
2015-12-30 00:52:50 -05:00
|
|
|
@highlighted_code = formatter.format(lexer.lex(@code))
|
|
|
|
|
2015-12-31 01:05:52 -05:00
|
|
|
is_diff_line? ? update_diff_lines : @highlighted_code.lines
|
2015-12-30 00:52:50 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2015-12-31 01:05:52 -05:00
|
|
|
def is_diff_line?
|
|
|
|
@lines.first.is_a?(Gitlab::Diff::Line)
|
|
|
|
end
|
|
|
|
|
|
|
|
def text_lines
|
|
|
|
@text_lines ||= (is_diff_line? ? @lines.map(&:text) : @lines)
|
|
|
|
end
|
|
|
|
|
|
|
|
def raw_content
|
|
|
|
@raw_content ||= text_lines.join(is_diff_line? ? "\n" : nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
def extract_line_prefixes
|
|
|
|
@diff_line_prefixes ||= begin
|
|
|
|
if is_diff_line?
|
2016-01-07 22:37:01 -05:00
|
|
|
text_lines.map { |line| line.sub!(/\A((\+|\-))/, '');$1 }
|
2015-12-31 01:05:52 -05:00
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-12-30 00:52:50 -05:00
|
|
|
def update_diff_lines
|
|
|
|
@highlighted_code.lines.each_with_index do |line, i|
|
2015-12-31 01:05:52 -05:00
|
|
|
diff_line = @lines[i]
|
2016-01-07 22:37:01 -05:00
|
|
|
line_prefix = @diff_line_prefixes[i] || ' '
|
2015-12-30 21:44:12 -05:00
|
|
|
|
|
|
|
# ignore highlighting for "match" lines
|
|
|
|
next if diff_line.type == 'match'
|
|
|
|
|
2016-01-07 22:37:01 -05:00
|
|
|
case diff_line.type
|
|
|
|
when 'new', nil
|
|
|
|
diff_line.text = new_lines[diff_line.new_pos - 1].try(:gsub!, /\A\s/, line_prefix)
|
|
|
|
when 'old'
|
|
|
|
diff_line.text = old_lines[diff_line.old_pos - 1].try(:gsub!, /\A\s/, line_prefix)
|
|
|
|
end
|
2015-12-30 00:52:50 -05:00
|
|
|
end
|
|
|
|
|
2015-12-31 01:05:52 -05:00
|
|
|
@lines
|
2015-12-30 00:52:50 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def lexer
|
2016-01-08 15:17:45 -05:00
|
|
|
Rouge::Lexer.guess(filename: @file_name, source: @code).new rescue Rouge::Lexers::PlainText.new
|
2015-12-30 00:52:50 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def unescape_html(content)
|
|
|
|
text = CGI.unescapeHTML(content)
|
|
|
|
text.gsub!(' ', ' ')
|
|
|
|
text
|
|
|
|
end
|
|
|
|
|
|
|
|
def formatter
|
2016-01-07 22:37:01 -05:00
|
|
|
self.class.formatter
|
|
|
|
end
|
|
|
|
|
|
|
|
def old_lines
|
|
|
|
@old_lines ||= begin
|
|
|
|
lines = self.class.process_file(diff_repository, diff_old_ref, diff_old_path)
|
|
|
|
lines.map! { |line| " #{line}" }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def new_lines
|
|
|
|
@new_lines ||= begin
|
|
|
|
lines = self.class.process_file(diff_repository, diff_new_ref, diff_new_path)
|
|
|
|
lines.map! { |line| " #{line}" }
|
|
|
|
end
|
2015-12-30 00:52:50 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|