debian-mirror-gitlab/lib/gitlab/highlight.rb

45 lines
1.3 KiB
Ruby
Raw Normal View History

2016-01-29 22:53:50 +05:30
module Gitlab
class Highlight
2016-06-02 11:05:42 +05:30
def self.highlight(blob_name, blob_content, nowrap: true, plain: false)
new(blob_name, blob_content, nowrap: nowrap).
highlight(blob_content, continue: false, plain: plain)
2016-01-29 22:53:50 +05:30
end
def self.highlight_lines(repository, ref, file_name)
blob = repository.blob_at(ref, file_name)
return [] unless blob
2016-04-02 18:10:28 +05:30
blob.load_all_data!(repository)
2016-01-29 22:53:50 +05:30
highlight(file_name, blob.data).lines.map!(&:html_safe)
end
def initialize(blob_name, blob_content, nowrap: true)
@formatter = rouge_formatter(nowrap: nowrap)
@lexer = Rouge::Lexer.guess(filename: blob_name, source: blob_content).new rescue Rouge::Lexers::PlainText
end
2016-06-02 11:05:42 +05:30
def highlight(text, continue: true, plain: false)
if plain
@formatter.format(Rouge::Lexers::PlainText.lex(text)).html_safe
else
@formatter.format(@lexer.lex(text, continue: continue)).html_safe
end
2016-01-29 22:53:50 +05:30
rescue
@formatter.format(Rouge::Lexers::PlainText.lex(text)).html_safe
end
private
def rouge_formatter(options = {})
options = options.reverse_merge(
nowrap: true,
cssclass: 'code highlight',
lineanchors: true,
lineanchorsid: 'LC'
)
Rouge::Formatters::HTMLGitlab.new(options)
end
end
end