2018-11-05 23:45:35 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-07-10 17:13:06 -04:00
|
|
|
module Gitlab
|
|
|
|
module DependencyLinker
|
|
|
|
class BaseLinker
|
2017-05-25 16:52:39 -04:00
|
|
|
URL_REGEX = %r{https?://[^'" ]+}.freeze
|
2019-02-15 14:11:16 -05:00
|
|
|
GIT_INVALID_URL_REGEX = /^git\+#{URL_REGEX}/.freeze
|
2017-05-25 16:52:39 -04:00
|
|
|
REPO_REGEX = %r{[^/'" ]+/[^/'" ]+}.freeze
|
2017-05-16 16:25:57 -04:00
|
|
|
|
2017-05-12 15:31:05 -04:00
|
|
|
class_attribute :file_type
|
|
|
|
|
|
|
|
def self.support?(blob_name)
|
|
|
|
Gitlab::FileDetector.type_of(blob_name) == file_type
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.link(*args)
|
|
|
|
new(*args).link
|
2016-07-10 17:13:06 -04:00
|
|
|
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-02-15 14:11:16 -05:00
|
|
|
def external_url(name, external_ref)
|
|
|
|
return if external_ref =~ GIT_INVALID_URL_REGEX
|
|
|
|
|
|
|
|
case external_ref
|
|
|
|
when /\A#{URL_REGEX}\z/
|
|
|
|
external_ref
|
|
|
|
when /\A#{REPO_REGEX}\z/
|
|
|
|
github_url(external_ref)
|
|
|
|
else
|
|
|
|
package_url(name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-10 17:13:06 -04:00
|
|
|
private
|
|
|
|
|
2019-02-25 08:22:13 -05:00
|
|
|
def package_url(_name)
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
2016-07-10 17:13:06 -04:00
|
|
|
def link_dependencies
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
2017-05-16 16:25:57 -04:00
|
|
|
def license_url(name)
|
|
|
|
Licensee::License.find(name)&.url
|
2016-07-10 17:13:06 -04:00
|
|
|
end
|
|
|
|
|
2017-05-16 16:27:50 -04:00
|
|
|
def github_url(name)
|
|
|
|
"https://github.com/#{name}"
|
|
|
|
end
|
2016-07-10 17:13:06 -04:00
|
|
|
|
2017-05-16 16:25:57 -04:00
|
|
|
def link_tag(name, url)
|
2019-11-26 10:06:50 -05:00
|
|
|
%{<a href="#{ERB::Util.html_escape_once(url)}" rel="nofollow noreferrer noopener" target="_blank">#{ERB::Util.html_escape_once(name)}</a>}.html_safe
|
2016-07-10 17:13:06 -04:00
|
|
|
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"`
|
2017-05-16 16:25:57 -04:00
|
|
|
def link_regex(regex, &url_proc)
|
2016-07-10 17:13:06 -04:00
|
|
|
highlighted_lines.map!.with_index do |rich_line, i|
|
2017-06-27 15:47:57 -04:00
|
|
|
marker = StringRegexMarker.new(plain_lines[i].chomp, rich_line.html_safe)
|
2016-07-10 17:13:06 -04:00
|
|
|
|
|
|
|
marker.mark(regex, group: :name) do |text, left:, right:|
|
2017-05-16 16:25:57 -04:00
|
|
|
url = yield(text)
|
|
|
|
url ? link_tag(text, url) : text
|
2016-07-10 17:13:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def plain_lines
|
|
|
|
@plain_lines ||= plain_text.lines
|
|
|
|
end
|
|
|
|
|
|
|
|
def highlighted_lines
|
|
|
|
@highlighted_lines ||= highlighted_text.lines
|
|
|
|
end
|
2017-05-25 09:55:47 -04:00
|
|
|
|
2017-05-25 16:52:39 -04:00
|
|
|
def regexp_for_value(value, default: /[^'" ]+/)
|
2017-05-25 09:55:47 -04:00
|
|
|
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
|
2016-07-10 17:13:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|