2018-12-13 13:39:08 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-01-29 22:53:50 +05:30
|
|
|
module Gitlab
|
|
|
|
class Highlight
|
2023-01-13 00:05:48 +05:30
|
|
|
def self.highlight(blob_name, blob_content, language: nil, plain: false, context: {})
|
2018-12-13 13:39:08 +05:30
|
|
|
new(blob_name, blob_content, language: language)
|
2023-01-13 00:05:48 +05:30
|
|
|
.highlight(blob_content, continue: false, plain: plain, context: context)
|
2016-01-29 22:53:50 +05:30
|
|
|
end
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
def self.too_large?(size)
|
2022-05-07 20:08:51 +05:30
|
|
|
size.to_i > self.file_size_limit
|
2021-06-08 01:23:25 +05:30
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
attr_reader :blob_name
|
2016-01-29 22:53:50 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
def initialize(blob_name, blob_content, language: nil)
|
2017-08-17 22:00:37 +05:30
|
|
|
@formatter = Rouge::Formatters::HTMLGitlab
|
2018-12-13 13:39:08 +05:30
|
|
|
@language = language
|
2016-08-24 12:49:21 +05:30
|
|
|
@blob_name = blob_name
|
|
|
|
@blob_content = blob_content
|
2016-01-29 22:53:50 +05:30
|
|
|
end
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
def highlight(text, continue: false, plain: false, context: {})
|
|
|
|
@context = context
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
plain ||= self.class.too_large?(text.length)
|
2018-12-13 13:39:08 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
highlighted_text = highlight_text(text, continue: continue, plain: plain)
|
|
|
|
highlighted_text = link_dependencies(text, highlighted_text) if blob_name
|
|
|
|
highlighted_text
|
2016-01-29 22:53:50 +05:30
|
|
|
end
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
def lexer
|
|
|
|
@lexer ||= custom_language || begin
|
|
|
|
Rouge::Lexer.guess(filename: @blob_name, source: @blob_content).new
|
2021-04-29 21:17:54 +05:30
|
|
|
rescue Rouge::Guesser::Ambiguous => e
|
|
|
|
e.alternatives.min_by(&:tag)
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-29 22:53:50 +05:30
|
|
|
private
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
attr_reader :context
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
def self.file_size_limit
|
2021-10-27 15:23:28 +05:30
|
|
|
Gitlab.config.extra['maximum_text_highlight_size_kilobytes']
|
2021-09-30 23:02:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private_class_method :file_size_limit
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
def custom_language
|
2019-07-07 11:18:12 +05:30
|
|
|
return unless @language
|
2016-01-29 22:53:50 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
Rouge::Lexer.find_fancy(@language)
|
2016-01-29 22:53:50 +05:30
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
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-11-18 22:05:49 +05:30
|
|
|
@formatter.format(Rouge::Lexers::PlainText.lex(text), **context).html_safe
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def highlight_rich(text, continue: true)
|
2018-11-08 19:23:39 +05:30
|
|
|
tag = lexer.tag
|
|
|
|
tokens = lexer.lex(text, continue: continue)
|
2022-07-23 23:45:48 +05:30
|
|
|
Gitlab::RenderTimeout.timeout { @formatter.format(tokens, **context, tag: tag).html_safe }
|
2018-11-08 19:23:39 +05:30
|
|
|
rescue Timeout::Error => e
|
2020-01-01 13:55:28 +05:30
|
|
|
Gitlab::ErrorTracking.track_and_raise_for_dev_exception(e)
|
2018-11-08 19:23:39 +05:30
|
|
|
highlight_plain(text)
|
2021-06-08 01:23:25 +05:30
|
|
|
rescue StandardError
|
2017-09-10 17:25:29 +05:30
|
|
|
highlight_plain(text)
|
|
|
|
end
|
|
|
|
|
|
|
|
def link_dependencies(text, highlighted_text)
|
|
|
|
Gitlab::DependencyLinker.link(blob_name, text, highlighted_text)
|
|
|
|
end
|
2016-01-29 22:53:50 +05:30
|
|
|
end
|
|
|
|
end
|