Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Play nicely with non-latin characters in autolinked urls #11

Merged
merged 1 commit into from

2 participants

@jamis

Given a URL that includes trailing non-latin word characters, those characters will be stripped off by the current implementation, because they do not match "\w".

This pull request uses "\p{Word}" instead of "\w" so that all unicode word characters will match, and will be preserved in the autolinked URL.

@xuanxu xuanxu merged commit 5097044 into tenderlove:master
@xuanxu
Collaborator

Merged, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 7 additions and 2 deletions.
  1. +6 −2 lib/rails_autolink/helpers.rb
  2. +1 −0  test/test_rails_autolink.rb
View
8 lib/rails_autolink/helpers.rb
@@ -1,3 +1,5 @@
+# encoding: utf-8
+
module RailsAutolink
require 'active_support/core_ext/object/blank'
require 'active_support/core_ext/array/extract_options'
@@ -80,6 +82,8 @@ def auto_link(text, *args, &block)#link = :all, html = {}, &block)
BRACKETS = { ']' => '[', ')' => '(', '}' => '{' }
+ WORD_PATTERN = RUBY_VERSION < '1.9' ? '\w' : '\p{Word}'
+
# 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 = {}, options = {})
@@ -93,7 +97,7 @@ def auto_link_urls(text, html_options = {}, options = {})
href
else
# don't include trailing punctuation character as part of the URL
- while href.sub!(/[^\w\/-]$/, '')
+ while href.sub!(/[^#{WORD_PATTERN}\/-]$/, '')
punctuation.push $&
if opening = BRACKETS[punctuation.last] and href.scan(opening).size > href.scan(punctuation.last).size
href << punctuation.pop
@@ -149,4 +153,4 @@ def conditional_html_safe(target, condition)
end
end
end
-end
+end
View
1  test/test_rails_autolink.rb
@@ -298,6 +298,7 @@ def test_auto_link_parsing
http://connect.oraclecorp.com/search?search[q]=green+france&search[type]=Group
http://of.openfoundry.org/projects/492/download#4th.Release.3
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
+ http://около.кола/колокола
)
urls.each do |url|
Something went wrong with that request. Please try again.