Software Development | Ruby on Rails
Text-to-HTLM translators: RedCloth and bluecloth

Both Textile and Markdown are text-to-HTML translators. They generate HTML mark-up from simple markup text  usually entered via a web form. In both systems the text includes syntax to generate some or all of headings, bold text, emphasised text, bulleted lists, tables, etc.

RedCloth Ruby Gem

The RedCloth Ruby Gem gives Ruby Texile support.

bluecloth Ruby Gem

The BlueCloth Ruby Gem gives Ruby Markdown support. It seems that Markdown is better at converting existing text and into HTML without any manual cleanup (Ross Belmont: Installing BlueCloth). For example when copy-pasting text from an e-mail to post online.

Ryan Tomayko: Moving Past BlueCloth is pretty scathing of BlueCloth on performance and stability grounds. He offers two replacement gems (rdiscount and rpeg-markdown). RDiscount is in turn based on Discount.

Mislav Marohnic ported BlueCloth to GitHub to enable him to fix some of the issues. He has since abandoned that repo for bluecloth 2.0 and adopted Discount. Apparently bluecloth 2.0 is available at Deveiate: BlueCloth but the server never responded when I went looking.

Some people have experienced problems installing BlueCloth 2.0.5 (Ruby Talk: bluecloth 2.0.5 installation error on Windows).

altered_beast and text-to-HTML translation

The altered_beast installation brings in both the BlueCloth Ruby Gem, for Markdown support, and the RedCloth Ruby Gem, for Texile support. But by default only RedCloth is used.

RedCloth is mentioned in lib\html_formatting.rb. If you want to use something else, and it doesn't have to be BlueCloth then this is the code to change:

lib\html_formatting.rb

require 'redcloth' 
module HtmlFormatting 
  protected 

  def format_attributes 
    self.class.formatted_attributes.each do |attr| 
      raw = read_attribute attr 
      linked = auto_link(raw) { |text| truncate(text, :length => 50) } 
      textilized = ::RedCloth.new(linked, [:hard_breaks]) 
      textilized.hard_breaks = true if textilized.respond_to?("hard_breaks=") 
      write_attribute "#{attr}_html", white_list_sanitizer.sanitize(textilized.to_html) 
    end 
  end 
end 

By default only three fields in altered_beast use formats_attributes and hence use RedCloth:

  • user.bio
  • forum.description
  • post.body

Just as an example this the code within models/user.rb that what makes the bio field use RedCloth:

app/models/user.rb (snippet)

formats_attributes :bio

Pretty simple really.