Method: AnyView::Helpers::FormatHelpers#simple_format

Defined in:
lib/any_view/format_helpers.rb

#simple_format(text, html_options = {}) ⇒ Object

Returns text transformed into HTML using simple formatting rules. Two or more consecutive newlines(nn) 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. simple_format(“hellonworld”) # => “<p>hello<br/>world</p>”



38
39
40
41
42
43
44
45
46
# File 'lib/any_view/format_helpers.rb', line 38

def simple_format(text, html_options={})
  start_tag = tag('p', html_options.merge(:open => true))
  text = text.to_s.dup
  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 << "</p>"
end