2010-03-13 77 views

回答

3

有一个关于如何在RedCloth's specs使用自定义标签的例子。基本上,你把你需要的新方法放到一个模块中,然后传递给你的RedCloth对象的扩展方法。

下面是把它称为在一个跨距中的文本的自定义标签的一个简单的例子:

module MappingExtension 
    def map(opts) 
    html = %Q{<span class="map">#{opts[:text]}</span>\n} 
    end 
end 

require 'redcloth' 

text = "The next line will contain a map:\n\nmap. map" 
r = RedCloth.new text 
r.extend MappingExtension 

r.to_html 

# "<p>The next line will contain a map:</p>\n<span class="map">map</span>\n" 

如果你想在Rails项目中使用这个,你可能希望覆盖的ActionView的textilize文本帮手以便它可以使用您的自定义标签来扩展RedCloth。

+0

谢谢,这帮助了很多......但是,增加新的扩展http://github.com/jgarber/redcloth/blob/3a0ab611933c97aef288acf5480f7c3656c9b2c1/spec/extension_spec.rb不'吨的工作... – 2010-03-16 08:39:45

+0

我的意思是将它们结合起来 – 2010-03-16 08:56:31

+0

你可以编辑你的问题来举例说明什么在你的代码中不起作用吗?您链接的规格在我的机器上按预期工作。 – agregoire 2010-03-16 13:49:33