2015-10-24 18:46:33 +05:30
|
|
|
require 'uri'
|
|
|
|
|
2015-12-23 02:04:40 +05:30
|
|
|
module Banzai
|
|
|
|
module Filter
|
2015-10-24 18:46:33 +05:30
|
|
|
# HTML filter that "fixes" relative upload links to files.
|
|
|
|
# Context options:
|
|
|
|
# :project (required) - Current project
|
|
|
|
#
|
|
|
|
class UploadLinkFilter < HTML::Pipeline::Filter
|
|
|
|
def call
|
2016-06-02 11:05:42 +05:30
|
|
|
return doc unless project
|
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
doc.xpath('descendant-or-self::a[starts-with(@href, "/uploads/")]').each do |el|
|
2015-10-24 18:46:33 +05:30
|
|
|
process_link_attr el.attribute('href')
|
|
|
|
end
|
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
doc.xpath('descendant-or-self::img[starts-with(@src, "/uploads/")]').each do |el|
|
2015-10-24 18:46:33 +05:30
|
|
|
process_link_attr el.attribute('src')
|
|
|
|
end
|
|
|
|
|
|
|
|
doc
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def process_link_attr(html_attr)
|
2016-06-22 15:30:34 +05:30
|
|
|
html_attr.value = build_url(html_attr.value).to_s
|
2015-10-24 18:46:33 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def build_url(uri)
|
2016-06-02 11:05:42 +05:30
|
|
|
File.join(Gitlab.config.gitlab.url, project.path_with_namespace, uri)
|
|
|
|
end
|
|
|
|
|
|
|
|
def project
|
|
|
|
context[:project]
|
2015-10-24 18:46:33 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
# Ensure that a :project key exists in context
|
|
|
|
#
|
|
|
|
# Note that while the key might exist, its value could be nil!
|
|
|
|
def validate
|
|
|
|
needs :project
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|