Commit 8b079315 authored by Rubén Dávila's avatar Rubén Dávila

A bit of refactoring. #3945

parent d8327562
...@@ -16,7 +16,7 @@ module Gitlab ...@@ -16,7 +16,7 @@ module Gitlab
end end
def highlighted_diff_lines def highlighted_diff_lines
Gitlab::Diff::Highlight.process_diff_lines(self) Gitlab::Diff::Highlight.process_diff_lines(new_path, diff_lines)
end end
def mode_changed? def mode_changed?
......
module Gitlab module Gitlab
module Diff module Diff
class Highlight class Highlight
def self.process_diff_lines(diff_file) def self.process_diff_lines(file_name, diff_lines)
processor = new(diff_file) processor = new(file_name, diff_lines)
processor.highlight processor.highlight
end end
def initialize(diff_file) def initialize(file_name, diff_lines)
text_lines = diff_file.diff_lines.map(&:text) text_lines = diff_lines.map(&:text)
@diff_file = diff_file @file_name = file_name
@diff_lines = diff_file.diff_lines @diff_lines = diff_lines
@diff_line_prefixes = text_lines.map { |line| line.sub!(/\A((\+|\-)\s*)/, '');$1 } @diff_line_prefixes = text_lines.map { |line| line.sub!(/\A((\+|\-)\s*)/, '');$1 }
@raw_lines = text_lines.join("\n") @raw_lines = text_lines.join("\n")
end end
...@@ -32,7 +32,7 @@ module Gitlab ...@@ -32,7 +32,7 @@ module Gitlab
end end
def lexer def lexer
parent = Rouge::Lexer.guess(filename: @diff_file.new_path, source: @code).new rescue Rouge::Lexers::PlainText.new parent = Rouge::Lexer.guess(filename: @file_name, source: @code).new rescue Rouge::Lexers::PlainText.new
Rouge::Lexers::GitlabDiff.new(parent_lexer: parent) Rouge::Lexers::GitlabDiff.new(parent_lexer: parent)
end end
...@@ -43,7 +43,7 @@ module Gitlab ...@@ -43,7 +43,7 @@ module Gitlab
end end
def formatter def formatter
@formatter ||= Rouge::Formatters::HTMLGitlab.new( Rouge::Formatters::HTMLGitlab.new(
nowrap: true, nowrap: true,
cssclass: 'code highlight', cssclass: 'code highlight',
lineanchors: true, lineanchors: true,
......
...@@ -2,6 +2,8 @@ Rouge::Token::Tokens.token(:InlineDiff, 'idiff') ...@@ -2,6 +2,8 @@ Rouge::Token::Tokens.token(:InlineDiff, 'idiff')
module Rouge module Rouge
module Lexers module Lexers
# This new Lexer is required in order to avoid the inline diff markup
# to be tokenized, it will be rendered as raw HTML code if that happens.
class GitlabDiff < RegexLexer class GitlabDiff < RegexLexer
title "GitLab Diff" title "GitLab Diff"
tag 'gitlab_diff' tag 'gitlab_diff'
......
...@@ -9,7 +9,7 @@ describe Gitlab::Diff::Highlight, lib: true do ...@@ -9,7 +9,7 @@ describe Gitlab::Diff::Highlight, lib: true do
let(:diff_file) { Gitlab::Diff::File.new(diff) } let(:diff_file) { Gitlab::Diff::File.new(diff) }
describe '.process_diff_lines' do describe '.process_diff_lines' do
let(:diff_lines) { Gitlab::Diff::Highlight.process_diff_lines(diff_file) } let(:diff_lines) { Gitlab::Diff::Highlight.process_diff_lines(diff_file.new_path, diff_file.diff_lines) }
it 'should return Gitlab::Diff::Line elements' do it 'should return Gitlab::Diff::Line elements' do
expect(diff_lines.first).to be_an_instance_of(Gitlab::Diff::Line) expect(diff_lines.first).to be_an_instance_of(Gitlab::Diff::Line)
......
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