Commit 0f331acd authored by Jerry Cheung's avatar Jerry Cheung

Merge remote-tracking branch 'benubois/relative'

Conflicts:
	README.md
parents fbb51e54 88e6614e
......@@ -85,7 +85,8 @@ filter.call
## Filters
* `MentionFilter` - replace `@user` mentions with links
* `AutolinkFilter` - auto_linking urls in HTML
* `AbsoluteSourceFilter` - replace relative image urls with fully qualified versions
* `AutoLinkFilter` - auto_linking urls in HTML
* `CamoFilter` - replace http image urls with [camo-fied](https://github.com/atmos/camo) https versions
* `EmailReplyFilter` - util filter for working with emails
* `EmojiFilter` - everyone loves [emoji](http://www.emoji-cheat-sheet.com/)!
......
......@@ -27,6 +27,7 @@ module HTML
autoload :VERSION, 'html/pipeline/version'
autoload :Pipeline, 'html/pipeline/pipeline'
autoload :Filter, 'html/pipeline/filter'
autoload :AbsoluteSourceFilter, 'html/pipeline/absolute_source_filter'
autoload :BodyContent, 'html/pipeline/body_content'
autoload :AutolinkFilter, 'html/pipeline/autolink_filter'
autoload :CamoFilter, 'html/pipeline/camo_filter'
......
require 'uri'
module HTML
class Pipeline
class AbsoluteSourceFilter < Filter
# HTML Filter for replacing relative and root relative image URLs with
# fully qualified URLs
#
# This is useful if an image is root relative but should really be going
# through a cdn, or if the content for the page assumes the host is known
# i.e. scraped webpages and some RSS feeds.
#
# Context options:
# :image_base_url - Base URL for image host for root relative src.
# :image_subpage_url - For relative src.
#
# This filter does not write additional information to the context.
# This filter would need to be run before CamoFilter.
def call
doc.search("img").each do |element|
next if element['src'].nil? || element['src'].empty?
src = element['src'].strip
unless src.start_with? 'http'
if src.start_with? '/'
base = image_base_url
else
base = image_subpage_url
end
element["src"] = URI.join(base, src).to_s
end
end
doc
end
# Private: the base url you want to use
def image_base_url
context[:image_base_url] or raise "Missing context :image_base_url for #{self.class.name}"
end
# Private: the relative url you want to use
def image_subpage_url
context[:image_subpage_url] or raise "Missing context :image_subpage_url for #{self.class.name}"
end
end
end
end
\ No newline at end of file
require "test_helper"
class HTML::Pipeline::AbsoluteSourceFilterTest < Test::Unit::TestCase
AbsoluteSourceFilter = HTML::Pipeline::AbsoluteSourceFilter
def setup
@image_base_url = 'http://assets.example.com'
@image_subpage_url = 'http://blog.example.com/a/post'
@options = {
:image_base_url => @image_base_url,
:image_subpage_url => @image_subpage_url
}
end
def test_rewrites_root_relative_urls
orig = %(<p><img src="/img.png"></p>)
puts AbsoluteSourceFilter.call(orig, @options).to_s
assert_equal "<p><img src=\"#{@image_base_url}/img.png\"></p>",
AbsoluteSourceFilter.call(orig, @options).to_s
end
def test_rewrites_root_relative_urls
orig = %(<p><img src="post/img.png"></p>)
assert_equal "<p><img src=\"#{@image_subpage_url}/img.png\"></p>",
AbsoluteSourceFilter.call(orig, @options).to_s
end
def test_does_not_rewrite_absolute_urls
orig = %(<p><img src="http://other.example.com/img.png"></p>)
result = AbsoluteSourceFilter.call(orig, @options).to_s
assert_no_match /@image_base_url/, result
assert_no_match /@image_subpage_url/, result
end
def test_fails_when_context_is_missing
assert_raise RuntimeError do
AbsoluteSourceFilter.call("<img src=\"img.png\">", {})
end
assert_raise RuntimeError do
AbsoluteSourceFilter.call("<img src=\"/img.png\">", {})
end
end
def test_tells_you_where_context_is_required
exception = assert_raise(RuntimeError) {
AbsoluteSourceFilter.call("<img src=\"img.png\">", {})
}
assert_match 'HTML::Pipeline::AbsoluteSourceFilter', exception.message
exception = assert_raise(RuntimeError) {
AbsoluteSourceFilter.call("<img src=\"/img.png\">", {})
}
assert_match 'HTML::Pipeline::AbsoluteSourceFilter', exception.message
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment