diff options
author | Douwe Maan <douwe@gitlab.com> | 2016-01-14 15:11:50 +0100 |
---|---|---|
committer | Douwe Maan <douwe@gitlab.com> | 2016-01-14 15:11:50 +0100 |
commit | 3a1d0535992594bc77320f081d1f20b760b1c1f7 (patch) | |
tree | 01648b2b11bf9a3cdc012bda4da62eaf075650fe /lib/gitlab | |
parent | c881627d114eb9c050d605e93673ef65a9da9a58 (diff) | |
download | gitlab-ce-3a1d0535992594bc77320f081d1f20b760b1c1f7.tar.gz |
Remove duplication around highlighting.
Diffstat (limited to 'lib/gitlab')
-rw-r--r-- | lib/gitlab/diff/highlight.rb | 13 | ||||
-rw-r--r-- | lib/gitlab/highlight.rb | 23 |
2 files changed, 24 insertions, 12 deletions
diff --git a/lib/gitlab/diff/highlight.rb b/lib/gitlab/diff/highlight.rb index e76a6f27856..f34eff62d79 100644 --- a/lib/gitlab/diff/highlight.rb +++ b/lib/gitlab/diff/highlight.rb @@ -20,18 +20,7 @@ module Gitlab 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.map!(&:html_safe) - end - - def self.formatter - @formatter ||= Rouge::Formatters::HTMLGitlab.new( - nowrap: true, - cssclass: 'code highlight', - lineanchors: true, - lineanchorsid: 'LC' - ) + Gitlab::Highlight.highlight(file_name, blob.data).lines.map!(&:html_safe) end def initialize(diff_file) diff --git a/lib/gitlab/highlight.rb b/lib/gitlab/highlight.rb new file mode 100644 index 00000000000..02e097eca3d --- /dev/null +++ b/lib/gitlab/highlight.rb @@ -0,0 +1,23 @@ +module Gitlab + class Highlight + def self.highlight(blob_name, blob_content, nowrap: true, continue: false) + formatter = rouge_formatter(nowrap: nowrap) + + lexer = Rouge::Lexer.guess(filename: blob_name, source: blob_content).new rescue Rouge::Lexers::PlainText + formatter.format(lexer.lex(blob_content, continue: continue)).html_safe + end + + private + + def self.rouge_formatter(options = {}) + options = options.reverse_merge( + nowrap: true, + cssclass: 'code highlight', + lineanchors: true, + lineanchorsid: 'LC' + ) + + Rouge::Formatters::HTMLGitlab.new(options) + end + end +end |