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

Changeset 9083

Show
Ignore:
Timestamp:
03/24/08 02:29:30 (2 months ago)
Author:
rick
Message:

Allow the #simple_format text_helper to take an html_options hash for each paragraph. Closes #2448 [Francois Beausoleil, thechrisoshow]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/CHANGELOG

    r9080 r9083  
    11*SVN* 
     2 
     3# Allow the #simple_format text_helper to take an html_options hash for each paragraph.  #2448 [Francois Beausoleil, thechrisoshow] 
    24 
    35* Fix regression from filter refactoring where re-adding a skipped filter resulted in it being called twice.  [rick] 
  • trunk/actionpack/lib/action_view/helpers/text_helper.rb

    r9035 r9083  
    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." 
     
    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) 
    305         content_tag 'p', text.to_s. 
    306           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 
     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={}) 
     310        start_tag = tag('p', html_options, true) 
     311        text = text.to_s.dup 
     312        text.gsub!(/\r\n?/, "\n")                    # \r\n and \r -> \n 
     313        text.gsub!(/\n\n+/, "</p>\n\n#{start_tag}")  # 2+ newline  -> paragraph 
     314        text.gsub!(/([^\n]\n)(?=[^\n])/, '\1<br />') # 1 newline   -> br 
     315        text.insert 0, start_tag 
     316        text << "</p>" 
    309317      end 
    310318 
  • trunk/actionpack/test/template/text_helper_test.rb

    r9030 r9083  
    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