gitlab-org--gitlab-foss/lib/gitlab/highlight.rb

64 lines
1.8 KiB
Ruby
Raw Normal View History

module Gitlab
class Highlight
def self.highlight(blob_name, blob_content, repository: nil, plain: false)
2017-06-21 13:48:12 +00:00
new(blob_name, blob_content, repository: repository)
.highlight(blob_content, continue: false, plain: plain)
end
2016-07-10 21:13:06 +00:00
attr_reader :blob_name
def initialize(blob_name, blob_content, repository: nil)
@formatter = Rouge::Formatters::HTMLGitlab
@repository = repository
@blob_name = blob_name
@blob_content = blob_content
end
2016-04-14 10:44:35 +00:00
def highlight(text, continue: true, plain: false)
2016-07-10 21:13:06 +00:00
highlighted_text = highlight_text(text, continue: continue, plain: plain)
highlighted_text = link_dependencies(text, highlighted_text) if blob_name
highlighted_text
end
def lexer
@lexer ||= custom_language || begin
2016-07-14 19:21:22 +00:00
Rouge::Lexer.guess(filename: @blob_name, source: @blob_content).new
rescue Rouge::Guesser::Ambiguous => e
e.alternatives.sort_by(&:tag).first
end
end
private
2016-06-21 16:23:42 +00:00
def custom_language
language_name = @repository && @repository.gitattribute(@blob_name, 'gitlab-language')
2016-06-21 16:23:42 +00:00
return nil unless language_name
2016-06-21 16:23:42 +00:00
Rouge::Lexer.find_fancy(language_name)
2016-06-21 16:23:42 +00:00
end
2016-07-10 21:13:06 +00:00
def highlight_text(text, continue: true, plain: false)
if plain
highlight_plain(text)
else
highlight_rich(text, continue: continue)
end
end
def highlight_plain(text)
@formatter.format(Rouge::Lexers::PlainText.lex(text)).html_safe
end
def highlight_rich(text, continue: true)
@formatter.format(lexer.lex(text, continue: continue), tag: lexer.tag).html_safe
rescue
highlight_plain(text)
end
def link_dependencies(text, highlighted_text)
Gitlab::DependencyLinker.link(blob_name, text, highlighted_text)
end
end
end