Skip to content

Commit

Permalink
Merge pull request #3 from gjtorikian/custom-filters
Browse files Browse the repository at this point in the history
Add support for custom filters
  • Loading branch information
gjtorikian committed Apr 1, 2014
2 parents a8ea307 + bff5c45 commit 4fd8e92
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,9 @@ html_pipeline:

Keep in mind that [filter dependencies are not bundled](https://github.com/jch/html-pipeline#dependencies),
so you'll need to add these in yourself.

## Custom filters

Custom filters can be designed [the same as in HTML::Pipeline](https://github.com/jch/html-pipeline#extending).

Check out [the test filter](./test/support/new_pipeline.rb) for an example.
7 changes: 6 additions & 1 deletion lib/jekyll-html-pipeline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ def setup
key = filter_key(f)
begin
filter = HTML::Pipeline.constants.find { |c| c.downcase == key }
HTML::Pipeline.const_get(filter)
# probably a custom filter
if filter.nil?
Jekyll::Converters.const_get(f)
else
HTML::Pipeline.const_get(filter)
end
rescue Exception => e
raise LoadError.new(e)
end
Expand Down
15 changes: 15 additions & 0 deletions test/support/new_pipeline.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require "html/pipeline"

class HelpMarkdownFilter < HTML::Pipeline::MarkdownFilter

def call
html = super

format_callout!(html)
end

def format_callout!(html)
html.gsub!(/(?:<p>)?{{#(tip|warning|error)}}(?:<\/p>)?/, '<div class="alert \1">')
html.gsub!(/(?:<p>)?{{\/(tip|warning|error)}}(?:<\/p>)?/, '</div>')
end
end
9 changes: 9 additions & 0 deletions test/test_jekyll_html_pipeline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,14 @@ class HTMLPipeline < Test::Unit::TestCase
markdown = Jekyll::Converters::Markdown.new override
assert_raise(ArgumentError) { markdown.convert(':trollface:') }
end

should "work for custom filters" do
require 'support/new_pipeline'
override = @config.dup
override['html_pipeline']['filters'] = ['HelpMarkdownFilter']
markdown = Jekyll::Converters::Markdown.new override
text = "\n {{#tip}}\n **Tip**: Wow! \n {{/tip}}"
assert_equal "<div class=\"alert tip\"><br>\n <strong>Tip</strong>: Wow! <br>\n </div>", markdown.convert(text)
end
end
end

0 comments on commit 4fd8e92

Please sign in to comment.