2015-11-16 20:56:28 +00:00
|
|
|
require 'gitlab/markdown'
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Markdown
|
2015-12-01 11:49:22 +00:00
|
|
|
# Issues, Snippets, Merge Requests, Commits and Commit Ranges share
|
|
|
|
# similar functionality in refernce 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
|
|
|
|
object_class.name.underscore
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.object_sym
|
|
|
|
object_name.to_sym
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.data_reference
|
|
|
|
"data-#{object_name.dasherize}"
|
|
|
|
end
|
|
|
|
|
|
|
|
# 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.
|
|
|
|
def self.references_in(text)
|
|
|
|
text.gsub(object_class.reference_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
|
|
|
|
|
|
|
|
def self.referenced_by(node)
|
|
|
|
{ object_sym => LazyReference.new(object_class, node.attr(data_reference)) }
|
|
|
|
end
|
|
|
|
|
|
|
|
delegate :object_class, :object_sym, :references_in, to: :class
|
|
|
|
|
|
|
|
def find_object(project, id)
|
|
|
|
# Implement in child class
|
|
|
|
# Example: project.merge_requests.find
|
|
|
|
end
|
|
|
|
|
|
|
|
def url_for_object(object, project)
|
|
|
|
# Implement in child class
|
|
|
|
# Example: project_merge_request_url
|
|
|
|
end
|
|
|
|
|
|
|
|
def call
|
|
|
|
replace_text_nodes_matching(object_class.reference_pattern) do |content|
|
|
|
|
object_link_filter(content)
|
|
|
|
end
|
|
|
|
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.
|
|
|
|
#
|
|
|
|
# Returns a String with references replaced with links. All links
|
|
|
|
# have `gfm` and `gfm-OBJECT_NAME` class names attached for styling.
|
|
|
|
def object_link_filter(text)
|
2015-11-30 20:14:46 +00:00
|
|
|
references_in(text) do |match, id, project_ref, matches|
|
2015-11-16 20:56:28 +00:00
|
|
|
project = project_from_ref(project_ref)
|
|
|
|
|
|
|
|
if project && object = find_object(project, id)
|
2015-11-30 20:14:46 +00:00
|
|
|
title = escape_once(object_link_title(object))
|
2015-11-16 20:56:28 +00:00
|
|
|
klass = reference_class(object_sym)
|
2015-11-30 20:14:46 +00:00
|
|
|
data = data_attribute(project: project.id, object_sym => object.id, original: match)
|
|
|
|
url = matches[:url] || url_for_object(object, project)
|
|
|
|
|
|
|
|
text = object.to_reference(context[:project])
|
|
|
|
|
|
|
|
extras = object_link_text_extras(object, matches)
|
|
|
|
text += " (#{extras.join(", ")})" if extras.any?
|
2015-11-16 20:56:28 +00:00
|
|
|
|
|
|
|
%(<a href="#{url}" #{data}
|
|
|
|
title="#{title}"
|
2015-11-30 20:14:46 +00:00
|
|
|
class="#{klass}">#{text}</a>)
|
2015-11-16 20:56:28 +00:00
|
|
|
else
|
|
|
|
match
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-11-30 20:14:46 +00:00
|
|
|
def object_link_text_extras(object, matches)
|
|
|
|
extras = []
|
|
|
|
|
|
|
|
if matches[:anchor] && matches[:anchor] =~ /\A\#note_(\d+)\z/
|
|
|
|
extras << "comment #{$1}"
|
|
|
|
end
|
|
|
|
|
|
|
|
extras
|
|
|
|
end
|
|
|
|
|
|
|
|
def object_link_title(object)
|
|
|
|
"#{object_class.name.titleize}: #{object.title}"
|
2015-11-16 20:56:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|