开发者

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 ;)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜