2015-11-16 20:56:28 +00:00
|
|
|
require 'gitlab/markdown'
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Markdown
|
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
|
|
|
|
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.
|
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
|
|
|
|
|
|
|
|
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
|
2015-12-01 15:26:05 +00:00
|
|
|
# `#123`
|
2015-11-16 20:56:28 +00:00
|
|
|
replace_text_nodes_matching(object_class.reference_pattern) do |content|
|
2015-12-01 14:51:27 +00:00
|
|
|
object_link_filter(content, object_class.reference_pattern)
|
|
|
|
end
|
|
|
|
|
2015-12-01 15:26:05 +00:00
|
|
|
# `[Issue](#123)`, which is turned into
|
|
|
|
# `<a href="#123">Issue</a>`
|
2015-12-01 15:25:56 +00:00
|
|
|
replace_link_nodes_with_href(object_class.reference_pattern) do |link, text|
|
|
|
|
object_link_filter(link, object_class.reference_pattern, link_text: text)
|
|
|
|
end
|
|
|
|
|
2015-12-01 15:26:05 +00:00
|
|
|
# `http://gitlab.example.com/namespace/project/issues/123`, which is turned into
|
|
|
|
# `<a href="http://gitlab.example.com/namespace/project/issues/123">http://gitlab.example.com/namespace/project/issues/123</a>`
|
2015-12-01 15:25:56 +00:00
|
|
|
replace_link_nodes_with_text(object_class.link_reference_pattern) do |text|
|
|
|
|
object_link_filter(text, object_class.link_reference_pattern)
|
|
|
|
end
|
2015-12-01 15:26:05 +00:00
|
|
|
|
|
|
|
# `[Issue](http://gitlab.example.com/namespace/project/issues/123)`, which is turned into
|
|
|
|
# `<a href="http://gitlab.example.com/namespace/project/issues/123">Issue</a>`
|
|
|
|
replace_link_nodes_with_href(object_class.link_reference_pattern) do |link, text|
|
|
|
|
object_link_filter(link, object_class.link_reference_pattern, link_text: text)
|
2015-11-16 20:56:28 +00:00
|
|
|
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.
|
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|
|
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-12-01 16:13:47 +00:00
|
|
|
|
2015-12-01 15:26:05 +00:00
|
|
|
data = data_attribute(
|
|
|
|
original: link_text || match,
|
|
|
|
project: project.id,
|
|
|
|
object_sym => object.id
|
|
|
|
)
|
2015-12-01 14:51:27 +00:00
|
|
|
|
|
|
|
url = matches[:url] if matches.names.include?("url")
|
|
|
|
url ||= url_for_object(object, project)
|
2015-11-30 20:14:46 +00:00
|
|
|
|
2015-12-01 15:25:56 +00:00
|
|
|
text = link_text
|
|
|
|
unless text
|
|
|
|
text = object.reference_link_text(context[:project])
|
2015-11-30 20:14:46 +00:00
|
|
|
|
2015-12-01 15:25:56 +00:00
|
|
|
extras = object_link_text_extras(object, matches)
|
|
|
|
text += " (#{extras.join(", ")})" if extras.any?
|
|
|
|
end
|
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 = []
|
|
|
|
|
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)
|
|
|
|
"#{object_class.name.titleize}: #{object.title}"
|
2015-11-16 20:56:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|