2018-10-22 07:00:50 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-01-14 14:11:50 +00:00
|
|
|
module Gitlab
|
|
|
|
class Highlight
|
2018-08-16 15:27:20 +00:00
|
|
|
TIMEOUT_BACKGROUND = 30.seconds
|
2020-08-24 18:10:19 +00:00
|
|
|
TIMEOUT_FOREGROUND = 1.5.seconds
|
2018-08-16 15:27:20 +00:00
|
|
|
|
2019-08-09 00:13:09 +00:00
|
|
|
def self.highlight(blob_name, blob_content, language: nil, plain: false)
|
|
|
|
new(blob_name, blob_content, language: language)
|
2017-06-21 13:48:12 +00:00
|
|
|
.highlight(blob_content, continue: false, plain: plain)
|
2016-01-14 14:11:50 +00:00
|
|
|
end
|
|
|
|
|
2021-05-06 12:10:38 +00:00
|
|
|
def self.too_large?(size)
|
2021-07-07 00:07:23 +00:00
|
|
|
return false unless size.to_i > self.file_size_limit
|
2021-05-12 09:10:19 +00:00
|
|
|
|
2021-07-07 00:07:23 +00:00
|
|
|
over_highlight_size_limit.increment(source: "file size: #{self.file_size_limit}") if Feature.enabled?(:track_file_size_over_highlight_limit)
|
2021-05-12 09:10:19 +00:00
|
|
|
|
|
|
|
true
|
2021-05-06 12:10:38 +00:00
|
|
|
end
|
|
|
|
|
2016-07-10 21:13:06 +00:00
|
|
|
attr_reader :blob_name
|
|
|
|
|
2019-08-09 00:13:09 +00:00
|
|
|
def initialize(blob_name, blob_content, language: nil)
|
2017-03-10 21:34:29 +00:00
|
|
|
@formatter = Rouge::Formatters::HTMLGitlab
|
2018-09-06 04:34:25 +00:00
|
|
|
@language = language
|
2016-07-14 18:40:55 +00:00
|
|
|
@blob_name = blob_name
|
2016-07-14 18:44:59 +00:00
|
|
|
@blob_content = blob_content
|
2016-06-10 22:42:43 +00:00
|
|
|
end
|
|
|
|
|
2021-03-18 06:11:52 +00:00
|
|
|
def highlight(text, continue: false, plain: false, context: {})
|
|
|
|
@context = context
|
|
|
|
|
2021-05-06 12:10:38 +00:00
|
|
|
plain ||= self.class.too_large?(text.length)
|
2018-09-11 04:37:44 +00:00
|
|
|
|
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
|
2016-01-19 13:52:41 +00:00
|
|
|
end
|
|
|
|
|
2016-07-14 18:44:59 +00:00
|
|
|
def lexer
|
|
|
|
@lexer ||= custom_language || begin
|
2016-07-14 19:21:22 +00:00
|
|
|
Rouge::Lexer.guess(filename: @blob_name, source: @blob_content).new
|
2021-03-21 18:09:29 +00:00
|
|
|
rescue Rouge::Guesser::Ambiguous => e
|
|
|
|
e.alternatives.min_by(&:tag)
|
2016-07-14 18:44:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-14 14:11:50 +00:00
|
|
|
private
|
|
|
|
|
2021-03-18 06:11:52 +00:00
|
|
|
attr_reader :context
|
|
|
|
|
2021-07-07 00:07:23 +00:00
|
|
|
def self.file_size_limit
|
2021-07-30 18:09:08 +00:00
|
|
|
Gitlab.config.extra['maximum_text_highlight_size_kilobytes']
|
2021-07-07 00:07:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private_class_method :file_size_limit
|
|
|
|
|
2016-06-21 16:23:42 +00:00
|
|
|
def custom_language
|
2019-02-08 12:19:53 +00:00
|
|
|
return unless @language
|
2016-06-21 16:23:42 +00:00
|
|
|
|
2018-09-06 04:34:25 +00:00
|
|
|
Rouge::Lexer.find_fancy(@language)
|
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)
|
2021-09-21 21:11:35 +00:00
|
|
|
@formatter.format(Rouge::Lexers::PlainText.lex(text), **context).html_safe
|
2016-07-10 21:13:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def highlight_rich(text, continue: true)
|
2021-06-14 21:10:22 +00:00
|
|
|
add_highlight_attempt_metric
|
|
|
|
|
2018-08-16 15:27:20 +00:00
|
|
|
tag = lexer.tag
|
|
|
|
tokens = lexer.lex(text, continue: continue)
|
2021-09-21 21:11:35 +00:00
|
|
|
Timeout.timeout(timeout_time) { @formatter.format(tokens, **context, tag: tag).html_safe }
|
2018-08-16 15:27:20 +00:00
|
|
|
rescue Timeout::Error => e
|
2021-05-06 12:10:38 +00:00
|
|
|
add_highlight_timeout_metric
|
|
|
|
|
2019-12-16 12:07:43 +00:00
|
|
|
Gitlab::ErrorTracking.track_and_raise_for_dev_exception(e)
|
2018-08-16 15:27:20 +00:00
|
|
|
highlight_plain(text)
|
2021-04-26 12:09:44 +00:00
|
|
|
rescue StandardError
|
2016-07-10 21:13:06 +00:00
|
|
|
highlight_plain(text)
|
|
|
|
end
|
|
|
|
|
2018-08-16 15:27:20 +00:00
|
|
|
def timeout_time
|
2019-12-22 09:07:51 +00:00
|
|
|
Gitlab::Runtime.sidekiq? ? TIMEOUT_BACKGROUND : TIMEOUT_FOREGROUND
|
2018-08-16 15:27:20 +00:00
|
|
|
end
|
|
|
|
|
2016-07-10 21:13:06 +00:00
|
|
|
def link_dependencies(text, highlighted_text)
|
|
|
|
Gitlab::DependencyLinker.link(blob_name, text, highlighted_text)
|
|
|
|
end
|
2021-05-05 18:10:31 +00:00
|
|
|
|
2021-06-14 21:10:22 +00:00
|
|
|
def add_highlight_attempt_metric
|
|
|
|
return unless Feature.enabled?(:track_highlight_timeouts)
|
|
|
|
|
|
|
|
highlighting_attempt.increment(source: (@language || "undefined"))
|
|
|
|
end
|
|
|
|
|
2021-05-06 12:10:38 +00:00
|
|
|
def add_highlight_timeout_metric
|
|
|
|
return unless Feature.enabled?(:track_highlight_timeouts)
|
|
|
|
|
|
|
|
highlight_timeout.increment(source: Gitlab::Runtime.sidekiq? ? "background" : "foreground")
|
|
|
|
end
|
|
|
|
|
2021-06-14 21:10:22 +00:00
|
|
|
def highlighting_attempt
|
|
|
|
@highlight_attempt ||= Gitlab::Metrics.counter(
|
|
|
|
:file_highlighting_attempt,
|
|
|
|
'Counts the times highlighting has been attempted on a file'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-05-06 12:10:38 +00:00
|
|
|
def highlight_timeout
|
|
|
|
@highlight_timeout ||= Gitlab::Metrics.counter(
|
|
|
|
:highlight_timeout,
|
|
|
|
'Counts the times highlights have timed out'
|
|
|
|
)
|
2021-05-05 18:10:31 +00:00
|
|
|
end
|
2021-05-12 09:10:19 +00:00
|
|
|
|
|
|
|
def self.over_highlight_size_limit
|
|
|
|
@over_highlight_size_limit ||= Gitlab::Metrics.counter(
|
|
|
|
:over_highlight_size_limit,
|
|
|
|
'Count the times files have been over the highlight size limit'
|
|
|
|
)
|
|
|
|
end
|
2016-01-14 14:11:50 +00:00
|
|
|
end
|
|
|
|
end
|