Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Jérome Perrin
gitlab-ce
Commits
79fa6c64
Commit
79fa6c64
authored
Nov 16, 2015
by
Dmitriy Zaporozhets
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Remove duplication in reference filters
Signed-off-by:
Dmitriy Zaporozhets
<
dmitriy.zaporozhets@gmail.com
>
parent
adec8c77
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
119 additions
and
166 deletions
+119
-166
lib/gitlab/markdown/abstract_reference_filter.rb
lib/gitlab/markdown/abstract_reference_filter.rb
+100
-0
lib/gitlab/markdown/issue_reference_filter.rb
lib/gitlab/markdown/issue_reference_filter.rb
+7
-56
lib/gitlab/markdown/merge_request_reference_filter.rb
lib/gitlab/markdown/merge_request_reference_filter.rb
+6
-55
lib/gitlab/markdown/snippet_reference_filter.rb
lib/gitlab/markdown/snippet_reference_filter.rb
+6
-55
No files found.
lib/gitlab/markdown/abstract_reference_filter.rb
0 → 100644
View file @
79fa6c64
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
lib/gitlab/markdown/issue_reference_filter.rb
View file @
79fa6c64
...
@@ -6,66 +6,17 @@ module Gitlab
...
@@ -6,66 +6,17 @@ module Gitlab
# issues that do not exist are ignored.
# issues that do not exist are ignored.
#
#
# This filter supports cross-project references.
# This filter supports cross-project references.
class
IssueReferenceFilter
<
ReferenceFilter
class
IssueReferenceFilter
<
AbstractReferenceFilter
include
CrossProjectReference
def
self
.
object_class
Issue
# 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
end
end
# Replace `#123` issue references in text with links to the referenced
def
find_object
(
project
,
id
)
# issue's details page.
project
.
get_issue
(
id
)
#
# 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
end
end
def
url_for_
issue
(
*
args
)
def
url_for_
object
(
issue
,
project
)
IssuesHelper
.
url_for_issue
(
*
args
)
IssuesHelper
.
url_for_issue
(
issue
.
iid
,
project
,
only_path:
context
[
:only_path
]
)
end
end
end
end
end
end
...
...
lib/gitlab/markdown/merge_request_reference_filter.rb
View file @
79fa6c64
...
@@ -6,65 +6,16 @@ module Gitlab
...
@@ -6,65 +6,16 @@ module Gitlab
# to merge requests that do not exist are ignored.
# to merge requests that do not exist are ignored.
#
#
# This filter supports cross-project references.
# This filter supports cross-project references.
class
MergeRequestReferenceFilter
<
ReferenceFilter
class
MergeRequestReferenceFilter
<
AbstractReferenceFilter
include
CrossProjectReference
def
self
.
object_class
MergeRequest
# 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
end
end
# Replace `!123` merge request references in text with links to the
def
find_object
(
project
,
id
)
# referenced merge request's details page.
project
.
merge_requests
.
find_by
(
iid:
id
)
#
# 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
end
end
def
url_for_
merge_reques
t
(
mr
,
project
)
def
url_for_
objec
t
(
mr
,
project
)
h
=
Gitlab
::
Application
.
routes
.
url_helpers
h
=
Gitlab
::
Application
.
routes
.
url_helpers
h
.
namespace_project_merge_request_url
(
project
.
namespace
,
project
,
mr
,
h
.
namespace_project_merge_request_url
(
project
.
namespace
,
project
,
mr
,
only_path:
context
[
:only_path
])
only_path:
context
[
:only_path
])
...
...
lib/gitlab/markdown/snippet_reference_filter.rb
View file @
79fa6c64
...
@@ -6,65 +6,16 @@ module Gitlab
...
@@ -6,65 +6,16 @@ module Gitlab
# snippets that do not exist are ignored.
# snippets that do not exist are ignored.
#
#
# This filter supports cross-project references.
# This filter supports cross-project references.
class
SnippetReferenceFilter
<
ReferenceFilter
class
SnippetReferenceFilter
<
AbstractReferenceFilter
include
CrossProjectReference
def
self
.
object_class
Snippet
# 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
end
end
# Replace `$123` snippet references in text with links to the referenced
def
find_object
(
project
,
id
)
# snippets's details page.
project
.
snippets
.
find_by
(
id:
id
)
#
# 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
end
end
def
url_for_
snippe
t
(
snippet
,
project
)
def
url_for_
objec
t
(
snippet
,
project
)
h
=
Gitlab
::
Application
.
routes
.
url_helpers
h
=
Gitlab
::
Application
.
routes
.
url_helpers
h
.
namespace_project_snippet_url
(
project
.
namespace
,
project
,
snippet
,
h
.
namespace_project_snippet_url
(
project
.
namespace
,
project
,
snippet
,
only_path:
context
[
:only_path
])
only_path:
context
[
:only_path
])
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment