How to write a HAML custom filter?
I have written a simple custom filter in HAML but I cannot get my Rails app to find it.
The filter looks like this:
# lib/haml/filters/gfmarkdown.rb
module Haml::Filters::Gfmarkdown
include Haml::Fil开发者_如何学Goters::Base
# copied from Haml::Filters::Markdown
lazy_require 'rdiscount', 'peg_markdown', 'maruku', 'bluecloth'
def render(text)
# copied from Haml::Filters::Markdown
engine = case @required
when 'rdiscount'
::RDiscount
when 'peg_markdown'
::PEGMarkdown
when 'maruku'
::Maruku
when 'bluecloth'
::BlueCloth
end
engine.new(gfm text).to_html # gfm method defined elsewhere
end
end
In my views, I try to use this filter like so:
:gfmarkdown
#{comment.body}
But I get this error message: Filter "gfmarkdown" is not defined.
Any ideas how I can get my Rails app to pick up my filter?
Versions: Rails 2.3.4, Haml 2.2.15.
I have found the solution: add an initializer which requires the filter.
# config/initializers/gfmarkdown.rb
require 'haml/filters/gfmarkdown'
I had thought that modules in lib/
were auto-required if the module hierarchy matched the file path. Oh well ;)
精彩评论