1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

The auto_link text helper accepts an optional block to format the link text for each url and email address. References #2628.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2963 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Jeremy Kemper 2005-11-10 06:04:50 +00:00
parent c955f378de
commit adba181060
3 changed files with 40 additions and 10 deletions

View file

@ -1,6 +1,8 @@
*SVN* *SVN*
* assert_tag uses exact matches for string conditions, instead of partial matches. Use regex to do partial matches. #2799 * The auto_link text helper accepts an optional block to format the link text for each url and email address. Example: auto_link(post.body) { |text| truncate(text, 10) } [Jeremy Kemper]
* assert_tag uses exact matches for string conditions, instead of partial matches. Use regex to do partial matches. #2799 [Jamis Buck]
* CGI::Session::ActiveRecordStore.data_column_name = 'foobar' to use a different session data column than the 'data' default. [nbpwie102@sneakemail.com] * CGI::Session::ActiveRecordStore.data_column_name = 'foobar' to use a different session data column than the 'data' default. [nbpwie102@sneakemail.com]

View file

@ -135,11 +135,17 @@ module ActionView
# auto_link("Go to http://www.rubyonrails.com and say hello to david@loudthinking.com") => # 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 # 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> # say hello to <a href="mailto:david@loudthinking.com">david@loudthinking.com</a>
def auto_link(text, link = :all, href_options = {}) #
# If a block is given, each url and email address is yielded and the
# result is used as the link text. Example:
# auto_link(post.body, :all, :target => '_blank') do |text|
# truncate(text, 15)
# end
def auto_link(text, link = :all, href_options = {}, &block)
case link case link
when :all then auto_link_urls(auto_link_email_addresses(text), href_options) when :all then auto_link_urls(auto_link_email_addresses(text, &block), href_options, &block)
when :email_addresses then auto_link_email_addresses(text) when :email_addresses then auto_link_email_addresses(text, &block)
when :urls then auto_link_urls(text, href_options) when :urls then auto_link_urls(text, href_options, &block)
end end
end end
@ -325,22 +331,37 @@ module ActionView
([[:punct:]]|\s|<|$) # trailing text ([[:punct:]]|\s|<|$) # trailing text
/x unless const_defined?(:AUTO_LINK_RE) /x unless const_defined?(:AUTO_LINK_RE)
# Turns all urls into clickable links. # Turns all urls into clickable links. If a block is given, each url
# is yielded and the result is used as the link text. Example:
# auto_link_urls(post.body, :all, :target => '_blank') do |text|
# truncate(text, 15)
# end
def auto_link_urls(text, href_options = {}) def auto_link_urls(text, href_options = {})
extra_options = tag_options(href_options.stringify_keys) || ""
text.gsub(AUTO_LINK_RE) do text.gsub(AUTO_LINK_RE) do
all, a, b, c, d = $&, $1, $2, $3, $5 all, a, b, c, d = $&, $1, $2, $3, $5
if a =~ /<a\s/i # don't replace URL's that are already linked if a =~ /<a\s/i # don't replace URL's that are already linked
all all
else else
extra_options = tag_options(href_options.stringify_keys) || "" text = b + c
%(#{a}<a href="#{b=="www."?"http://www.":b}#{c}"#{extra_options}>#{b}#{c}</a>#{d}) text = yield(text) if block_given?
%(#{a}<a href="#{b=="www."?"http://www.":b}#{c}"#{extra_options}>#{text}</a>#{d})
end end
end end
end end
# Turns all email addresses into clickable links. # Turns all email addresses into clickable links. If a block is given,
# each email is yielded and the result is used as the link text.
# Example:
# auto_link_email_addresses(post.body) do |text|
# truncate(text, 15)
# end
def auto_link_email_addresses(text) def auto_link_email_addresses(text)
text.gsub(/([\w\.!#\$%\-+.]+@[A-Za-z0-9\-]+(\.[A-Za-z0-9\-]+)+)/, '<a href="mailto:\1">\1</a>') text.gsub(/([\w\.!#\$%\-+.]+@[A-Za-z0-9\-]+(\.[A-Za-z0-9\-]+)+)/) do
text = $1
text = yield(text) if block_given?
%{<a href="mailto:#{$1}">#{text}</a>}
end
end end
end end
end end

View file

@ -161,6 +161,13 @@ class TextHelperTest < Test::Unit::TestCase
assert_equal %(<p><a href="#{url1}">#{url1}</a><br /><a href="#{url2}">#{url2}</a><br /></p>), auto_link("<p>#{url1}<br />#{url2}<br /></p>") assert_equal %(<p><a href="#{url1}">#{url1}</a><br /><a href="#{url2}">#{url2}</a><br /></p>), auto_link("<p>#{url1}<br />#{url2}<br /></p>")
end end
def test_auto_link_with_block
url = "http://api.rubyonrails.com/Foo.html"
email = "fantabulous@shiznadel.ic"
assert_equal %(<p><a href="#{url}">#{url[0..7]}...</a><br /><a href="mailto:#{email}">#{email[0..7]}...</a><br /></p>), auto_link("<p>#{url}<br />#{email}<br /></p>") { |url| truncate(url, 10) }
end
def test_sanitize_form def test_sanitize_form
raw = "<form action=\"/foo/bar\" method=\"post\"><input></form>" raw = "<form action=\"/foo/bar\" method=\"post\"><input></form>"
result = sanitize(raw) result = sanitize(raw)