Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
李少辉-开发者
gitlab-foss
提交
b3b8fc6c
G
gitlab-foss
项目概览
李少辉-开发者
/
gitlab-foss
通知
15
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
gitlab-foss
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
b3b8fc6c
编写于
4月 14, 2015
作者:
R
Robert Speicher
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
DRY up reference filters using ReferenceFilter base class
上级
6189b24f
变更
10
隐藏空白更改
内联
并排
Showing
10 changed file
with
55 addition
and
320 deletion
+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
未找到文件。
lib/gitlab/markdown/commit_range_reference_filter.rb
浏览文件 @
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
浏览文件 @
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
)
replace_text_nodes_matching
(
COMMIT_PATTERN
)
do
|
content
|
commit_link_filter
(
content
)
end
doc
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
浏览文件 @
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
浏览文件 @
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
)
replace_text_nodes_matching
(
ISSUE_PATTERN
)
do
|
content
|
issue_link_filter
(
content
)
end
doc
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
浏览文件 @
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
)
replace_text_nodes_matching
(
ISSUE_PATTERN
)
do
|
content
|
issue_link_filter
(
content
)
end
doc
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
浏览文件 @
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
)
replace_text_nodes_matching
(
LABEL_PATTERN
)
do
|
content
|
label_link_filter
(
content
)
end
doc
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
浏览文件 @
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
)
replace_text_nodes_matching
(
MERGE_REQUEST_PATTERN
)
do
|
content
|
merge_request_link_filter
(
content
)
end
doc
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
浏览文件 @
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
浏览文件 @
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
浏览文件 @
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
)
replace_text_nodes_matching
(
USER_PATTERN
)
do
|
content
|
user_link_filter
(
content
)
end
doc
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,27 +65,23 @@ 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
,
only_path:
context
[
:only_path
])
urls
.
namespace_project_url
(
project
.
namespace
,
project
,
only_path:
context
[
:only_path
])
end
def
user_can_reference_group?
(
group
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录