Remove duplication around highlighting.
This commit is contained in:
parent
c881627d11
commit
3a1d053599
|
|
@ -1,20 +1,6 @@
|
|||
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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
Loading…
Reference in New Issue