Skip to content

Commit

Permalink
Added TextHelper#simple_format as a non-dependency text presentation …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
dhh committed Mar 20, 2005
1 parent ebf6637 commit 1b93da3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
4 changes: 4 additions & 0 deletions 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 <tt>disabled => :true</tt> will give <tt>disabled="disabled"</tt>. #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:
Expand Down
15 changes: 14 additions & 1 deletion actionpack/lib/action_view/helpers/text_helper.rb
Expand Up @@ -90,11 +90,24 @@ def textilize_without_paragraph(text)
# 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.
Expand Down
6 changes: 6 additions & 0 deletions actionpack/test/template/text_helper_test.rb
Expand Up @@ -3,6 +3,12 @@

class TextHelperTest < Test::Unit::TestCase
include ActionView::Helpers::TextHelper

def test_simple_format
assert_equal "<p>crazy\n<br /> cross\n<br /> platform linebreaks</p>", simple_format("crazy\r\n cross\r platform linebreaks")
assert_equal "<p>A paragraph</p>\n\n<p>and another one!</p>", simple_format("A paragraph\n\nand another one!")
assert_equal "<p>A paragraph\n<br /> With a newline</p>", simple_format("A paragraph\n With a newline")
end

def test_truncate
assert_equal "Hello World!", truncate("Hello World!", 12)
Expand Down

0 comments on commit 1b93da3

Please sign in to comment.