Ruby on Rails
RedCloth3

This page covers problems and workarounds for RedCloth 3. RedCloth 4 has been released, so try that if you’re experiencing problems with 3.x.

Installing the old RedCloth 3.0.3

If you you are using Rails v1.0-1.2.5 (and probably 2.0), make sure you are using RedCloth 3.0.3 instead of 3.0.4. You can then use textilize() correctly. See below for attempted fixes for the result if you don’t do this!

You can do this by running

gem install RedCloth --version "3.0.3" 
gem uninstall RedCloth --version "3.0.4"

Textile Rule for Glyphs

According to the RedCloth attribute list on setting rules for Textile processing, the rule for processing glyphs is called “inline_textile_glyphs”. This is incorrect. The actual rule in RedCloth 3.0.4 is “glyphs_textile”.

Problems (Redcloth 3.0.4 with Rails 1.0)

Solution: use 3.0.3 or one of the below fixes.

In theory you should be able to use the ActionView texthelper textilize which is documented in http://rails.rubyonrails.com/classes/ActionView/Helpers/TextHelper.html#M000422. However this does some strange things with newlines. For example the following

textilize(“h1. This is a test of textile\n\nParagraph\n\nAnother paragraph\n\n* Bullets”)

produces

This is a test of textile

Paragraph

Another paragraph


  • Bullets

This was because RedCloth 3.0.4’s support for hard breaks was broken. It was fixed but never released.

The solution

If you want hard breaks to be on (that is, one newline is a <br/> and two makes a paragraph), turn on the :hard_breaks option, but you also need to work around a bug that was fixed in May, 2006:


class RedCloth
# Patch for RedCloth. Fixed in RedCloth r128 but why hasn’t released it yet.
# http://code.whytheluckystiff.net/redcloth/changeset/128
def hard
break( text )
text.gsub!( /(.)\n(?!\n|\Z| ([#=]+(\s|$)|[{|]))/, “\\1
” ) if hard_breaks
end
end

Or just install RedCloth 3.0.3 or 4.0

—-

I’m running 1.1.1 and RedCloth 3.0.3 and the textilize method doesn’t seem to work at all for me. I was getting undefined local variable exceptions until I upgraded to 3.0.4, at which time I still experienced the same display issues notwithstanding any of the fixes described here. This is growing to be quite a mess.

RedCloth 3.0.4 doesn’t help. But include the fix-lines above (see The solution if you want hard breaks) in your rails project and you’ll be fine (posted here again):


class RedCloth
# Patch for RedCloth. Fixed in RedCloth r128 but why hasn’t released it yet.
# http://code.whytheluckystiff.net/redcloth/changeset/128
def hard
break( text )
text.gsub!( /(.)\n(?!\n|\Z| ([#=]+(\s|$)|[{|]))/, “\\1
” ) if hard_breaks
end
end

RedCloth 3.0.4 with Rails 1.2.5
I found I had to merge the ideas mentioned above to get both hard breaks and tables to work.
Basically, I did two things:

  1. — defined my own textilize method in application_helper.rb that called RedCloth.new with the parameter :hard_breaks:

    def textilize(text) RedCloth.new(text, [:hard_breaks]).to_html end

    and I also stuck the hard_break method into my environment.rb

    class RedCloth # Patch for RedCloth. Fixed in RedCloth r128 but _why hasn't released it yet. # <a href="http://code.whytheluckystiff.net/redcloth/changeset/128">http://code.whytheluckystiff.net/redcloth/changeset/128</a> def hard_break( text ) text.gsub!( /(.)\n(?!\n|\Z| *([#*=]+(\s|$)|[{|]))/, "\\1<br />" ) if hard_breaks end end