aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--actionpack/CHANGELOG2
-rw-r--r--actionpack/lib/action_view/helpers/text_helper.rb5
-rw-r--r--actionpack/test/template/text_helper_test.rb4
3 files changed, 11 insertions, 0 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG
index 174cb88d20..c9221c45be 100644
--- a/actionpack/CHANGELOG
+++ b/actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Added TextHelper#word_wrap(text, line_length = 80) #1449 [tuxie@dekadance.se]
+
* Added a fall-through action for form_remote_tag that'll be used in case Javascript is unavailable #1459 [Scott Barron]. Example:
form_remote_tag :html => { :action => url_for(:controller => "some", :action => "place") }
diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb
index a65a979343..5e1a2e66ce 100644
--- a/actionpack/lib/action_view/helpers/text_helper.rb
+++ b/actionpack/lib/action_view/helpers/text_helper.rb
@@ -65,6 +65,11 @@ module ActionView
end
end
+ # Word wrap long lines to line_width.
+ def word_wrap(text, line_width = 80)
+ text.gsub(/\n/, "\n\n").gsub(/(.{1,#{line_width}})(\s+|$)/, "\\1\n").strip
+ end
+
begin
require "redcloth"
diff --git a/actionpack/test/template/text_helper_test.rb b/actionpack/test/template/text_helper_test.rb
index 722f08a7cf..2206ac7d04 100644
--- a/actionpack/test/template/text_helper_test.rb
+++ b/actionpack/test/template/text_helper_test.rb
@@ -67,6 +67,10 @@ class TextHelperTest < Test::Unit::TestCase
assert_equal("...iful morning", excerpt("This is a beautiful morning", "morning", 5))
assert_nil excerpt("This is a beautiful morning", "day")
end
+
+ def test_word_wrap
+ assert_equal("my very very\nvery long\nstring", word_wrap("my very very very long string", 15))
+ end
def test_pluralization
assert_equal("1 count", pluralize(1, "count"))