Commit 3a1d0535 authored by Douwe Maan's avatar Douwe Maan

Remove duplication around highlighting.

parent c881627d
module BlobHelper module BlobHelper
def rouge_formatter(options = {})
default_options = {
nowrap: false,
cssclass: 'code highlight',
lineanchors: true,
lineanchorsid: 'LC'
}
Rouge::Formatters::HTMLGitlab.new(default_options.merge!(options))
end
def highlight(blob_name, blob_content, nowrap: false, continue: false) def highlight(blob_name, blob_content, nowrap: false, continue: false)
formatter = rouge_formatter(nowrap: nowrap) Gitlab::Highlight.highlight(blob_name, blob_content, nowrap: nowrap, continue: continue)
@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 end
def no_highlight_files def no_highlight_files
......
...@@ -20,18 +20,7 @@ module Gitlab ...@@ -20,18 +20,7 @@ module Gitlab
blob = repository.blob_at(ref, file_name) blob = repository.blob_at(ref, file_name)
return [] unless blob return [] unless blob
content = blob.data Gitlab::Highlight.highlight(file_name, blob.data).lines.map!(&:html_safe)
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'
)
end end
def initialize(diff_file) def initialize(diff_file)
......
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
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