Method: ActionView::Helpers::TextHelper#simple_format
- Defined in:
- lib/action_view/helpers/text_helper.rb
#simple_format(text, html_options = {}, options = {}) ⇒ Object
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> 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.
You can pass any HTML attributes into html_options. These will be added to all created paragraphs.
Options
-
:sanitize- Iffalse, does not sanitizetext.
Examples
my_text = "Here is some basic text...\n...with a line break."
simple_format(my_text)
# => "<p>Here is some basic text...\n<br />...with a line break.</p>"
more_text = "We want to put a paragraph...\n\n...right there."
simple_format(more_text)
# => "<p>We want to put a paragraph...</p>\n\n<p>...right there.</p>"
simple_format("Look ma! A class!", :class => 'description')
# => "<p class='description'>Look ma! A class!</p>"
simple_format("<span>I'm allowed!</span> It's true.", {}, :sanitize => false)
# => "<p><span>I'm allowed!</span> It's true.</p>"
266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/action_view/helpers/text_helper.rb', line 266 def simple_format(text, ={}, ={}) text = '' if text.nil? text = text.dup start_tag = tag('p', , true) text = sanitize(text) unless [:sanitize] == false text = text.to_str text.gsub!(/\r\n?/, "\n") # \r\n and \r -> \n text.gsub!(/\n\n+/, "</p>\n\n#{start_tag}") # 2+ newline -> paragraph text.gsub!(/([^\n]\n)(?=[^\n])/, '\1<br />') # 1 newline -> br text.insert 0, start_tag text.html_safe.safe_concat("</p>") end |