Ticket #2448: simple-format-html-options.patch
| File simple-format-html-options.patch, 2.3 kB (added by François Beausoleil <fbeausoleil@ftml.net>, 3 years ago) |
|---|
-
actionpack/test/template/text_helper_test.rb
old new 268 268 assert_equal(%w{Specialized Fuji Giant}, @cycles) 269 269 end 270 270 271 def test_simple_format_one_para_with_html_options 272 assert_equal %q(<p class="test">this is the text</p>), 273 simple_format("this is the text", :class => 'test') 274 end 275 276 def test_simple_format_two_paras_with_html_options 277 assert_equal %Q(<p class="test">para 1</p>\n\n<p class="test">para 2</p>), 278 simple_format("para 1\n\npara 2", :class => 'test') 279 end 271 280 end -
actionpack/lib/action_view/helpers/text_helper.rb
old new 118 118 # Returns +text+ transformed into HTML using very simple formatting rules 119 119 # Surrounds paragraphs with <tt><p></tt> tags, and converts line breaks into <tt><br /></tt> 120 120 # Two consecutive newlines(<tt>\n\n</tt>) are considered as a paragraph, one newline (<tt>\n</tt>) is 121 # considered a linebreak, three or more consecutive newlines are turned into two newlines 122 def simple_format(text) 121 # considered a linebreak, three or more consecutive newlines are turned into two newlines 122 # 123 # You can pass any HTML attributes into <var>html_options</var>. These 124 # will be added to all created paragraphs. 125 def simple_format(text, html_options={}) 123 126 text.gsub!(/(\r\n|\n|\r)/, "\n") # lets make them newlines crossplatform 124 127 text.gsub!(/\n\n+/, "\n\n") # zap dupes 125 text.gsub!(/\n\n/, '</p>\0<p>') # turn two newlines into paragraph128 text.gsub!(/\n\n/, "</p>\\0#{tag('p', html_options, true)}") # turn two newlines into paragraph 126 129 text.gsub!(/([^\n])(\n)([^\n])/, '\1\2<br />\3') # turn single newline into <br /> 127 130 128 content_tag("p", text )131 content_tag("p", text, html_options) 129 132 end 130 133 131 134 # Turns all urls and email addresses into clickable links. The +link+ parameter can limit what should be linked.