BigW Consortium Gitlab

Commit 79fa6c64 by Dmitriy Zaporozhets

Remove duplication in reference filters

parent adec8c77
require 'gitlab/markdown'
module Gitlab
module Markdown
# Issues, Snippets and Merge Requests shares similar functionality in refernce filtering.
# All this functionality moved to this class
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)
#
# AnyReferenceFilter.references_in(text) do |match, object|
# "<a href=...>PREFIX#{object}</a>"
# end
#
# PREFIX - symbol that detects reference (like ! for merge requests)
# object - reference object (snippet, merget request etc)
# text - String text to search.
#
# Yields the String match, the Integer referenced object ID, and an optional String
# of the external project reference.
#
# Returns a String replaced with the return of the block.
def self.references_in(text)
text.gsub(object_class.reference_pattern) do |match|
yield match, $~[object_sym].to_i, $~[:project]
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)
references_in(text) do |match, id, project_ref|
project = project_from_ref(project_ref)
if project && object = find_object(project, id)
title = escape_once("#{object_title}: #{object.title}")
klass = reference_class(object_sym)
data = data_attribute(project: project.id, object_sym => object.id)
url = url_for_object(object, project)
%(<a href="#{url}" #{data}
title="#{title}"
class="#{klass}">#{match}</a>)
else
match
end
end
end
def object_title
object_class.name.titleize
end
end
end
end
......@@ -6,66 +6,17 @@ module Gitlab
# issues that do not exist are ignored.
#
# This filter supports cross-project references.
class IssueReferenceFilter < ReferenceFilter
include CrossProjectReference
# Public: Find `#123` issue references in text
#
# IssueReferenceFilter.references_in(text) do |match, issue, project_ref|
# "<a href=...>##{issue}</a>"
# end
#
# text - String text to search.
#
# Yields the String match, the Integer issue ID, and an optional String of
# the external project reference.
#
# Returns a String replaced with the return of the block.
def self.references_in(text)
text.gsub(Issue.reference_pattern) do |match|
yield match, $~[:issue].to_i, $~[:project]
end
end
def self.referenced_by(node)
{ issue: LazyReference.new(Issue, node.attr("data-issue")) }
end
def call
replace_text_nodes_matching(Issue.reference_pattern) do |content|
issue_link_filter(content)
end
class IssueReferenceFilter < AbstractReferenceFilter
def self.object_class
Issue
end
# Replace `#123` issue references in text with links to the referenced
# issue's details page.
#
# text - String text to replace references in.
#
# Returns a String with `#123` references replaced with links. All links
# have `gfm` and `gfm-issue` class names attached for styling.
def issue_link_filter(text)
self.class.references_in(text) do |match, id, project_ref|
project = self.project_from_ref(project_ref)
if project && issue = project.get_issue(id)
url = url_for_issue(id, project, only_path: context[:only_path])
title = escape_once("Issue: #{issue.title}")
klass = reference_class(:issue)
data = data_attribute(project: project.id, issue: issue.id)
%(<a href="#{url}" #{data}
title="#{title}"
class="#{klass}">#{match}</a>)
else
match
end
end
def find_object(project, id)
project.get_issue(id)
end
def url_for_issue(*args)
IssuesHelper.url_for_issue(*args)
def url_for_object(issue, project)
IssuesHelper.url_for_issue(issue.iid, project, only_path: context[:only_path])
end
end
end
......
......@@ -6,65 +6,16 @@ module Gitlab
# to merge requests that do not exist are ignored.
#
# This filter supports cross-project references.
class MergeRequestReferenceFilter < ReferenceFilter
include CrossProjectReference
# Public: Find `!123` merge request references in text
#
# MergeRequestReferenceFilter.references_in(text) do |match, merge_request, project_ref|
# "<a href=...>##{merge_request}</a>"
# end
#
# text - String text to search.
#
# Yields the String match, the Integer merge request ID, and an optional
# String of the external project reference.
#
# Returns a String replaced with the return of the block.
def self.references_in(text)
text.gsub(MergeRequest.reference_pattern) do |match|
yield match, $~[:merge_request].to_i, $~[:project]
end
end
def self.referenced_by(node)
{ merge_request: LazyReference.new(MergeRequest, node.attr("data-merge-request")) }
end
def call
replace_text_nodes_matching(MergeRequest.reference_pattern) do |content|
merge_request_link_filter(content)
end
class MergeRequestReferenceFilter < AbstractReferenceFilter
def self.object_class
MergeRequest
end
# Replace `!123` merge request references in text with links to the
# referenced merge request's details page.
#
# text - String text to replace references in.
#
# Returns a String with `!123` references replaced with links. All links
# have `gfm` and `gfm-merge_request` class names attached for styling.
def merge_request_link_filter(text)
self.class.references_in(text) do |match, id, project_ref|
project = self.project_from_ref(project_ref)
if project && merge_request = project.merge_requests.find_by(iid: id)
title = escape_once("Merge Request: #{merge_request.title}")
klass = reference_class(:merge_request)
data = data_attribute(project: project.id, merge_request: merge_request.id)
url = url_for_merge_request(merge_request, project)
%(<a href="#{url}" #{data}
title="#{title}"
class="#{klass}">#{match}</a>)
else
match
end
end
def find_object(project, id)
project.merge_requests.find_by(iid: id)
end
def url_for_merge_request(mr, project)
def url_for_object(mr, project)
h = Gitlab::Application.routes.url_helpers
h.namespace_project_merge_request_url(project.namespace, project, mr,
only_path: context[:only_path])
......
......@@ -6,65 +6,16 @@ module Gitlab
# snippets that do not exist are ignored.
#
# This filter supports cross-project references.
class SnippetReferenceFilter < ReferenceFilter
include CrossProjectReference
# Public: Find `$123` snippet references in text
#
# SnippetReferenceFilter.references_in(text) do |match, snippet|
# "<a href=...>$#{snippet}</a>"
# end
#
# text - String text to search.
#
# Yields the String match, the Integer snippet ID, and an optional String
# of the external project reference.
#
# Returns a String replaced with the return of the block.
def self.references_in(text)
text.gsub(Snippet.reference_pattern) do |match|
yield match, $~[:snippet].to_i, $~[:project]
end
end
def self.referenced_by(node)
{ snippet: LazyReference.new(Snippet, node.attr("data-snippet")) }
end
def call
replace_text_nodes_matching(Snippet.reference_pattern) do |content|
snippet_link_filter(content)
end
class SnippetReferenceFilter < AbstractReferenceFilter
def self.object_class
Snippet
end
# Replace `$123` snippet references in text with links to the referenced
# snippets's details page.
#
# text - String text to replace references in.
#
# Returns a String with `$123` references replaced with links. All links
# have `gfm` and `gfm-snippet` class names attached for styling.
def snippet_link_filter(text)
self.class.references_in(text) do |match, id, project_ref|
project = self.project_from_ref(project_ref)
if project && snippet = project.snippets.find_by(id: id)
title = escape_once("Snippet: #{snippet.title}")
klass = reference_class(:snippet)
data = data_attribute(project: project.id, snippet: snippet.id)
url = url_for_snippet(snippet, project)
%(<a href="#{url}" #{data}
title="#{title}"
class="#{klass}">#{match}</a>)
else
match
end
end
def find_object(project, id)
project.snippets.find_by(id: id)
end
def url_for_snippet(snippet, project)
def url_for_object(snippet, project)
h = Gitlab::Application.routes.url_helpers
h.namespace_project_snippet_url(project.namespace, project, snippet,
only_path: context[:only_path])
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment