2015-12-15 14:51:16 +00:00
|
|
|
module Banzai
|
|
|
|
module Filter
|
2015-12-01 15:25:56 +00:00
|
|
|
# Issues, Merge Requests, Snippets, Commits and Commit Ranges share
|
2015-12-01 16:13:47 +00:00
|
|
|
# similar functionality in reference filtering.
|
2015-11-16 20:56:28 +00:00
|
|
|
class AbstractReferenceFilter < ReferenceFilter
|
|
|
|
include CrossProjectReference
|
|
|
|
|
|
|
|
def self.object_class
|
|
|
|
# Implement in child class
|
|
|
|
# Example: MergeRequest
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.object_name
|
2016-03-24 15:27:52 +00:00
|
|
|
@object_name ||= object_class.name.underscore
|
2015-11-16 20:56:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.object_sym
|
2016-03-24 15:27:52 +00:00
|
|
|
@object_sym ||= object_name.to_sym
|
2015-11-16 20:56:28 +00:00
|
|
|
end
|
|
|
|
|
2016-03-29 09:58:05 +00:00
|
|
|
def self.object_class_title
|
|
|
|
@object_title ||= object_class.name.titleize
|
|
|
|
end
|
|
|
|
|
2015-11-16 20:56:28 +00:00
|
|
|
# Public: Find references in text (like `!123` for merge requests)
|
|
|
|
#
|
2015-12-01 11:49:22 +00:00
|
|
|
# AnyReferenceFilter.references_in(text) do |match, id, project_ref, matches|
|
|
|
|
# object = find_object(project_ref, id)
|
|
|
|
# "<a href=...>#{object.to_reference}</a>"
|
2015-11-16 20:56:28 +00:00
|
|
|
# end
|
|
|
|
#
|
|
|
|
# text - String text to search.
|
|
|
|
#
|
2015-12-01 11:49:22 +00:00
|
|
|
# Yields the String match, the Integer referenced object ID, an optional String
|
|
|
|
# of the external project reference, and all of the matchdata.
|
2015-11-16 20:56:28 +00:00
|
|
|
#
|
|
|
|
# Returns a String replaced with the return of the block.
|
2015-12-01 14:51:27 +00:00
|
|
|
def self.references_in(text, pattern = object_class.reference_pattern)
|
|
|
|
text.gsub(pattern) do |match|
|
2015-11-30 20:14:46 +00:00
|
|
|
yield match, $~[object_sym].to_i, $~[:project], $~
|
2015-11-16 20:56:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-12-30 16:16:59 +00:00
|
|
|
def object_class
|
|
|
|
self.class.object_class
|
|
|
|
end
|
|
|
|
|
|
|
|
def object_sym
|
|
|
|
self.class.object_sym
|
|
|
|
end
|
|
|
|
|
2016-03-29 09:58:05 +00:00
|
|
|
def object_class_title
|
|
|
|
self.class.object_class_title
|
|
|
|
end
|
|
|
|
|
2015-12-30 16:16:59 +00:00
|
|
|
def references_in(*args, &block)
|
|
|
|
self.class.references_in(*args, &block)
|
|
|
|
end
|
2015-11-16 20:56:28 +00:00
|
|
|
|
|
|
|
def find_object(project, id)
|
|
|
|
# Implement in child class
|
|
|
|
# Example: project.merge_requests.find
|
|
|
|
end
|
|
|
|
|
2016-03-24 14:53:38 +00:00
|
|
|
def find_object_cached(project, id)
|
|
|
|
if RequestStore.active?
|
|
|
|
cache = find_objects_cache[object_class][project.id]
|
|
|
|
|
2016-04-04 09:39:11 +00:00
|
|
|
get_or_set_cache(cache, id) { find_object(project, id) }
|
2016-03-24 14:53:38 +00:00
|
|
|
else
|
|
|
|
find_object(project, id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def project_from_ref_cache(ref)
|
|
|
|
if RequestStore.active?
|
|
|
|
cache = project_refs_cache
|
|
|
|
|
2016-04-04 09:39:11 +00:00
|
|
|
get_or_set_cache(cache, ref) { project_from_ref(ref) }
|
2016-03-24 14:53:38 +00:00
|
|
|
else
|
|
|
|
project_from_ref(ref)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-11-16 20:56:28 +00:00
|
|
|
def url_for_object(object, project)
|
|
|
|
# Implement in child class
|
|
|
|
# Example: project_merge_request_url
|
|
|
|
end
|
|
|
|
|
2016-03-24 14:53:38 +00:00
|
|
|
def url_for_object_cached(object, project)
|
|
|
|
if RequestStore.active?
|
|
|
|
cache = url_for_object_cache[object_class][project.id]
|
|
|
|
|
2016-04-04 09:39:11 +00:00
|
|
|
get_or_set_cache(cache, object) { url_for_object(object, project) }
|
2016-03-24 14:53:38 +00:00
|
|
|
else
|
|
|
|
url_for_object(object, project)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-11-16 20:56:28 +00:00
|
|
|
def call
|
2016-03-24 14:57:22 +00:00
|
|
|
return doc if project.nil?
|
2015-12-24 13:43:07 +00:00
|
|
|
|
2016-03-24 14:57:22 +00:00
|
|
|
ref_pattern = object_class.reference_pattern
|
|
|
|
link_pattern = object_class.link_reference_pattern
|
2015-12-01 14:51:27 +00:00
|
|
|
|
2016-06-01 15:39:12 +00:00
|
|
|
nodes.each do |node|
|
2016-03-24 14:57:22 +00:00
|
|
|
if text_node?(node) && ref_pattern
|
|
|
|
replace_text_when_pattern_matches(node, ref_pattern) do |content|
|
|
|
|
object_link_filter(content, ref_pattern)
|
|
|
|
end
|
|
|
|
|
|
|
|
elsif element_node?(node)
|
|
|
|
yield_valid_link(node) do |link, text|
|
2016-04-06 12:24:30 +00:00
|
|
|
if ref_pattern && link =~ /\A#{ref_pattern}\z/
|
2016-03-24 14:57:22 +00:00
|
|
|
replace_link_node_with_href(node, link) do
|
|
|
|
object_link_filter(link, ref_pattern, link_text: text)
|
|
|
|
end
|
|
|
|
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
next unless link_pattern
|
|
|
|
|
|
|
|
if link == text && text =~ /\A#{link_pattern}/
|
|
|
|
replace_link_node_with_text(node, link) do
|
|
|
|
object_link_filter(text, link_pattern)
|
|
|
|
end
|
|
|
|
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
if link =~ /\A#{link_pattern}\z/
|
|
|
|
replace_link_node_with_href(node, link) do
|
|
|
|
object_link_filter(link, link_pattern, link_text: text)
|
|
|
|
end
|
2015-12-24 13:43:07 +00:00
|
|
|
|
2016-03-24 14:57:22 +00:00
|
|
|
next
|
|
|
|
end
|
|
|
|
end
|
2015-12-24 13:43:07 +00:00
|
|
|
end
|
2015-11-16 20:56:28 +00:00
|
|
|
end
|
2016-02-25 11:45:38 +00:00
|
|
|
|
|
|
|
doc
|
2015-11-16 20:56:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Replace references (like `!123` for merge requests) in text with links
|
|
|
|
# to the referenced object's details page.
|
|
|
|
#
|
|
|
|
# text - String text to replace references in.
|
2015-12-01 16:13:47 +00:00
|
|
|
# pattern - Reference pattern to match against.
|
|
|
|
# link_text - Original content of the link being replaced.
|
2015-11-16 20:56:28 +00:00
|
|
|
#
|
|
|
|
# Returns a String with references replaced with links. All links
|
|
|
|
# have `gfm` and `gfm-OBJECT_NAME` class names attached for styling.
|
2015-12-01 15:25:56 +00:00
|
|
|
def object_link_filter(text, pattern, link_text: nil)
|
2015-12-01 14:51:27 +00:00
|
|
|
references_in(text, pattern) do |match, id, project_ref, matches|
|
2016-03-24 14:53:38 +00:00
|
|
|
project = project_from_ref_cache(project_ref)
|
2015-11-16 20:56:28 +00:00
|
|
|
|
2016-03-24 14:53:38 +00:00
|
|
|
if project && object = find_object_cached(project, id)
|
2015-12-24 20:19:03 +00:00
|
|
|
title = object_link_title(object)
|
2015-11-16 20:56:28 +00:00
|
|
|
klass = reference_class(object_sym)
|
2015-12-01 16:13:47 +00:00
|
|
|
|
2016-06-28 15:50:17 +00:00
|
|
|
data = data_attributes_for(link_text || match, project, object)
|
2015-12-01 14:51:27 +00:00
|
|
|
|
2016-03-24 14:53:38 +00:00
|
|
|
if matches.names.include?("url") && matches[:url]
|
|
|
|
url = matches[:url]
|
|
|
|
else
|
|
|
|
url = url_for_object_cached(object, project)
|
|
|
|
end
|
2015-11-30 20:14:46 +00:00
|
|
|
|
2015-12-24 20:19:03 +00:00
|
|
|
text = link_text || object_link_text(object, matches)
|
2015-11-16 20:56:28 +00:00
|
|
|
|
|
|
|
%(<a href="#{url}" #{data}
|
2015-12-24 20:19:03 +00:00
|
|
|
title="#{escape_once(title)}"
|
|
|
|
class="#{klass}">#{escape_once(text)}</a>)
|
2015-11-16 20:56:28 +00:00
|
|
|
else
|
|
|
|
match
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-28 15:50:17 +00:00
|
|
|
def data_attributes_for(text, project, object)
|
|
|
|
data_attribute(
|
|
|
|
original: text,
|
|
|
|
project: project.id,
|
|
|
|
object_sym => object.id
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2015-11-30 20:14:46 +00:00
|
|
|
def object_link_text_extras(object, matches)
|
|
|
|
extras = []
|
|
|
|
|
2015-12-01 14:51:27 +00:00
|
|
|
if matches.names.include?("anchor") && matches[:anchor] && matches[:anchor] =~ /\A\#note_(\d+)\z/
|
2015-11-30 20:14:46 +00:00
|
|
|
extras << "comment #{$1}"
|
|
|
|
end
|
|
|
|
|
|
|
|
extras
|
|
|
|
end
|
|
|
|
|
|
|
|
def object_link_title(object)
|
2016-03-29 09:58:05 +00:00
|
|
|
"#{object_class_title}: #{object.title}"
|
2015-11-16 20:56:28 +00:00
|
|
|
end
|
2015-12-24 13:38:23 +00:00
|
|
|
|
|
|
|
def object_link_text(object, matches)
|
|
|
|
text = object.reference_link_text(context[:project])
|
|
|
|
|
|
|
|
extras = object_link_text_extras(object, matches)
|
|
|
|
text += " (#{extras.join(", ")})" if extras.any?
|
|
|
|
|
|
|
|
text
|
|
|
|
end
|
2016-03-24 14:53:38 +00:00
|
|
|
|
2016-06-01 15:39:12 +00:00
|
|
|
# Returns a Hash containing all object references (e.g. issue IDs) per the
|
|
|
|
# project they belong to.
|
|
|
|
def references_per_project
|
|
|
|
@references_per_project ||= begin
|
|
|
|
refs = Hash.new { |hash, key| hash[key] = Set.new }
|
|
|
|
|
|
|
|
regex = Regexp.union(object_class.reference_pattern,
|
|
|
|
object_class.link_reference_pattern)
|
|
|
|
|
|
|
|
nodes.each do |node|
|
|
|
|
node.to_html.scan(regex) do
|
|
|
|
project = $~[:project] || current_project_path
|
2016-06-18 17:55:45 +00:00
|
|
|
symbol = $~[object_sym]
|
2016-06-01 15:39:12 +00:00
|
|
|
|
2016-06-18 17:55:45 +00:00
|
|
|
refs[project] << symbol if object_class.reference_valid?(symbol)
|
2016-06-01 15:39:12 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
refs
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns a Hash containing referenced projects grouped per their full
|
|
|
|
# path.
|
|
|
|
def projects_per_reference
|
|
|
|
@projects_per_reference ||= begin
|
|
|
|
hash = {}
|
|
|
|
refs = Set.new
|
|
|
|
|
|
|
|
references_per_project.each do |project_ref, _|
|
|
|
|
refs << project_ref
|
|
|
|
end
|
|
|
|
|
|
|
|
find_projects_for_paths(refs.to_a).each do |project|
|
|
|
|
hash[project.path_with_namespace] = project
|
|
|
|
end
|
|
|
|
|
|
|
|
hash
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns the projects for the given paths.
|
|
|
|
def find_projects_for_paths(paths)
|
|
|
|
Project.where_paths_in(paths).includes(:namespace)
|
|
|
|
end
|
|
|
|
|
|
|
|
def current_project_path
|
|
|
|
@current_project_path ||= project.path_with_namespace
|
|
|
|
end
|
|
|
|
|
2016-03-24 14:53:38 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def project_refs_cache
|
|
|
|
RequestStore[:banzai_project_refs] ||= {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_objects_cache
|
|
|
|
RequestStore[:banzai_find_objects_cache] ||= Hash.new do |hash, key|
|
|
|
|
hash[key] = Hash.new { |h, k| h[k] = {} }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def url_for_object_cache
|
|
|
|
RequestStore[:banzai_url_for_object] ||= Hash.new do |hash, key|
|
|
|
|
hash[key] = Hash.new { |h, k| h[k] = {} }
|
|
|
|
end
|
|
|
|
end
|
2016-04-04 09:39:11 +00:00
|
|
|
|
|
|
|
def get_or_set_cache(cache, key)
|
|
|
|
if cache.key?(key)
|
|
|
|
cache[key]
|
|
|
|
else
|
2016-05-27 03:37:41 +00:00
|
|
|
value = yield
|
|
|
|
cache[key] = value if key.present?
|
|
|
|
value
|
2016-04-04 09:39:11 +00:00
|
|
|
end
|
|
|
|
end
|
2015-11-16 20:56:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|