2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-01-14 09:11:50 -05:00
|
|
|
module Gitlab
|
|
|
|
class Highlight
|
2018-08-16 11:27:20 -04:00
|
|
|
TIMEOUT_BACKGROUND = 30.seconds
|
|
|
|
TIMEOUT_FOREGROUND = 3.seconds
|
2018-09-11 00:37:44 -04:00
|
|
|
MAXIMUM_TEXT_HIGHLIGHT_SIZE = 1.megabyte
|
2018-08-16 11:27:20 -04:00
|
|
|
|
2019-08-08 20:13:09 -04:00
|
|
|
def self.highlight(blob_name, blob_content, language: nil, plain: false)
|
|
|
|
new(blob_name, blob_content, language: language)
|
2017-06-21 09:48:12 -04:00
|
|
|
.highlight(blob_content, continue: false, plain: plain)
|
2016-01-14 09:11:50 -05:00
|
|
|
end
|
|
|
|
|
2016-07-10 17:13:06 -04:00
|
|
|
attr_reader :blob_name
|
|
|
|
|
2019-08-08 20:13:09 -04:00
|
|
|
def initialize(blob_name, blob_content, language: nil)
|
2017-03-10 16:34:29 -05:00
|
|
|
@formatter = Rouge::Formatters::HTMLGitlab
|
2018-09-06 00:34:25 -04:00
|
|
|
@language = language
|
2016-07-14 14:40:55 -04:00
|
|
|
@blob_name = blob_name
|
2016-07-14 14:44:59 -04:00
|
|
|
@blob_content = blob_content
|
2016-06-10 18:42:43 -04:00
|
|
|
end
|
|
|
|
|
2016-04-14 06:44:35 -04:00
|
|
|
def highlight(text, continue: true, plain: false)
|
2018-09-11 00:37:44 -04:00
|
|
|
plain ||= text.length > MAXIMUM_TEXT_HIGHLIGHT_SIZE
|
|
|
|
|
2016-07-10 17:13:06 -04:00
|
|
|
highlighted_text = highlight_text(text, continue: continue, plain: plain)
|
|
|
|
highlighted_text = link_dependencies(text, highlighted_text) if blob_name
|
|
|
|
highlighted_text
|
2016-01-19 08:52:41 -05:00
|
|
|
end
|
|
|
|
|
2016-07-14 14:44:59 -04:00
|
|
|
def lexer
|
|
|
|
@lexer ||= custom_language || begin
|
2016-07-14 15:21:22 -04:00
|
|
|
Rouge::Lexer.guess(filename: @blob_name, source: @blob_content).new
|
|
|
|
rescue Rouge::Guesser::Ambiguous => e
|
2020-01-27 16:08:47 -05:00
|
|
|
e.alternatives.min_by(&:tag)
|
2016-07-14 14:44:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-14 09:11:50 -05:00
|
|
|
private
|
|
|
|
|
2016-06-21 12:23:42 -04:00
|
|
|
def custom_language
|
2019-02-08 07:19:53 -05:00
|
|
|
return unless @language
|
2016-06-21 12:23:42 -04:00
|
|
|
|
2018-09-06 00:34:25 -04:00
|
|
|
Rouge::Lexer.find_fancy(@language)
|
2016-06-21 12:23:42 -04:00
|
|
|
end
|
2016-07-10 17:13:06 -04: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)
|
2019-08-08 20:13:09 -04:00
|
|
|
@formatter.format(Rouge::Lexers::PlainText.lex(text)).html_safe
|
2016-07-10 17:13:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def highlight_rich(text, continue: true)
|
2018-08-16 11:27:20 -04:00
|
|
|
tag = lexer.tag
|
|
|
|
tokens = lexer.lex(text, continue: continue)
|
2019-08-08 20:13:09 -04:00
|
|
|
Timeout.timeout(timeout_time) { @formatter.format(tokens, tag: tag).html_safe }
|
2018-08-16 11:27:20 -04:00
|
|
|
rescue Timeout::Error => e
|
2019-12-16 07:07:43 -05:00
|
|
|
Gitlab::ErrorTracking.track_and_raise_for_dev_exception(e)
|
2018-08-16 11:27:20 -04:00
|
|
|
highlight_plain(text)
|
2016-07-10 17:13:06 -04:00
|
|
|
rescue
|
|
|
|
highlight_plain(text)
|
|
|
|
end
|
|
|
|
|
2018-08-16 11:27:20 -04:00
|
|
|
def timeout_time
|
2019-12-22 04:07:51 -05:00
|
|
|
Gitlab::Runtime.sidekiq? ? TIMEOUT_BACKGROUND : TIMEOUT_FOREGROUND
|
2018-08-16 11:27:20 -04:00
|
|
|
end
|
|
|
|
|
2016-07-10 17:13:06 -04:00
|
|
|
def link_dependencies(text, highlighted_text)
|
|
|
|
Gitlab::DependencyLinker.link(blob_name, text, highlighted_text)
|
|
|
|
end
|
2016-01-14 09:11:50 -05:00
|
|
|
end
|
|
|
|
end
|