2019-02-15 15:39:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
module Gitlab
|
|
|
|
module DependencyLinker
|
|
|
|
class BaseLinker
|
|
|
|
URL_REGEX = %r{https?://[^'" ]+}.freeze
|
2019-03-13 22:55:13 +05:30
|
|
|
GIT_INVALID_URL_REGEX = /^git\+#{URL_REGEX}/.freeze
|
2017-09-10 17:25:29 +05:30
|
|
|
REPO_REGEX = %r{[^/'" ]+/[^/'" ]+}.freeze
|
2020-03-07 23:17:34 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
class_attribute :file_type
|
|
|
|
|
|
|
|
def self.support?(blob_name)
|
|
|
|
Gitlab::FileDetector.type_of(blob_name) == file_type
|
|
|
|
end
|
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
def self.link(...)
|
|
|
|
new(...).link
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
attr_accessor :plain_text, :highlighted_text
|
|
|
|
|
|
|
|
def initialize(plain_text, highlighted_text)
|
|
|
|
@plain_text = plain_text
|
|
|
|
@highlighted_text = highlighted_text
|
|
|
|
end
|
|
|
|
|
|
|
|
def link
|
|
|
|
link_dependencies
|
|
|
|
|
|
|
|
highlighted_lines.join.html_safe
|
|
|
|
end
|
|
|
|
|
2019-03-13 22:55:13 +05:30
|
|
|
def external_url(name, external_ref)
|
|
|
|
return if external_ref =~ GIT_INVALID_URL_REGEX
|
|
|
|
|
|
|
|
case external_ref
|
2022-08-27 11:52:29 +05:30
|
|
|
when /\A#{URL_REGEX}\z/o
|
2019-03-13 22:55:13 +05:30
|
|
|
external_ref
|
2022-08-27 11:52:29 +05:30
|
|
|
when /\A#{REPO_REGEX}\z/o
|
2019-03-13 22:55:13 +05:30
|
|
|
github_url(external_ref)
|
|
|
|
else
|
|
|
|
package_url(name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
private
|
|
|
|
|
2019-03-13 22:55:13 +05:30
|
|
|
def package_url(_name)
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def link_dependencies
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def license_url(name)
|
|
|
|
Licensee::License.find(name)&.url
|
|
|
|
end
|
|
|
|
|
|
|
|
def github_url(name)
|
|
|
|
"https://github.com/#{name}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def link_tag(name, url)
|
2022-08-13 15:12:31 +05:30
|
|
|
href_attribute = %{href="#{ERB::Util.html_escape_once(url)}" } if Gitlab::UrlSanitizer.valid_web?(url)
|
|
|
|
|
|
|
|
%{<a #{href_attribute}rel="nofollow noreferrer noopener" target="_blank">#{ERB::Util.html_escape_once(name)}</a>}.html_safe
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
# Links package names based on regex.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
# link_regex(/(github:|:github =>)\s*['"](?<name>[^'"]+)['"]/)
|
|
|
|
# # Will link `user/repo` in `github: "user/repo"` or `:github => "user/repo"`
|
|
|
|
def link_regex(regex, &url_proc)
|
|
|
|
highlighted_lines.map!.with_index do |rich_line, i|
|
2021-01-29 00:20:46 +05:30
|
|
|
marker = StringRegexMarker.new((plain_lines[i].chomp! || plain_lines[i]), rich_line.html_safe)
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
marker.mark(regex, group: :name) do |text, left:, right:, mode:|
|
2017-09-10 17:25:29 +05:30
|
|
|
url = yield(text)
|
|
|
|
url ? link_tag(text, url) : text
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def plain_lines
|
|
|
|
@plain_lines ||= plain_text.lines
|
|
|
|
end
|
|
|
|
|
|
|
|
def highlighted_lines
|
|
|
|
@highlighted_lines ||= highlighted_text.lines
|
|
|
|
end
|
|
|
|
|
|
|
|
def regexp_for_value(value, default: /[^'" ]+/)
|
|
|
|
case value
|
|
|
|
when Array
|
|
|
|
Regexp.union(value.map { |v| regexp_for_value(v, default: default) })
|
|
|
|
when String
|
|
|
|
Regexp.escape(value)
|
|
|
|
when Regexp
|
|
|
|
value
|
|
|
|
else
|
|
|
|
default
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|