Dimitar Vouldjeff
Dimitar Vouldjeff

Reputation: 2107

Extend RedCloth gem

I want to put a new textile tag like h1. , but its name will be map.

So I found the following article but it doesn`t work on RedCloth 4.2.2

Thanks

Upvotes: 1

Views: 1235

Answers (1)

agregoire
agregoire

Reputation: 2052

There's an example on how to use custom tags in RedCloth's specs. Basically, you put the new method you need in a module, and you pass it to the extend method of your RedCloth object.

Here's a quick example of a custom tag that puts the text it's called with in a span:

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"

If you want to use this in a Rails project, you might want to override ActionView's textilize text helper so that it extends RedCloth with your custom tag.

Upvotes: 3

Related Questions