summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakub Jirutka <jakub@jirutka.cz>2015-05-11 20:43:18 +0200
committerJakub Jirutka <jakub@jirutka.cz>2015-05-11 20:43:18 +0200
commitb3276661f7c5077c1254b5463bb16c5e46b01abf (patch)
treed4e5a4adbe52cccf42423b6329085281ec45c509
parentf7adac87fe824a453197f0be547c35684d468dd7 (diff)
downloadgitlab-ce-b3276661f7c5077c1254b5463bb16c5e46b01abf.tar.gz
RelativeLinkFilter: refactor according to suggestions by @tsigo
-rw-r--r--lib/gitlab/markdown.rb1
-rw-r--r--lib/gitlab/markdown/relative_link_filter.rb29
2 files changed, 13 insertions, 17 deletions
diff --git a/lib/gitlab/markdown.rb b/lib/gitlab/markdown.rb
index dadd0e0d64b..cc68416d5fc 100644
--- a/lib/gitlab/markdown.rb
+++ b/lib/gitlab/markdown.rb
@@ -98,7 +98,6 @@ module Gitlab
Gitlab::Markdown::SanitizationFilter,
Gitlab::Markdown::RelativeLinkFilter,
-
Gitlab::Markdown::EmojiFilter,
Gitlab::Markdown::TableOfContentsFilter,
Gitlab::Markdown::AutolinkFilter,
diff --git a/lib/gitlab/markdown/relative_link_filter.rb b/lib/gitlab/markdown/relative_link_filter.rb
index 084f84b25f7..deb302c88e1 100644
--- a/lib/gitlab/markdown/relative_link_filter.rb
+++ b/lib/gitlab/markdown/relative_link_filter.rb
@@ -1,3 +1,4 @@
+require 'html/pipeline/filter'
require 'uri'
module Gitlab
@@ -13,7 +14,7 @@ module Gitlab
class RelativeLinkFilter < HTML::Pipeline::Filter
def call
- if !project_wiki && repository.try(:exists?) && !repository.empty?
+ if linkable_files?
doc.search('a').each do |el|
process_link_attr el.attribute('href')
end
@@ -28,6 +29,10 @@ module Gitlab
protected
+ def linkable_files?
+ context[:project_wiki].nil? && repository.try(:exists?) && !repository.empty?
+ end
+
def process_link_attr(html_attr)
return if html_attr.blank?
@@ -42,7 +47,7 @@ module Gitlab
uri.path = [
relative_url_root,
- project.path_with_namespace,
+ context[:project].path_with_namespace,
path_type(file_path),
ref || 'master', # assume that if no ref exists we can point to master
file_path
@@ -52,7 +57,7 @@ module Gitlab
end
def relative_file_path(path)
- nested_path = build_nested_path(path, requested_path)
+ nested_path = build_nested_path(path, context[:requested_path])
file_exists?(nested_path) ? nested_path : path
end
@@ -89,28 +94,20 @@ module Gitlab
end
def current_sha
- if commit
- commit.id
- elsif ref
- repository.commit(ref).try(:sha)
- else
- repository.head_commit.sha
- end
+ context[:commit].try(:id) ||
+ ref ? repository.commit(ref).try(:sha) : repository.head_commit.sha
end
def relative_url_root
Gitlab.config.gitlab.relative_url_root.presence || '/'
end
- [:commit, :project, :project_wiki, :requested_path, :ref].each do |name|
- define_method(name) do
- context[name]
- end
+ def ref
+ context[:ref]
end
def repository
- return if project.nil?
- project.repository
+ context[:project].try(:repository)
end
end
end