From 1b93da3262de157e7a1339b1b4ce3ff5c22081a5 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 20 Mar 2005 15:03:32 +0000 Subject: Added TextHelper#simple_format as a non-dependency text presentation helper.Fixed TextHelper#markdown to use blank? instead of empty? so it can deal with nil strings passed #814 [Johan Sorensen] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@933 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/CHANGELOG | 4 ++++ actionpack/lib/action_view/helpers/text_helper.rb | 15 ++++++++++++++- actionpack/test/template/text_helper_test.rb | 6 ++++++ 3 files changed, 24 insertions(+), 1 deletion(-) (limited to 'actionpack') diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 3595b54a81..4aac69d45a 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,9 @@ *SVN* +* Fixed TextHelper#markdown to use blank? instead of empty? so it can deal with nil strings passed #814 [Johan Sörensen] + +* Added TextHelper#simple_format as a non-dependency text presentation helper #814 [Johan Sörensen] + * Added that the html options disabled, readonly, and multiple can all be treated as booleans. So specifying disabled => :true will give disabled="disabled". #809 [mindel] * Added path collection syntax for Routes that will gobble up the rest of the url and pass it on to the controller #830 [rayners]. Example: diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb index b97b1e130a..aa4d3ab8af 100644 --- a/actionpack/lib/action_view/helpers/text_helper.rb +++ b/actionpack/lib/action_view/helpers/text_helper.rb @@ -90,11 +90,24 @@ module ActionView # Returns the text with all the Markdown codes turned into HTML-tags. # This method is only available if BlueCloth can be required. def markdown(text) - text.empty? ? "" : BlueCloth.new(text).to_html + text.blank? ? "" : BlueCloth.new(text).to_html end rescue LoadError # We can't really help what's not there end + + # Returns +text+ transformed into html using very simple formatting rules + # Surrounds paragraphs with <p> tags, and converts line breaks into <br /> + # Two consecutive newlines(\n\n) are considered as a paragraph, one newline (\n) is + # considered a linebreak, three or more consecutive newlines are turned into two newlines + def simple_format(text) + text.gsub!(/(\r\n|\n|\r)/, "\n") # lets make them newlines crossplatform + text.gsub!(/\n\n+/, "\n\n") # zap dupes + text.gsub!(/\n\n/, '

\0

') # turn two newlines into paragraph + text.gsub!(/([^\n])(\n)([^\n])/, '\1\2
\3') # turn single newline into
+ + return '

' + text + '

' # wrap the first and last line in paragraphs before we're done + end # Turns all urls and email addresses into clickable links. The +link+ parameter can limit what should be linked. # Options are :all (default), :email_addresses, and :urls. diff --git a/actionpack/test/template/text_helper_test.rb b/actionpack/test/template/text_helper_test.rb index aba0d99699..efc66b3bd1 100644 --- a/actionpack/test/template/text_helper_test.rb +++ b/actionpack/test/template/text_helper_test.rb @@ -3,6 +3,12 @@ require File.dirname(__FILE__) + '/../../lib/action_view/helpers/text_helper' class TextHelperTest < Test::Unit::TestCase include ActionView::Helpers::TextHelper + + def test_simple_format + assert_equal "

crazy\n
cross\n
platform linebreaks

", simple_format("crazy\r\n cross\r platform linebreaks") + assert_equal "

A paragraph

\n\n

and another one!

", simple_format("A paragraph\n\nand another one!") + assert_equal "

A paragraph\n
With a newline

", simple_format("A paragraph\n With a newline") + end def test_truncate assert_equal "Hello World!", truncate("Hello World!", 12) -- cgit v1.2.3