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
b3b8fc6c
Commit
b3b8fc6c
authored
Apr 14, 2015
by
Robert Speicher
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
DRY up reference filters using ReferenceFilter base class
parent
6189b24f
Changes
10
Show whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
55 additions
and
320 deletions
+55
-320
lib/gitlab/markdown/commit_range_reference_filter.rb
lib/gitlab/markdown/commit_range_reference_filter.rb
+5
-39
lib/gitlab/markdown/commit_reference_filter.rb
lib/gitlab/markdown/commit_reference_filter.rb
+5
-39
lib/gitlab/markdown/cross_project_reference.rb
lib/gitlab/markdown/cross_project_reference.rb
+5
-5
lib/gitlab/markdown/external_issue_reference_filter.rb
lib/gitlab/markdown/external_issue_reference_filter.rb
+4
-40
lib/gitlab/markdown/issue_reference_filter.rb
lib/gitlab/markdown/issue_reference_filter.rb
+6
-42
lib/gitlab/markdown/label_reference_filter.rb
lib/gitlab/markdown/label_reference_filter.rb
+5
-38
lib/gitlab/markdown/merge_request_reference_filter.rb
lib/gitlab/markdown/merge_request_reference_filter.rb
+5
-35
lib/gitlab/markdown/reference_filter.rb
lib/gitlab/markdown/reference_filter.rb
+1
-2
lib/gitlab/markdown/snippet_reference_filter.rb
lib/gitlab/markdown/snippet_reference_filter.rb
+6
-34
lib/gitlab/markdown/user_reference_filter.rb
lib/gitlab/markdown/user_reference_filter.rb
+13
-46
No files found.
lib/gitlab/markdown/commit_range_reference_filter.rb
View file @
b3b8fc6c
require
'html/pipeline'
module
Gitlab
module
Markdown
# HTML filter that replaces commit range references with links. References
# within <pre>, <code>, <a>, and <style> elements are ignored.
# HTML filter that replaces commit range references with links.
#
# This filter supports cross-project references.
#
# 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
class
CommitRangeReferenceFilter
<
ReferenceFilter
include
CrossProjectReference
# Public: Find commit range references in text
...
...
@@ -42,26 +32,10 @@ module Gitlab
# This pattern supports cross-project references.
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
doc
.
search
(
'text()'
).
each
do
|
node
|
content
=
node
.
to_html
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
)
replace_text_nodes_matching
(
COMMIT_RANGE_PATTERN
)
do
|
content
|
commit_range_link_filter
(
content
)
end
doc
end
# Replace commit range references in text with links to compare the commit
...
...
@@ -82,7 +56,7 @@ module Gitlab
url
=
url_for_commit_range
(
project
,
from_id
,
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
...
...
@@ -95,10 +69,6 @@ module Gitlab
end
end
def
validate
needs
:project
end
def
split_commit_range
(
range
)
from_id
,
to_id
=
range
.
split
(
/\.{2,3}/
,
2
)
from_id
<<
"^"
if
range
!~
/\.{3}/
...
...
@@ -118,10 +88,6 @@ module Gitlab
from:
from_id
,
to:
to_id
,
only_path:
context
[
:only_path
])
end
def
project
context
[
:project
]
end
end
end
end
lib/gitlab/markdown/commit_reference_filter.rb
View file @
b3b8fc6c
require
'html/pipeline'
module
Gitlab
module
Markdown
# HTML filter that replaces commit references with links. References within
# <pre>, <code>, <a>, and <style> elements are ignored.
# HTML filter that replaces commit references with links.
#
# This filter supports cross-project references.
#
# 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
class
CommitReferenceFilter
<
ReferenceFilter
include
CrossProjectReference
# Public: Find commit references in text
...
...
@@ -41,30 +31,10 @@ module Gitlab
# This pattern supports cross-project references.
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
doc
.
search
(
'text()'
).
each
do
|
node
|
content
=
node
.
to_html
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
doc
replace_text_nodes_matching
(
COMMIT_PATTERN
)
do
|
content
|
commit_link_filter
(
content
)
end
def
validate
needs
:project
end
# Replace commit references in text with links to the commit specified.
...
...
@@ -81,7 +51,7 @@ module Gitlab
url
=
url_for_commit
(
project
,
commit
)
title
=
commit
.
link_title
klass
=
"gfm gfm-commit
#{
context
[
:reference_class
]
}
"
.
strip
klass
=
reference_class
(
:commit
)
project_ref
+=
'@'
if
project_ref
...
...
@@ -99,10 +69,6 @@ module Gitlab
h
.
namespace_project_commit_url
(
project
.
namespace
,
project
,
commit
,
only_path:
context
[
:only_path
])
end
def
project
context
[
:project
]
end
end
end
end
lib/gitlab/markdown/cross_project_reference.rb
View file @
b3b8fc6c
module
Gitlab
module
Markdown
#
Includes shared code for reference filters that support an optional
#
cross-project
reference.
#
Common methods for ReferenceFilters that support an optional cross-project
# reference.
module
CrossProjectReference
NAMING_PATTERN
=
Gitlab
::
Regex
::
NAMESPACE_REGEX_STR
PROJECT_PATTERN
=
"(?<project>
#{
NAMING_PATTERN
}
/
#{
NAMING_PATTERN
}
)"
...
...
@@ -9,9 +9,9 @@ module Gitlab
# Given a cross-project reference string, get the Project record
#
# Defaults to value of `context[:project]` if:
#
- No reference is given
#
- Reference given doesn't exist
#
-
Reference given can't be read by the current user
#
* No reference is given OR
#
* Reference given doesn't exist OR
#
*
Reference given can't be read by the current user
#
# ref - String reference.
#
...
...
lib/gitlab/markdown/external_issue_reference_filter.rb
View file @
b3b8fc6c
require
'html/pipeline'
module
Gitlab
module
Markdown
# HTML filter that replaces external issue tracker references with links.
# References within <pre>, <code>, <a>, and <style> elements are ignored.
#
# 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
class
ExternalIssueReferenceFilter
<
ReferenceFilter
# Public: Find `JIRA-123` issue references in text
#
# ExternalIssueReferenceFilter.references_in(text) do |match, issue|
...
...
@@ -31,31 +22,10 @@ module Gitlab
# Pattern used to extract `JIRA-123` issue references from text
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
doc
.
search
(
'text()'
).
each
do
|
node
|
content
=
node
.
to_html
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
doc
replace_text_nodes_matching
(
ISSUE_PATTERN
)
do
|
content
|
issue_link_filter
(
content
)
end
def
validate
needs
:project
end
# Replace `JIRA-123` issue references in text with links to the referenced
...
...
@@ -72,7 +42,7 @@ module Gitlab
url
=
url_for_issue
(
issue
,
project
,
only_path:
context
[
:only_path
])
title
=
"Issue in
#{
project
.
external_issue_tracker
.
title
}
"
klass
=
"gfm gfm-issue
#{
context
[
:reference_class
]
}
"
.
strip
klass
=
reference_class
(
:issue
)
%(<a href="#{url}"
title="#{title}"
...
...
@@ -80,12 +50,6 @@ module Gitlab
end
end
# TODO (rspeicher): Duplicates IssueReferenceFilter
def
project
context
[
:project
]
end
# TODO (rspeicher): Duplicates IssueReferenceFilter
def
url_for_issue
(
*
args
)
IssuesHelper
.
url_for_issue
(
*
args
)
end
...
...
lib/gitlab/markdown/issue_reference_filter.rb
View file @
b3b8fc6c
require
'html/pipeline'
module
Gitlab
module
Markdown
# HTML filter that replaces issue references with links. References within
# <pre>, <code>, <a>, and <style> elements are ignored. References to issues
# that do not exist are ignored.
# HTML filter that replaces issue references with links. References to
# issues that do not exist are ignored.
#
# This filter supports cross-project references.
#
# 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
class
IssueReferenceFilter
<
ReferenceFilter
include
CrossProjectReference
# Public: Find `#123` issue references in text
...
...
@@ -40,31 +30,10 @@ module Gitlab
# This pattern supports cross-project references.
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
doc
.
search
(
'text()'
).
each
do
|
node
|
content
=
node
.
to_html
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
doc
replace_text_nodes_matching
(
ISSUE_PATTERN
)
do
|
content
|
issue_link_filter
(
content
)
end
def
validate
needs
:project
end
# Replace `#123` issue references in text with links to the referenced
...
...
@@ -79,11 +48,10 @@ module Gitlab
project
=
self
.
project_from_ref
(
project_ref
)
if
project
.
issue_exists?
(
issue
)
# FIXME: Ugly
url
=
url_for_issue
(
issue
,
project
,
only_path:
context
[
:only_path
])
title
=
"Issue:
#{
title_for_issue
(
issue
,
project
)
}
"
klass
=
"gfm gfm-issue
#{
context
[
:reference_class
]
}
"
.
strip
klass
=
reference_class
(
:issue
)
%(<a href="#{url}"
title="#{title}"
...
...
@@ -94,10 +62,6 @@ module Gitlab
end
end
def
project
context
[
:project
]
end
def
url_for_issue
(
*
args
)
IssuesHelper
.
url_for_issue
(
*
args
)
end
...
...
lib/gitlab/markdown/label_reference_filter.rb
View file @
b3b8fc6c
require
'html/pipeline'
module
Gitlab
module
Markdown
# HTML filter that replaces label references with links. References within
# <pre>, <code>, <a>, and <style> elements are ignored.
#
# 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
# HTML filter that replaces label references with links.
class
LabelReferenceFilter
<
ReferenceFilter
# Public: Find label references in text
#
# LabelReferenceFilter.references_in(text) do |match, id, name|
...
...
@@ -42,30 +33,10 @@ module Gitlab
)
}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
doc
.
search
(
'text()'
).
each
do
|
node
|
content
=
node
.
to_html
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
doc
replace_text_nodes_matching
(
LABEL_PATTERN
)
do
|
content
|
label_link_filter
(
content
)
end
def
validate
needs
:project
end
# Replace label references in text with links to the label specified.
...
...
@@ -83,7 +54,7 @@ module Gitlab
if
label
=
project
.
labels
.
find_by
(
params
)
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>)
else
...
...
@@ -118,10 +89,6 @@ module Gitlab
{
name:
name
.
tr
(
'\'"'
,
''
)
}
end
end
def
project
context
[
:project
]
end
end
end
end
lib/gitlab/markdown/merge_request_reference_filter.rb
View file @
b3b8fc6c
require
'html/pipeline'
module
Gitlab
module
Markdown
# HTML filter that replaces merge request references with links. References
# within <pre>, <code>, <a>, and <style> elements are ignored. References to
# merge requests that do not exist are ignored.
# to merge requests that do not exist are ignored.
#
# This filter supports cross-project references.
#
# 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
class
MergeRequestReferenceFilter
<
ReferenceFilter
include
CrossProjectReference
# Public: Find `!123` merge request references in text
...
...
@@ -45,25 +35,9 @@ module Gitlab
IGNORE_PARENTS
=
%w(pre code a style)
.
to_set
def
call
doc
.
search
(
'text()'
).
each
do
|
node
|
content
=
node
.
to_html
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
doc
replace_text_nodes_matching
(
MERGE_REQUEST_PATTERN
)
do
|
content
|
merge_request_link_filter
(
content
)
end
def
validate
needs
:project
end
# Replace `!123` merge request references in text with links to the
...
...
@@ -79,7 +53,7 @@ module Gitlab
if
merge_request
=
project
.
merge_requests
.
find_by
(
iid:
id
)
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
)
...
...
@@ -92,10 +66,6 @@ module Gitlab
end
end
def
project
context
[
:project
]
end
# TODO (rspeicher): Cleanup
def
url_for_merge_request
(
mr
,
project
)
h
=
Rails
.
application
.
routes
.
url_helpers
...
...
lib/gitlab/markdown/reference_filter.rb
View file @
b3b8fc6c
...
...
@@ -7,8 +7,7 @@ module Gitlab
# References within <pre>, <code>, <a>, and <style> elements are ignored.
#
# Context options:
# :project (required) - Current project, ignored when reference is
# cross-project.
# :project (required) - Current project, ignored if reference is cross-project.
# :reference_class - Custom CSS class added to reference links.
# :only_path - Generate path-only links.
#
...
...
lib/gitlab/markdown/snippet_reference_filter.rb
View file @
b3b8fc6c
require
'html/pipeline'
module
Gitlab
module
Markdown
# HTML filter that replaces snippet references with links. References within
# <pre>, <code>, <a>, and <style> elements are ignored. References to
# HTML filter that replaces snippet references with links. References to
# snippets that do not exist are ignored.
#
# 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
SnippetReferenceFilter
<
HTML
::
Pipeline
::
Filter
# This filter supports cross-project references.
class
SnippetReferenceFilter
<
ReferenceFilter
include
CrossProjectReference
# Public: Find `$123` snippet references in text
...
...
@@ -38,30 +30,10 @@ module Gitlab
# This pattern supports cross-project references.
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
doc
.
search
(
'text()'
).
each
do
|
node
|
content
=
node
.
to_html
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
)
replace_text_nodes_matching
(
SNIPPET_PATTERN
)
do
|
content
|
snippet_link_filter
(
content
)
end
doc
end
def
validate
needs
:project
end
# Replace `$123` snippet references in text with links to the referenced
...
...
@@ -77,7 +49,7 @@ module Gitlab
if
snippet
=
project
.
snippets
.
find_by
(
id:
id
)
title
=
"Snippet:
#{
snippet
.
title
}
"
klass
=
"gfm gfm-snippet
#{
context
[
:reference_class
]
}
"
.
strip
klass
=
reference_class
(
:snippet
)
url
=
url_for_snippet
(
snippet
,
project
)
...
...
lib/gitlab/markdown/user_reference_filter.rb
View file @
b3b8fc6c
require
'html/pipeline'
module
Gitlab
module
Markdown
# HTML filter that replaces user or group references with links. References
# within <pre>, <code>, <a>, and <style> elements are ignored.
# HTML filter that replaces user or group references with links.
#
# A special `@all` reference is also supported.
#
# 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
class
UserReferenceFilter
<
ReferenceFilter
# Public: Find `@user` user references in text
#
# UserReferenceFilter.references_in(text) do |match, username|
...
...
@@ -33,30 +24,10 @@ module Gitlab
# Pattern used to extract `@user` user references from text
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
doc
.
search
(
'text()'
).
each
do
|
node
|
content
=
node
.
to_html
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
doc
replace_text_nodes_matching
(
USER_PATTERN
)
do
|
content
|
user_link_filter
(
content
)
end
def
validate
needs
:project
end
# Replace `@user` user references in text with links to the referenced
...
...
@@ -70,7 +41,7 @@ module Gitlab
project
=
context
[
:project
]
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'
url
=
link_to_all
(
project
)
...
...
@@ -94,26 +65,22 @@ module Gitlab
end
end
def
project
context
[
:project
]
private
def
urls
Rails
.
application
.
routes
.
url_helpers
end
# TODO (rspeicher): Cleanup
def
group_url
(
*
args
)
h
=
Rails
.
application
.
routes
.
url_helpers
h
.
group_url
(
*
args
)
urls
.
group_url
(
*
args
)
end
# TODO (rspeicher): Cleanup
def
user_url
(
*
args
)
h
=
Rails
.
application
.
routes
.
url_helpers
h
.
user_url
(
*
args
)
urls
.
user_url
(
*
args
)
end
# TODO (rspeicher): Cleanup
def
link_to_all
(
project
)
h
=
Rails
.
application
.
routes
.
url_helpers
h
.
namespace_project_url
(
project
.
namespace
,
project
,
urls
.
namespace_project_url
(
project
.
namespace
,
project
,
only_path:
context
[
:only_path
])
end
...
...
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