summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2015-05-14 14:37:43 +0000
committerDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2015-05-14 14:37:43 +0000
commit947231a4617fb41c809e6a0c1f222f50b6645ca3 (patch)
treeab03dca3f0acc315f3368e9709246a9e87df1bbf /lib
parent19251a1fc70522540efc2a54eb3380088d28c9ee (diff)
parent7f2fb72a0a7b92ed0947699234c1bc3fdc376973 (diff)
downloadgitlab-ce-947231a4617fb41c809e6a0c1f222f50b6645ca3.tar.gz
Merge branch 'rs-relative-link-filter' into 'master'
Minor RelativeLinkFilter cleanup See merge request !649
Diffstat (limited to 'lib')
-rw-r--r--lib/gitlab/markdown.rb4
-rw-r--r--lib/gitlab/markdown/relative_link_filter.rb44
2 files changed, 31 insertions, 17 deletions
diff --git a/lib/gitlab/markdown.rb b/lib/gitlab/markdown.rb
index cc68416d5fc..89abd521b1e 100644
--- a/lib/gitlab/markdown.rb
+++ b/lib/gitlab/markdown.rb
@@ -68,9 +68,9 @@ module Gitlab
reference_class: html_options[:class],
# RelativeLinkFilter
- ref: @ref,
+ ref: @ref,
requested_path: @path,
- project_wiki: @project_wiki
+ project_wiki: @project_wiki
}
result = pipeline.call(text, context)
diff --git a/lib/gitlab/markdown/relative_link_filter.rb b/lib/gitlab/markdown/relative_link_filter.rb
index deb302c88e1..9de2b24a9da 100644
--- a/lib/gitlab/markdown/relative_link_filter.rb
+++ b/lib/gitlab/markdown/relative_link_filter.rb
@@ -9,19 +9,18 @@ module Gitlab
# :commit
# :project
# :project_wiki
- # :requested_path
# :ref
+ # :requested_path
class RelativeLinkFilter < HTML::Pipeline::Filter
-
def call
- if linkable_files?
- doc.search('a').each do |el|
- process_link_attr el.attribute('href')
- end
-
- doc.search('img').each do |el|
- process_link_attr el.attribute('src')
- end
+ return doc unless linkable_files?
+
+ doc.search('a').each do |el|
+ process_link_attr el.attribute('href')
+ end
+
+ doc.search('img').each do |el|
+ process_link_attr el.attribute('src')
end
doc
@@ -40,6 +39,8 @@ module Gitlab
if uri.relative? && uri.path.present?
html_attr.value = rebuild_relative_uri(uri).to_s
end
+ rescue URI::Error
+ # noop
end
def rebuild_relative_uri(uri)
@@ -85,12 +86,25 @@ module Gitlab
repository.tree(current_sha, path).entries.any?
end
- # Check if the path is pointing to a directory(tree) or a file(blob)
- # eg. doc/api is directory and doc/README.md is file.
+ # Get the type of the given path
+ #
+ # path - String path to check
+ #
+ # Examples:
+ #
+ # path_type('doc/README.md') # => 'blob'
+ # path_type('doc/logo.png') # => 'raw'
+ # path_type('doc/api') # => 'tree'
+ #
+ # Returns a String
def path_type(path)
- return 'tree' if repository.tree(current_sha, path).entries.any?
- return 'raw' if repository.blob_at(current_sha, path).try(:image?)
- 'blob'
+ if repository.tree(current_sha, path).entries.any?
+ 'tree'
+ elsif repository.blob_at(current_sha, path).try(:image?)
+ 'raw'
+ else
+ 'blob'
+ end
end
def current_sha