From 4f984c9d0e66601a81cb5ae6e3b50582e6dc0c2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Thu, 13 Nov 2008 22:39:16 +0100 Subject: auto_link helper: add intelligent ending closing bracket handling. add new tests and reorder new ones for readability Signed-off-by: Michael Koziarski [#1353 state:committed] --- actionpack/lib/action_view/helpers/text_helper.rb | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) (limited to 'actionpack/lib/action_view/helpers/text_helper.rb') 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 $` =~ /]*href="$/ - if href =~ /[^\w\/-]$/ - punctuation = href[-1, 1] - href = href[0, href.length - 1] - else - punctuation = '' + if $` =~ /]*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 -- cgit v1.2.3