提交 b3b8fc6c 编写于 作者: R Robert Speicher

DRY up reference filters using ReferenceFilter base class

上级 6189b24f
require 'html/pipeline'
module Gitlab module Gitlab
module Markdown module Markdown
# HTML filter that replaces commit range references with links. References # HTML filter that replaces commit range references with links.
# within <pre>, <code>, <a>, and <style> elements are ignored.
# #
# This filter supports cross-project references. # This filter supports cross-project references.
# class CommitRangeReferenceFilter < ReferenceFilter
# Context options:
# :project (required) - Current project, ignored when reference is
# cross-project.
# :reference_class - Custom CSS class added to reference links.
# :only_path - Generate path-only links.
#
class CommitRangeReferenceFilter < HTML::Pipeline::Filter
include CrossProjectReference include CrossProjectReference
# Public: Find commit range references in text # Public: Find commit range references in text
...@@ -42,26 +32,10 @@ module Gitlab ...@@ -42,26 +32,10 @@ module Gitlab
# This pattern supports cross-project references. # This pattern supports cross-project references.
COMMIT_RANGE_PATTERN = /(#{PROJECT_PATTERN}@)?(?<commit_range>\h{6,40}\.{2,3}\h{6,40})/ COMMIT_RANGE_PATTERN = /(#{PROJECT_PATTERN}@)?(?<commit_range>\h{6,40}\.{2,3}\h{6,40})/
# Don't look for references in text nodes that are children of these
# elements.
IGNORE_PARENTS = %w(pre code a style).to_set
def call def call
doc.search('text()').each do |node| replace_text_nodes_matching(COMMIT_RANGE_PATTERN) do |content|
content = node.to_html commit_range_link_filter(content)
next if project.nil?
next unless content.match(COMMIT_RANGE_PATTERN)
next if has_ancestor?(node, IGNORE_PARENTS)
html = commit_range_link_filter(content)
next if html == content
node.replace(html)
end end
doc
end end
# Replace commit range references in text with links to compare the commit # Replace commit range references in text with links to compare the commit
...@@ -82,7 +56,7 @@ module Gitlab ...@@ -82,7 +56,7 @@ module Gitlab
url = url_for_commit_range(project, from_id, to_id) url = url_for_commit_range(project, from_id, to_id)
title = "Commits #{from_id} through #{to_id}" title = "Commits #{from_id} through #{to_id}"
klass = "gfm gfm-commit_range #{context[:reference_class]}".strip klass = reference_class(:commit_range)
project_ref += '@' if project_ref project_ref += '@' if project_ref
...@@ -95,10 +69,6 @@ module Gitlab ...@@ -95,10 +69,6 @@ module Gitlab
end end
end end
def validate
needs :project
end
def split_commit_range(range) def split_commit_range(range)
from_id, to_id = range.split(/\.{2,3}/, 2) from_id, to_id = range.split(/\.{2,3}/, 2)
from_id << "^" if range !~ /\.{3}/ from_id << "^" if range !~ /\.{3}/
...@@ -118,10 +88,6 @@ module Gitlab ...@@ -118,10 +88,6 @@ module Gitlab
from: from_id, to: to_id, from: from_id, to: to_id,
only_path: context[:only_path]) only_path: context[:only_path])
end end
def project
context[:project]
end
end end
end end
end end
require 'html/pipeline'
module Gitlab module Gitlab
module Markdown module Markdown
# HTML filter that replaces commit references with links. References within # HTML filter that replaces commit references with links.
# <pre>, <code>, <a>, and <style> elements are ignored.
# #
# This filter supports cross-project references. # This filter supports cross-project references.
# class CommitReferenceFilter < ReferenceFilter
# Context options:
# :project (required) - Current project, ignored when reference is
# cross-project.
# :reference_class - Custom CSS class added to reference links.
# :only_path - Generate path-only links.
#
class CommitReferenceFilter < HTML::Pipeline::Filter
include CrossProjectReference include CrossProjectReference
# Public: Find commit references in text # Public: Find commit references in text
...@@ -41,30 +31,10 @@ module Gitlab ...@@ -41,30 +31,10 @@ module Gitlab
# This pattern supports cross-project references. # This pattern supports cross-project references.
COMMIT_PATTERN = /(#{PROJECT_PATTERN}@)?(?<commit>\h{6,40})/ COMMIT_PATTERN = /(#{PROJECT_PATTERN}@)?(?<commit>\h{6,40})/
# Don't look for references in text nodes that are children of these
# elements.
IGNORE_PARENTS = %w(pre code a style).to_set
def call def call
doc.search('text()').each do |node| replace_text_nodes_matching(COMMIT_PATTERN) do |content|
content = node.to_html commit_link_filter(content)
next if project.nil?
next unless content.match(COMMIT_PATTERN)
next if has_ancestor?(node, IGNORE_PARENTS)
html = commit_link_filter(content)
next if html == content
node.replace(html)
end end
doc
end
def validate
needs :project
end end
# Replace commit references in text with links to the commit specified. # Replace commit references in text with links to the commit specified.
...@@ -81,7 +51,7 @@ module Gitlab ...@@ -81,7 +51,7 @@ module Gitlab
url = url_for_commit(project, commit) url = url_for_commit(project, commit)
title = commit.link_title title = commit.link_title
klass = "gfm gfm-commit #{context[:reference_class]}".strip klass = reference_class(:commit)
project_ref += '@' if project_ref project_ref += '@' if project_ref
...@@ -99,10 +69,6 @@ module Gitlab ...@@ -99,10 +69,6 @@ module Gitlab
h.namespace_project_commit_url(project.namespace, project, commit, h.namespace_project_commit_url(project.namespace, project, commit,
only_path: context[:only_path]) only_path: context[:only_path])
end end
def project
context[:project]
end
end end
end end
end end
module Gitlab module Gitlab
module Markdown module Markdown
# Includes shared code for reference filters that support an optional # Common methods for ReferenceFilters that support an optional cross-project
# cross-project reference. # reference.
module CrossProjectReference module CrossProjectReference
NAMING_PATTERN = Gitlab::Regex::NAMESPACE_REGEX_STR NAMING_PATTERN = Gitlab::Regex::NAMESPACE_REGEX_STR
PROJECT_PATTERN = "(?<project>#{NAMING_PATTERN}/#{NAMING_PATTERN})" PROJECT_PATTERN = "(?<project>#{NAMING_PATTERN}/#{NAMING_PATTERN})"
...@@ -9,9 +9,9 @@ module Gitlab ...@@ -9,9 +9,9 @@ module Gitlab
# Given a cross-project reference string, get the Project record # Given a cross-project reference string, get the Project record
# #
# Defaults to value of `context[:project]` if: # Defaults to value of `context[:project]` if:
# - No reference is given # * No reference is given OR
# - Reference given doesn't exist # * Reference given doesn't exist OR
# - Reference given can't be read by the current user # * Reference given can't be read by the current user
# #
# ref - String reference. # ref - String reference.
# #
......
require 'html/pipeline'
module Gitlab module Gitlab
module Markdown module Markdown
# HTML filter that replaces external issue tracker references with links. # HTML filter that replaces external issue tracker references with links.
# References within <pre>, <code>, <a>, and <style> elements are ignored. class ExternalIssueReferenceFilter < ReferenceFilter
#
# Context options:
# :project (required) - Current project.
# :reference_class - Custom CSS class added to reference links.
# :only_path - Generate path-only links.
#
class ExternalIssueReferenceFilter < HTML::Pipeline::Filter
# Public: Find `JIRA-123` issue references in text # Public: Find `JIRA-123` issue references in text
# #
# ExternalIssueReferenceFilter.references_in(text) do |match, issue| # ExternalIssueReferenceFilter.references_in(text) do |match, issue|
...@@ -31,31 +22,10 @@ module Gitlab ...@@ -31,31 +22,10 @@ module Gitlab
# Pattern used to extract `JIRA-123` issue references from text # Pattern used to extract `JIRA-123` issue references from text
ISSUE_PATTERN = /(?<issue>([A-Z\-]+-)\d+)/ ISSUE_PATTERN = /(?<issue>([A-Z\-]+-)\d+)/
# Don't look for references in text nodes that are children of these
# elements.
IGNORE_PARENTS = %w(pre code a style).to_set
def call def call
doc.search('text()').each do |node| replace_text_nodes_matching(ISSUE_PATTERN) do |content|
content = node.to_html issue_link_filter(content)
next if project.nil?
next if project.default_issues_tracker?
next unless content.match(ISSUE_PATTERN)
next if has_ancestor?(node, IGNORE_PARENTS)
html = issue_link_filter(content)
next if html == content
node.replace(html)
end end
doc
end
def validate
needs :project
end end
# Replace `JIRA-123` issue references in text with links to the referenced # Replace `JIRA-123` issue references in text with links to the referenced
...@@ -72,7 +42,7 @@ module Gitlab ...@@ -72,7 +42,7 @@ module Gitlab
url = url_for_issue(issue, project, only_path: context[:only_path]) url = url_for_issue(issue, project, only_path: context[:only_path])
title = "Issue in #{project.external_issue_tracker.title}" title = "Issue in #{project.external_issue_tracker.title}"
klass = "gfm gfm-issue #{context[:reference_class]}".strip klass = reference_class(:issue)
%(<a href="#{url}" %(<a href="#{url}"
title="#{title}" title="#{title}"
...@@ -80,12 +50,6 @@ module Gitlab ...@@ -80,12 +50,6 @@ module Gitlab
end end
end end
# TODO (rspeicher): Duplicates IssueReferenceFilter
def project
context[:project]
end
# TODO (rspeicher): Duplicates IssueReferenceFilter
def url_for_issue(*args) def url_for_issue(*args)
IssuesHelper.url_for_issue(*args) IssuesHelper.url_for_issue(*args)
end end
......
require 'html/pipeline'
module Gitlab module Gitlab
module Markdown module Markdown
# HTML filter that replaces issue references with links. References within # HTML filter that replaces issue references with links. References to
# <pre>, <code>, <a>, and <style> elements are ignored. References to issues # issues that do not exist are ignored.
# that do not exist are ignored.
# #
# This filter supports cross-project references. # This filter supports cross-project references.
# class IssueReferenceFilter < ReferenceFilter
# Context options:
# :project (required) - Current project, ignored when reference is
# cross-project.
# :reference_class - Custom CSS class added to reference links.
# :only_path - Generate path-only links.
#
class IssueReferenceFilter < HTML::Pipeline::Filter
include CrossProjectReference include CrossProjectReference
# Public: Find `#123` issue references in text # Public: Find `#123` issue references in text
...@@ -40,31 +30,10 @@ module Gitlab ...@@ -40,31 +30,10 @@ module Gitlab
# This pattern supports cross-project references. # This pattern supports cross-project references.
ISSUE_PATTERN = /#{PROJECT_PATTERN}?\#(?<issue>([a-zA-Z\-]+-)?\d+)/ ISSUE_PATTERN = /#{PROJECT_PATTERN}?\#(?<issue>([a-zA-Z\-]+-)?\d+)/
# Don't look for references in text nodes that are children of these
# elements.
IGNORE_PARENTS = %w(pre code a style).to_set
def call def call
doc.search('text()').each do |node| replace_text_nodes_matching(ISSUE_PATTERN) do |content|
content = node.to_html issue_link_filter(content)
next if project.nil?
next unless project.default_issues_tracker?
next unless content.match(ISSUE_PATTERN)
next if has_ancestor?(node, IGNORE_PARENTS)
html = issue_link_filter(content)
next if html == content
node.replace(html)
end end
doc
end
def validate
needs :project
end end
# Replace `#123` issue references in text with links to the referenced # Replace `#123` issue references in text with links to the referenced
...@@ -79,11 +48,10 @@ module Gitlab ...@@ -79,11 +48,10 @@ module Gitlab
project = self.project_from_ref(project_ref) project = self.project_from_ref(project_ref)
if project.issue_exists?(issue) if project.issue_exists?(issue)
# FIXME: Ugly
url = url_for_issue(issue, project, only_path: context[:only_path]) url = url_for_issue(issue, project, only_path: context[:only_path])
title = "Issue: #{title_for_issue(issue, project)}" title = "Issue: #{title_for_issue(issue, project)}"
klass = "gfm gfm-issue #{context[:reference_class]}".strip klass = reference_class(:issue)
%(<a href="#{url}" %(<a href="#{url}"
title="#{title}" title="#{title}"
...@@ -94,10 +62,6 @@ module Gitlab ...@@ -94,10 +62,6 @@ module Gitlab
end end
end end
def project
context[:project]
end
def url_for_issue(*args) def url_for_issue(*args)
IssuesHelper.url_for_issue(*args) IssuesHelper.url_for_issue(*args)
end end
......
require 'html/pipeline'
module Gitlab module Gitlab
module Markdown module Markdown
# HTML filter that replaces label references with links. References within # HTML filter that replaces label references with links.
# <pre>, <code>, <a>, and <style> elements are ignored. class LabelReferenceFilter < ReferenceFilter
#
# Context options:
# :project (required) - Current project.
# :reference_class - Custom CSS class added to reference links.
# :only_path - Generate path-only links.
#
class LabelReferenceFilter < HTML::Pipeline::Filter
# Public: Find label references in text # Public: Find label references in text
# #
# LabelReferenceFilter.references_in(text) do |match, id, name| # LabelReferenceFilter.references_in(text) do |match, id, name|
...@@ -42,30 +33,10 @@ module Gitlab ...@@ -42,30 +33,10 @@ module Gitlab
) )
}x }x
# Don't look for references in text nodes that are children of these
# elements.
IGNORE_PARENTS = %w(pre code a style).to_set
def call def call
doc.search('text()').each do |node| replace_text_nodes_matching(LABEL_PATTERN) do |content|
content = node.to_html label_link_filter(content)
next if project.nil?
next unless content.match(LABEL_PATTERN)
next if has_ancestor?(node, IGNORE_PARENTS)
html = label_link_filter(content)
next if html == content
node.replace(html)
end end
doc
end
def validate
needs :project
end end
# Replace label references in text with links to the label specified. # Replace label references in text with links to the label specified.
...@@ -83,7 +54,7 @@ module Gitlab ...@@ -83,7 +54,7 @@ module Gitlab
if label = project.labels.find_by(params) if label = project.labels.find_by(params)
url = url_for_label(project, label) url = url_for_label(project, label)
klass = "gfm gfm-label #{context[:reference_class]}".strip klass = reference_class(:label)
%(<a href="#{url}" class="#{klass}">#{render_colored_label(label)}</a>) %(<a href="#{url}" class="#{klass}">#{render_colored_label(label)}</a>)
else else
...@@ -118,10 +89,6 @@ module Gitlab ...@@ -118,10 +89,6 @@ module Gitlab
{ name: name.tr('\'"', '') } { name: name.tr('\'"', '') }
end end
end end
def project
context[:project]
end
end end
end end
end end
require 'html/pipeline'
module Gitlab module Gitlab
module Markdown module Markdown
# HTML filter that replaces merge request references with links. References # HTML filter that replaces merge request references with links. References
# within <pre>, <code>, <a>, and <style> elements are ignored. References to # to merge requests that do not exist are ignored.
# merge requests that do not exist are ignored.
# #
# This filter supports cross-project references. # This filter supports cross-project references.
# class MergeRequestReferenceFilter < ReferenceFilter
# Context options:
# :project (required) - Current project, ignored when reference is
# cross-project.
# :reference_class - Custom CSS class added to reference links.
# :only_path - Generate path-only links.
#
class MergeRequestReferenceFilter < HTML::Pipeline::Filter
include CrossProjectReference include CrossProjectReference
# Public: Find `!123` merge request references in text # Public: Find `!123` merge request references in text
...@@ -45,25 +35,9 @@ module Gitlab ...@@ -45,25 +35,9 @@ module Gitlab
IGNORE_PARENTS = %w(pre code a style).to_set IGNORE_PARENTS = %w(pre code a style).to_set
def call def call
doc.search('text()').each do |node| replace_text_nodes_matching(MERGE_REQUEST_PATTERN) do |content|
content = node.to_html merge_request_link_filter(content)
next if project.nil?
next unless content.match(MERGE_REQUEST_PATTERN)
next if has_ancestor?(node, IGNORE_PARENTS)
html = merge_request_link_filter(content)
next if html == content
node.replace(html)
end end
doc
end
def validate
needs :project
end end
# Replace `!123` merge request references in text with links to the # Replace `!123` merge request references in text with links to the
...@@ -79,7 +53,7 @@ module Gitlab ...@@ -79,7 +53,7 @@ module Gitlab
if merge_request = project.merge_requests.find_by(iid: id) if merge_request = project.merge_requests.find_by(iid: id)
title = "Merge Request: #{merge_request.title}" title = "Merge Request: #{merge_request.title}"
klass = "gfm gfm-merge_request #{context[:reference_class]}".strip klass = reference_class(:merge_request)
url = url_for_merge_request(merge_request, project) url = url_for_merge_request(merge_request, project)
...@@ -92,10 +66,6 @@ module Gitlab ...@@ -92,10 +66,6 @@ module Gitlab
end end
end end
def project
context[:project]
end
# TODO (rspeicher): Cleanup # TODO (rspeicher): Cleanup
def url_for_merge_request(mr, project) def url_for_merge_request(mr, project)
h = Rails.application.routes.url_helpers h = Rails.application.routes.url_helpers
......
...@@ -7,8 +7,7 @@ module Gitlab ...@@ -7,8 +7,7 @@ module Gitlab
# References within <pre>, <code>, <a>, and <style> elements are ignored. # References within <pre>, <code>, <a>, and <style> elements are ignored.
# #
# Context options: # Context options:
# :project (required) - Current project, ignored when reference is # :project (required) - Current project, ignored if reference is cross-project.
# cross-project.
# :reference_class - Custom CSS class added to reference links. # :reference_class - Custom CSS class added to reference links.
# :only_path - Generate path-only links. # :only_path - Generate path-only links.
# #
......
require 'html/pipeline'
module Gitlab module Gitlab
module Markdown module Markdown
# HTML filter that replaces snippet references with links. References within # HTML filter that replaces snippet references with links. References to
# <pre>, <code>, <a>, and <style> elements are ignored. References to
# snippets that do not exist are ignored. # snippets that do not exist are ignored.
# #
# Context options: # This filter supports cross-project references.
# :project (required) - Current project, ignored when reference is class SnippetReferenceFilter < ReferenceFilter
# cross-project.
# :reference_class - Custom CSS class added to reference links.
# :only_path - Generate path-only links.
#
class SnippetReferenceFilter < HTML::Pipeline::Filter
include CrossProjectReference include CrossProjectReference
# Public: Find `$123` snippet references in text # Public: Find `$123` snippet references in text
...@@ -38,30 +30,10 @@ module Gitlab ...@@ -38,30 +30,10 @@ module Gitlab
# This pattern supports cross-project references. # This pattern supports cross-project references.
SNIPPET_PATTERN = /#{PROJECT_PATTERN}?\$(?<snippet>\d+)/ SNIPPET_PATTERN = /#{PROJECT_PATTERN}?\$(?<snippet>\d+)/
# Don't look for references in text nodes that are children of these
# elements.
IGNORE_PARENTS = %w(pre code a style).to_set
def call def call
doc.search('text()').each do |node| replace_text_nodes_matching(SNIPPET_PATTERN) do |content|
content = node.to_html snippet_link_filter(content)
next if context[:project].nil?
next unless content.match(SNIPPET_PATTERN)
next if has_ancestor?(node, IGNORE_PARENTS)
html = snippet_link_filter(content)
next if html == content
node.replace(html)
end end
doc
end
def validate
needs :project
end end
# Replace `$123` snippet references in text with links to the referenced # Replace `$123` snippet references in text with links to the referenced
...@@ -77,7 +49,7 @@ module Gitlab ...@@ -77,7 +49,7 @@ module Gitlab
if snippet = project.snippets.find_by(id: id) if snippet = project.snippets.find_by(id: id)
title = "Snippet: #{snippet.title}" title = "Snippet: #{snippet.title}"
klass = "gfm gfm-snippet #{context[:reference_class]}".strip klass = reference_class(:snippet)
url = url_for_snippet(snippet, project) url = url_for_snippet(snippet, project)
......
require 'html/pipeline'
module Gitlab module Gitlab
module Markdown module Markdown
# HTML filter that replaces user or group references with links. References # HTML filter that replaces user or group references with links.
# within <pre>, <code>, <a>, and <style> elements are ignored.
# #
# A special `@all` reference is also supported. # A special `@all` reference is also supported.
# class UserReferenceFilter < ReferenceFilter
# Context options:
# :project (required) - Current project.
# :reference_class - Custom CSS class added to reference links.
# :only_path - Generate path-only links.
#
class UserReferenceFilter < HTML::Pipeline::Filter
# Public: Find `@user` user references in text # Public: Find `@user` user references in text
# #
# UserReferenceFilter.references_in(text) do |match, username| # UserReferenceFilter.references_in(text) do |match, username|
...@@ -33,30 +24,10 @@ module Gitlab ...@@ -33,30 +24,10 @@ module Gitlab
# Pattern used to extract `@user` user references from text # Pattern used to extract `@user` user references from text
USER_PATTERN = /@(?<user>#{Gitlab::Regex::NAMESPACE_REGEX_STR})/ USER_PATTERN = /@(?<user>#{Gitlab::Regex::NAMESPACE_REGEX_STR})/
# Don't look for references in text nodes that are children of these
# elements.
IGNORE_PARENTS = %w(pre code a style).to_set
def call def call
doc.search('text()').each do |node| replace_text_nodes_matching(USER_PATTERN) do |content|
content = node.to_html user_link_filter(content)
next if project.nil?
next unless content.match(USER_PATTERN)
next if has_ancestor?(node, IGNORE_PARENTS)
html = user_link_filter(content)
next if html == content
node.replace(html)
end end
doc
end
def validate
needs :project
end end
# Replace `@user` user references in text with links to the referenced # Replace `@user` user references in text with links to the referenced
...@@ -70,7 +41,7 @@ module Gitlab ...@@ -70,7 +41,7 @@ module Gitlab
project = context[:project] project = context[:project]
self.class.references_in(text) do |match, user| self.class.references_in(text) do |match, user|
klass = "gfm gfm-project_member #{context[:reference_class]}".strip klass = reference_class(:project_member)
if user == 'all' if user == 'all'
url = link_to_all(project) url = link_to_all(project)
...@@ -94,27 +65,23 @@ module Gitlab ...@@ -94,27 +65,23 @@ module Gitlab
end end
end end
def project private
context[:project]
def urls
Rails.application.routes.url_helpers
end end
# TODO (rspeicher): Cleanup
def group_url(*args) def group_url(*args)
h = Rails.application.routes.url_helpers urls.group_url(*args)
h.group_url(*args)
end end
# TODO (rspeicher): Cleanup
def user_url(*args) def user_url(*args)
h = Rails.application.routes.url_helpers urls.user_url(*args)
h.user_url(*args)
end end
# TODO (rspeicher): Cleanup
def link_to_all(project) def link_to_all(project)
h = Rails.application.routes.url_helpers urls.namespace_project_url(project.namespace, project,
h.namespace_project_url(project.namespace, project, only_path: context[:only_path])
only_path: context[:only_path])
end end
def user_can_reference_group?(group) def user_can_reference_group?(group)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册