aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-02-19 12:15:38 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-02-19 12:15:38 +0000
commit0369ec79a9c752cac137ae8223e47215a34c39d2 (patch)
treec195c16b619a1fd1fe2210ef50fc75ca437879aa /actionpack/lib/action_view
parent677005c349b4c9585082bd2664cce2b36399d20e (diff)
downloadrails-0369ec79a9c752cac137ae8223e47215a34c39d2.tar.gz
rails-0369ec79a9c752cac137ae8223e47215a34c39d2.tar.bz2
rails-0369ec79a9c752cac137ae8223e47215a34c39d2.zip
Made auto_link the only public method of its clan and added an option to control what to be linked instead
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@684 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack/lib/action_view')
-rw-r--r--actionpack/lib/action_view/helpers/text_helper.rb39
1 files changed, 23 insertions, 16 deletions
diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb
index ae65a1c2bd..26a8b74faa 100644
--- a/actionpack/lib/action_view/helpers/text_helper.rb
+++ b/actionpack/lib/action_view/helpers/text_helper.rb
@@ -96,22 +96,19 @@ 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>')
+ # 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.
+ #
+ # Example:
+ # auto_link("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, link = :all)
+ case link
+ when :all then auto_link_urls(auto_link_email_addresses(text))
+ when :email_addresses then auto_link_email_addresses(text)
+ when :urls then auto_link_urls(text)
+ end
end
# Turns all links into words, like "<a href="something">else</a>" to "else".
@@ -124,6 +121,16 @@ module ActionView
def escape_regexp(text)
text.gsub(/([\\|?+*\/\)\(])/) { |m| "\\#{$1}" }
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
end
end
end \ No newline at end of file