Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #2448: simple_format_html_options.diff

File simple_format_html_options.diff, 2.3 kB (added by thechrisoshow, 7 months ago)

Updated for r9082 (includes tests and docs)

  • actionpack/test/template/text_helper_test.rb

    old new  
    2424 
    2525    text = "A\r\n  \nB\n\n\r\n\t\nC\nD".freeze 
    2626    assert_equal "<p>A\n<br />  \n<br />B</p>\n\n<p>\t\n<br />C\n<br />D</p>", simple_format(text) 
     27     
     28     assert_equal %q(<p class="test">This is a classy test</p>), simple_format("This is a classy test", :class => 'test') 
     29     assert_equal %Q(<p class="test">para 1</p>\n\n<p class="test">para 2</p>), simple_format("para 1\n\npara 2", :class => 'test')      
    2730  end 
    2831 
    2932  def test_truncate 
  • actionpack/lib/action_view/helpers/text_helper.rb

    old new  
    291291      # considered as a linebreak and a <tt><br /></tt> tag is appended. This 
    292292      # method does not remove the newlines from the +text+.  
    293293      # 
     294      # You can pass any HTML attributes into <tt>html_options</tt>.  These  
     295      # will be added to all created paragraphs. 
    294296      # ==== Examples 
    295297      #   my_text = "Here is some basic text...\n...with a line break." 
    296298      # 
     
    301303      # 
    302304      #   simple_format(more_text) 
    303305      #   # => "<p>We want to put a paragraph...</p>\n\n<p>...right there.</p>" 
    304       def simple_format(text) 
     306      # 
     307      #   simple_format("Look ma! A class!", :class => 'description') 
     308      #   # => "<p class='description'>Look ma! A class!</p>" 
     309      def simple_format(text, html_options={}) 
    305310        content_tag 'p', text.to_s. 
    306311          gsub(/\r\n?/, "\n").                    # \r\n and \r -> \n 
    307           gsub(/\n\n+/, "</p>\n\n<p>").           # 2+ newline  -> paragraph 
    308           gsub(/([^\n]\n)(?=[^\n])/, '\1<br />')  # 1 newline   -> br 
     312          gsub(/\n\n+/, "</p>\n\n#{tag('p', html_options, true)}").           # 2+ newline  -> paragraph 
     313          gsub(/([^\n]\n)(?=[^\n])/, '\1<br />'),  # 1 newline   -> br 
     314          html_options 
    309315      end 
    310316 
    311317      # Turns all URLs and e-mail addresses into clickable links. The +link+ parameter