Textilize Alternative

The textilize methods in Rails has never really been what I wanted. When it wasn’t adding paragraphs it was messing with my line brakes but it made such a good job of making HTML and XHTML that validates. In order to make it work better I re-wrote the method to be more suitable.

Put the following in the application_helper.rb file and call it using custom_text("String").

def custom_text(text)
  if text.blank?
    ""
  else
    text = RedCloth.new(text).to_html
    if text[0..2] == "<p>" then text = text[3..-1] end
    if text[-4..-1] == "</p>" then text = text[0..-5] end
    text = text.gsub("<p>", "")
    text = text.gsub("</p>", "")
    return text
  end
end

Job done, you should get correct HTML and XHTML but no paragraphs and no messing with line breaks.

Textilize Alternative