From 417f8bb74cecca7e27a10fd85f0e657177dfce88 Mon Sep 17 00:00:00 2001 From: Ben Ubois Date: Wed, 28 Nov 2012 00:07:42 -0800 Subject: [PATCH 1/3] Added an example of a custom filter. --- README.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/README.md b/README.md index 113706c3..28690d06 100644 --- a/README.md +++ b/README.md @@ -151,6 +151,36 @@ EmojiPipeline = Pipeline.new [ ], context, {} ``` +## Extending +If you need a custom filter, you can write your own. All that is needed is a +class that inherits from `HTML::Pipeline::Filter` and has a `call` method. For +example this filter adds a base url to images that are root relative: + +```ruby +require 'uri' + +class RootRelativeFilter < HTML::Pipeline::Filter + + def call + doc.search("img").each do |img| + next if img['src'].nil? + src = img['src'].strip + if src.start_with? '/' + img["src"] = URI.join(context[:base_url], src).to_s + end + end + doc + end + +end +``` + +Now this filter can be used in a pipeline: + +```ruby +Pipeline.new [ ImageSourceFilter ], { :base_url => 'http://somehost.com' } +``` + ## Development To see what has changed in recent versions, see the [CHANGELOG](https://github.com/jch/html-pipeline/blob/master/CHANGELOG.md). From 55560db7feb2cc53a21e3432e8916f148972e831 Mon Sep 17 00:00:00 2001 From: Ben Ubois Date: Wed, 28 Nov 2012 00:11:08 -0800 Subject: [PATCH 2/3] Fixed typo in example. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 28690d06..590469ad 100644 --- a/README.md +++ b/README.md @@ -178,7 +178,7 @@ end Now this filter can be used in a pipeline: ```ruby -Pipeline.new [ ImageSourceFilter ], { :base_url => 'http://somehost.com' } +Pipeline.new [ RootRelativeFilter ], { :base_url => 'http://somehost.com' } ``` ## Development From 2d8b046f652b2e0fd54e30483f028718ff7b0a28 Mon Sep 17 00:00:00 2001 From: Ben Ubois Date: Wed, 28 Nov 2012 01:09:50 -0800 Subject: [PATCH 3/3] Extending example copy edits. --- README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 590469ad..f86f6786 100644 --- a/README.md +++ b/README.md @@ -152,9 +152,10 @@ EmojiPipeline = Pipeline.new [ ``` ## Extending -If you need a custom filter, you can write your own. All that is needed is a -class that inherits from `HTML::Pipeline::Filter` and has a `call` method. For -example this filter adds a base url to images that are root relative: +To write a custom filter, you need a class with a `call` method that inherits +from `HTML::Pipeline::Filter`. + +For example this filter adds a base url to images that are root relative: ```ruby require 'uri'