public Method

TextHelper.simple_format(text)

Returns text transformed into HTML using simple formatting rules. Two or more consecutive newlines(\n\n) are considered as a paragraph and wrapped in <p></p> tags. One newline (\n) is considered as a linebreak and a <br /> tag is appended. This method does not remove the newlines from the text.

Examples

my_text = """Here is some basic text...
          ...with a line break."""

simple_format(my_text)
# => "<p>Here is some basic text...<br />...with a line break.</p>"

more_text = """We want to put a paragraph...

            ...right there."""

simple_format(more_text)
# => "<p>We want to put a paragraph...</p><p>...right there.</p>"

Source Code

# File action_view/helpers/text_helper.rb, line 307
def simple_format(text)
  content_tag 'p', text.to_s.
    gsub(/\r\n?/, "\n").                    # \r\n and \r -> \n
    gsub(/\n\n+/, "</p>\n\n<p>").           # 2+ newline  -> paragraph
    gsub(/([^\n]\n)(?=[^\n])/, '\1<br />')  # 1 newline   -> br
end
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.