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

Remove duplication around highlighting.

parent c881627d
No related merge requests found
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)
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
Gitlab::Highlight.highlight(blob_name, blob_content, nowrap: nowrap, continue: continue)
end
def no_highlight_files
......
......@@ -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)
......
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