Login | Signup | Donate

Posted
19 Aug 2006 @ 22:15

Categories
Ruby on Rails, Tutorial

Author
Alex

Bookmark
Delicious, Digg, reddit, StumbleUpon

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.rhtml’ 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 brakes.

Comments

There are currently no comments available for this post.

Add A Comment

Please login or signup in order to post comments.