Skip to content

Commit

Permalink
Merge pull request #19 from benubois/extending_doc
Browse files Browse the repository at this point in the history
Added an example of a custom filter. [skip ci]
  • Loading branch information
jch committed Nov 29, 2012
2 parents 41ed791 + 2d8b046 commit 9d3e97d
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,37 @@ EmojiPipeline = Pipeline.new [
], context, {}
```

## Extending
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'

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 [ RootRelativeFilter ], { :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).
Expand Down

0 comments on commit 9d3e97d

Please sign in to comment.