aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/helpers/text_helper.rb
diff options
context:
space:
mode:
authorMislav Marohnić <mislav.marohnic@gmail.com>2008-11-13 22:39:16 +0100
committerMichael Koziarski <michael@koziarski.com>2008-11-15 18:30:17 +0100
commit4f984c9d0e66601a81cb5ae6e3b50582e6dc0c2d (patch)
treeeed03362c18cc80ef3b69b1bb1aee78e734b4fc0 /actionpack/lib/action_view/helpers/text_helper.rb
parentc6c5cd554110f6e62290de3e3008076b2f69e7cb (diff)
downloadrails-4f984c9d0e66601a81cb5ae6e3b50582e6dc0c2d.tar.gz
rails-4f984c9d0e66601a81cb5ae6e3b50582e6dc0c2d.tar.bz2
rails-4f984c9d0e66601a81cb5ae6e3b50582e6dc0c2d.zip
auto_link helper: add intelligent ending closing bracket handling. add new tests and reorder new ones for readability
Signed-off-by: Michael Koziarski <michael@koziarski.com> [#1353 state:committed]
Diffstat (limited to 'actionpack/lib/action_view/helpers/text_helper.rb')
-rw-r--r--actionpack/lib/action_view/helpers/text_helper.rb22
1 files changed, 13 insertions, 9 deletions
diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb
index 07f98158f7..9bd3d63423 100644
--- a/actionpack/lib/action_view/helpers/text_helper.rb
+++ b/actionpack/lib/action_view/helpers/text_helper.rb
@@ -549,28 +549,32 @@ module ActionView
[^\s<]+
}x unless const_defined?(:AUTO_LINK_RE)
+ BRACKETS = { ']' => '[', ')' => '(', '}' => '{' }
+
# Turns all urls into clickable links. If a block is given, each url
# is yielded and the result is used as the link text.
def auto_link_urls(text, html_options = {})
link_attributes = html_options.stringify_keys
text.gsub(AUTO_LINK_RE) do
href = $&
+ punctuation = ''
# detect already linked URLs
- unless $` =~ /<a\s[^>]*href="$/
- if href =~ /[^\w\/-]$/
- punctuation = href[-1, 1]
- href = href[0, href.length - 1]
- else
- punctuation = ''
+ if $` =~ /<a\s[^>]*href="$/
+ # do not change string; URL is alreay linked
+ href
+ else
+ # don't include trailing punctuation character as part of the URL
+ if href.sub!(/[^\w\/-]$/, '') and punctuation = $& and opening = BRACKETS[punctuation]
+ if href.scan(opening).size > href.scan(punctuation).size
+ href << punctuation
+ punctuation = ''
+ end
end
link_text = block_given?? yield(href) : href
href = 'http://' + href unless href.index('http') == 0
content_tag(:a, h(link_text), link_attributes.merge('href' => href)) + punctuation
- else
- # do not change string; URL is alreay linked
- href
end
end
end