aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-03-20 15:03:32 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-03-20 15:03:32 +0000
commit1b93da3262de157e7a1339b1b4ce3ff5c22081a5 (patch)
tree81e4e3abb3b902d62b0b3d5cea6ab5d1230d9c77 /actionpack/lib/action_view
parentebf66379186db094ba94914c2aed4e6a485d4708 (diff)
downloadrails-1b93da3262de157e7a1339b1b4ce3ff5c22081a5.tar.gz
rails-1b93da3262de157e7a1339b1b4ce3ff5c22081a5.tar.bz2
rails-1b93da3262de157e7a1339b1b4ce3ff5c22081a5.zip
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
Diffstat (limited to 'actionpack/lib/action_view')
-rw-r--r--actionpack/lib/action_view/helpers/text_helper.rb15
1 files changed, 14 insertions, 1 deletions
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.
# <i>This method is only available if BlueCloth can be required</i>.
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 <tt>&lt;p&gt;</tt> tags, and converts line breaks into <tt>&lt;br /&gt;</tt>
+ # Two consecutive newlines(<tt>\n\n</tt>) are considered as a paragraph, one newline (<tt>\n</tt>) 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/, '</p>\0<p>') # turn two newlines into paragraph
+ text.gsub!(/([^\n])(\n)([^\n])/, '\1\2<br />\3') # turn single newline into <br />
+
+ return '<p>' + text + '</p>' # 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.