Skip to content

Commit 5097044

Browse files
committed
Merge pull request tenderlove#11 from jamis/master
Play nicely with non-latin characters in autolinked urls
2 parents a065b4b + 91a59b5 commit 5097044

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

lib/rails_autolink/helpers.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# encoding: utf-8
2+
13
module RailsAutolink
24
require 'active_support/core_ext/object/blank'
35
require 'active_support/core_ext/array/extract_options'
@@ -80,6 +82,8 @@ def auto_link(text, *args, &block)#link = :all, html = {}, &block)
8082

8183
BRACKETS = { ']' => '[', ')' => '(', '}' => '{' }
8284

85+
WORD_PATTERN = RUBY_VERSION < '1.9' ? '\w' : '\p{Word}'
86+
8387
# Turns all urls into clickable links. If a block is given, each url
8488
# is yielded and the result is used as the link text.
8589
def auto_link_urls(text, html_options = {}, options = {})
@@ -93,7 +97,7 @@ def auto_link_urls(text, html_options = {}, options = {})
9397
href
9498
else
9599
# don't include trailing punctuation character as part of the URL
96-
while href.sub!(/[^\w\/-]$/, '')
100+
while href.sub!(/[^#{WORD_PATTERN}\/-]$/, '')
97101
punctuation.push $&
98102
if opening = BRACKETS[punctuation.last] and href.scan(opening).size > href.scan(punctuation.last).size
99103
href << punctuation.pop
@@ -149,4 +153,4 @@ def conditional_html_safe(target, condition)
149153
end
150154
end
151155
end
152-
end
156+
end

test/test_rails_autolink.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ def test_auto_link_parsing
298298
http://connect.oraclecorp.com/search?search[q]=green+france&search[type]=Group
299299
http://of.openfoundry.org/projects/492/download#4th.Release.3
300300
http://maps.google.co.uk/maps?f=q&q=the+london+eye&ie=UTF8&ll=51.503373,-0.11939&spn=0.007052,0.012767&z=16&iwloc=A
301+
http://около.кола/колокола
301302
)
302303

303304
urls.each do |url|

0 commit comments

Comments
 (0)