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
Léo-Paul Géneau
gitlab-ce
Commits
aa24feed
Commit
aa24feed
authored
May 16, 2017
by
Douwe Maan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor DependencyLinker::BaseLinker
parent
52527be4
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
71 additions
and
62 deletions
+71
-62
lib/gitlab/dependency_linker/base_linker.rb
lib/gitlab/dependency_linker/base_linker.rb
+9
-49
lib/gitlab/dependency_linker/gemfile_linker.rb
lib/gitlab/dependency_linker/gemfile_linker.rb
+13
-10
lib/gitlab/dependency_linker/method_linker.rb
lib/gitlab/dependency_linker/method_linker.rb
+48
-0
spec/lib/gitlab/dependency_linker/gemfile_linker_spec.rb
spec/lib/gitlab/dependency_linker/gemfile_linker_spec.rb
+1
-1
spec/lib/gitlab/highlight_spec.rb
spec/lib/gitlab/highlight_spec.rb
+0
-2
No files found.
lib/gitlab/dependency_linker/base_linker.rb
View file @
aa24feed
module
Gitlab
module
DependencyLinker
class
BaseLinker
URL_REGEX
=
%r{https?://[^'"]+}
.
freeze
class_attribute
:file_type
def
self
.
support?
(
blob_name
)
...
...
@@ -26,59 +28,17 @@ module Gitlab
private
def
package_url
(
name
)
raise
NotImplementedError
end
def
link_dependencies
raise
NotImplementedError
end
def
package_link
(
name
,
url
=
package_url
(
name
))
return
name
unless
url
%{<a href="#{ERB::Util.html_escape_once(url)}" rel="noopener noreferrer" target="_blank">#{ERB::Util.html_escape_once(name)}</a>}
end
# Links package names in a method call or assignment string argument.
#
# Example:
# link_method_call("gem")
# # Will link `package` in `gem "package"`, `gem("package")` and `gem = "package"`
#
# link_method_call("gem", "specific_package")
# # Will link `specific_package` in `gem "specific_package"`
#
# link_method_call("github", /[^\/]+\/[^\/]+/)
# # Will link `user/repo` in `github "user/repo"`, but not `github "package"`
#
# link_method_call(%w[add_dependency add_development_dependency])
# # Will link `spec.add_dependency "package"` and `spec.add_development_dependency "package"`
#
# link_method_call("name")
# # Will link `package` in `self.name = "package"`
def
link_method_call
(
method_names
,
value
=
nil
,
&
url_proc
)
value
=
case
value
when
String
Regexp
.
escape
(
value
)
when
nil
/[^'"]+/
else
value
def
license_url
(
name
)
Licensee
::
License
.
find
(
name
)
&
.
url
end
method_names
=
Array
(
method_names
).
map
{
|
name
|
Regexp
.
escape
(
name
)
}
regex
=
%r{
#{
Regexp
.
union
(
method_names
)
}
# Method name
\s
* # Whitespace
[(=]? # Opening brace or equals sign
\s
* # Whitespace
['"](?<name>
#{
value
}
)['"] # Package name in quotes
}x
link_regex
(
regex
,
&
url_proc
)
def
link_tag
(
name
,
url
)
%{<a href="#{ERB::Util.html_escape_once(url)}" rel="nofollow noreferrer noopener" target="_blank">#{ERB::Util.html_escape_once(name)}</a>}
end
# Links package names based on regex.
...
...
@@ -86,13 +46,13 @@ module Gitlab
# Example:
# link_regex(/(github:|:github =>)\s*['"](?<name>[^'"]+)['"]/)
# # Will link `user/repo` in `github: "user/repo"` or `:github => "user/repo"`
def
link_regex
(
regex
)
def
link_regex
(
regex
,
&
url_proc
)
highlighted_lines
.
map!
.
with_index
do
|
rich_line
,
i
|
marker
=
StringRegexMarker
.
new
(
plain_lines
[
i
],
rich_line
.
html_safe
)
marker
.
mark
(
regex
,
group: :name
)
do
|
text
,
left
:,
right
:|
url
=
block_given?
?
yield
(
text
)
:
package_url
(
text
)
package_link
(
text
,
url
)
url
=
yield
(
text
)
url
?
link_tag
(
text
,
url
)
:
text
end
end
end
...
...
lib/gitlab/dependency_linker/gemfile_linker.rb
View file @
aa24feed
module
Gitlab
module
DependencyLinker
class
GemfileLinker
<
Base
Linker
class
GemfileLinker
<
Method
Linker
self
.
file_type
=
:gemfile
private
def
link_dependencies
# Link `gem "package_name"` to https://rubygems.org/gems/package_name
link_method_call
(
"gem"
)
link_urls
link_packages
end
def
link_urls
# Link `github: "user/repo"` to https://github.com/user/repo
link_regex
(
/(github:|:github\s*=>)\s*['"](?<name>[^'"]+)['"]/
)
do
|
name
|
"https://github.com/
#{
name
}
"
end
link_regex
(
/(github:|:github\s*=>)\s*['"](?<name>[^'"]+)['"]/
,
&
method
(
:github_url
))
# Link `git: "https://gitlab.example.com/user/repo"` to https://gitlab.example.com/user/repo
link_regex
(
%r{(git:|:git
\s
*=>)
\s
*['"](?<name>
https?://[^'"]+)['"]}
)
{
|
url
|
url
}
link_regex
(
%r{(git:|:git
\s
*=>)
\s
*['"](?<name>
#{
URL_REGEX
}
)['"]}
,
&
:itself
)
# Link `source "https://rubygems.org"` to https://rubygems.org
link_method_call
(
"source"
,
%r{https?://[^'"]+}
)
{
|
url
|
url
}
link_method_call
(
'source'
,
URL_REGEX
,
&
:itself
)
end
def
package_url
(
name
)
def
link_packages
# Link `gem "package_name"` to https://rubygems.org/gems/package_name
link_method_call
(
'gem'
)
do
|
name
|
"https://rubygems.org/gems/
#{
name
}
"
end
end
end
end
end
lib/gitlab/dependency_linker/method_linker.rb
0 → 100644
View file @
aa24feed
module
Gitlab
module
DependencyLinker
class
MethodLinker
<
BaseLinker
private
# Links package names in a method call or assignment string argument.
#
# Example:
# link_method_call('gem')
# # Will link `package` in `gem "package"`, `gem("package")` and `gem = "package"`
#
# link_method_call('gem', 'specific_package')
# # Will link `specific_package` in `gem "specific_package"`
#
# link_method_call('github', /[^\/"]+\/[^\/"]+/)
# # Will link `user/repo` in `github "user/repo"`, but not `github "package"`
#
# link_method_call(%w[add_dependency add_development_dependency])
# # Will link `spec.add_dependency "package"` and `spec.add_development_dependency "package"`
#
# link_method_call('name')
# # Will link `package` in `self.name = "package"`
def
link_method_call
(
method_names
,
value
=
nil
,
&
url_proc
)
method_names
=
Array
(
method_names
).
map
{
|
name
|
Regexp
.
escape
(
name
)
}
value
=
case
value
when
String
Regexp
.
escape
(
value
)
when
nil
/[^'"]+/
else
value
end
regex
=
%r{
#{
Regexp
.
union
(
method_names
)
}
# Method name
\s
* # Whitespace
[(=]? # Opening brace or equals sign
\s
* # Whitespace
['"](?<name>
#{
value
}
)['"] # Package name in quotes
}x
link_regex
(
regex
,
&
url_proc
)
end
end
end
end
spec/lib/gitlab/dependency_linker/gemfile_linker_spec.rb
View file @
aa24feed
...
...
@@ -33,7 +33,7 @@ describe Gitlab::DependencyLinker::GemfileLinker, lib: true do
subject
{
Gitlab
::
Highlight
.
highlight
(
file_name
,
file_content
)
}
def
link
(
name
,
url
)
%{<a href="#{url}" rel="no
opener noreferr
er" target="_blank">#{name}</a>}
%{<a href="#{url}" rel="no
follow noreferrer noopen
er" target="_blank">#{name}</a>}
end
it
'links sources'
do
...
...
spec/lib/gitlab/highlight_spec.rb
View file @
aa24feed
...
...
@@ -59,8 +59,6 @@ describe Gitlab::Highlight, lib: true do
end
describe
'#highlight'
do
subject
{
described_class
.
highlight
(
file_name
,
file_content
,
nowrap:
false
)
}
it
'links dependencies via DependencyLinker'
do
expect
(
Gitlab
::
DependencyLinker
).
to
receive
(
:link
).
with
(
'file.name'
,
'Contents'
,
anything
).
and_call_original
...
...
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