aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-02-18 14:06:36 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-02-18 14:06:36 +0000
commitb305756d9f5e92d0e1cf41c1036139a510882f1f (patch)
tree1d0627d973df212de60aa791051271f45b34699d /actionpack
parentbc8e41247b2bdc37da8be4e963caf99451418cd2 (diff)
downloadrails-b305756d9f5e92d0e1cf41c1036139a510882f1f.tar.gz
rails-b305756d9f5e92d0e1cf41c1036139a510882f1f.tar.bz2
rails-b305756d9f5e92d0e1cf41c1036139a510882f1f.zip
Added TextHelper#auto_link, TextHelper#auto_link_urls, and TextHelper#auto_link_email_addresses to turn those elements into ahrefs
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@661 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/CHANGELOG2
-rw-r--r--actionpack/lib/action_view/helpers/text_helper.rb18
-rw-r--r--actionpack/test/template/text_helper_test.rb6
3 files changed, 26 insertions, 0 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG
index 087b355416..994b73ff6f 100644
--- a/actionpack/CHANGELOG
+++ b/actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Added TextHelper#auto_link, TextHelper#auto_link_urls, and TextHelper#auto_link_email_addresses to turn those elements into ahrefs
+
* Fixed that on validation errors, scaffold couldn't find template #654 [mindel]
* Added Base#hide_actions(*names) to hide public methods from a controller that would otherwise have been callable through the URL. For the majority of cases, its preferred just to make the methods you don't want to expose protected or private (so they'll automatically be hidden) -- but if you must have a public method, this is a way to make it uncallable. Base#hidden_actions retrieve the list of all hidden actions for the controller #644 [Nicholas Seckar]
diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb
index cb5a4578ee..5002291cf2 100644
--- a/actionpack/lib/action_view/helpers/text_helper.rb
+++ b/actionpack/lib/action_view/helpers/text_helper.rb
@@ -96,6 +96,24 @@ module ActionView
# We can't really help what's not there
end
+ # Turns all urls and email addresses into clickable links. Example:
+ # "Go to http://www.rubyonrails.com and say hello to david@loudthinking.com" =>
+ # Go to <a href="http://www.rubyonrails.com">http://www.rubyonrails.com</a> and
+ # say hello to <a href="mailto:david@loudthinking.com">david@loudthinking.com</a>
+ def auto_link(text)
+ auto_link_urls(auto_link_email_addresses(text))
+ end
+
+ # Turns all urls into clickable links.
+ def auto_link_urls(text)
+ text.gsub(/([^=><!:'"\/]|^)((http[s]?:\/\/)|(www\.))(\S+\b\/?)([[:punct:]]*)(\s|$)/, '\1<a href="\3\4\5">\3\4\5</a>\6\7')
+ end
+
+ # Turns all email addresses into clickable links.
+ def auto_link_email_addresses(text)
+ text.gsub(/([\w\.!#\$%\-+.]+@[A-Za-z0-9\-]+(\.[A-Za-z0-9\-]+)+)/, '<a href="mailto:\1">\1</a>')
+ end
+
# Turns all links into words, like "<a href="something">else</a>" to "else".
def strip_links(text)
text.gsub(/<a.*>(.*)<\/a>/m, '\1')
diff --git a/actionpack/test/template/text_helper_test.rb b/actionpack/test/template/text_helper_test.rb
index 347420a72b..086eb46d36 100644
--- a/actionpack/test/template/text_helper_test.rb
+++ b/actionpack/test/template/text_helper_test.rb
@@ -59,4 +59,10 @@ class TextHelperTest < Test::Unit::TestCase
assert_equal("1 count", pluralize(1, "count"))
assert_equal("2 counts", pluralize(2, "count"))
end
+
+ def test_auto_linking
+ assert_equal %(hello <a href="mailto:david@loudthinking.com">david@loudthinking.com</a>), auto_link_email_addresses("hello david@loudthinking.com")
+ assert_equal %(Go to <a href="http://www.rubyonrails.com">http://www.rubyonrails.com</a>), auto_link_urls("Go to http://www.rubyonrails.com")
+ assert_equal %(Go to <a href="http://www.rubyonrails.com">http://www.rubyonrails.com</a> and say hello to <a href="mailto:david@loudthinking.com">david@loudthinking.com</a>), auto_link("Go to http://www.rubyonrails.com and say hello to david@loudthinking.com")
+ end
end \ No newline at end of file