Repository: tenderlove/rails_autolink (Ruby)

Issue:
parse URLs that end in dashes
``` ruby
auto_link('http://example.com/e-')
```

should return

``` html
<a href="http://example.com/e-">http://example.com/e-</a>
```

but returns

``` html
<a href="http://example.com/e">http://example.com/e</a>-
```



Discussion:
Can you give a concrete example of a live URL that has this happen? Seems weird to have a url ending with a hyphen. Is that even a valid url?

The case that I ran into involved a URL to redeem a gift code. Hyphen is a valid character for URLs, so I assume their system generates a random URL from the valid character set which can sometimes end in a hyphen. 

I agree that it's unusual, but I don't think it's in anyway invalid. 

I did some searching around, looks like Drupal can generate URLs that end in hyphens too: https://productforums.google.com/forum/m/#!topic/webmasters/tBv3MKghw0Y

[Devise.friendly_token](https://github.com/plataformatec/devise/blob/v4.2.0/lib/devise.rb#L477) generate URL that end in dash too.
This issue occurs when we give URL including `Devise.friendly_token` to `auto_link`. As below:

```rb
auto_link('https://example.com/confirm?token=MRPEkBAJyHgBUFNgpLs-')
#=> "<a href=\"https://example.com/confirm?token=MRPEkBAJyHgBUFNgpLs\">https://example.com/confirm?token=MRPEkBAJyHgBUFNgpLs</a>-"
```



Patch:
diff --git a/lib/rails_autolink/helpers.rb b/lib/rails_autolink/helpers.rb
index 2269331..cbb33da 100644
--- a/lib/rails_autolink/helpers.rb
+++ b/lib/rails_autolink/helpers.rb
@@ -98,7 +98,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!(/[^#{WORD_PATTERN}\/-=&]$/, '')
+                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


Test patch:
diff --git a/test/test_rails_autolink.rb b/test/test_rails_autolink.rb
index 56bc94c..adc0cf6 100644
--- a/test/test_rails_autolink.rb
+++ b/test/test_rails_autolink.rb
@@ -332,6 +332,28 @@ def test_autolink_with_trailing_amp_on_link
     assert_equal generate_result(url), auto_link(url)
   end
 
+  def test_autolink_with_trailing_colon_on_link
+    url = "http://www.rubyonrails.com/foo.cgi?trailing_colon=value:"
+    expected_url = "http://www.rubyonrails.com/foo.cgi?trailing_colon=value"
+
+    assert_equal "#{generate_result(expected_url)}:", auto_link(url)
+  end
+
+  def test_autolink_with_trailing_hyphen_on_link
+    url = "http://www.rubyonrails.com/foo.cgi?trailing_hyphen=value-"
+    assert_equal generate_result(url), auto_link(url)
+  end
+
+  def test_autolink_with_trailing_forward_slash_on_link
+    url = "http://www.rubyonrails.com/foo.cgi?trailing_forward_slash=value/"
+    assert_equal generate_result(url), auto_link(url)
+  end
+
+  def test_autolink_with_trailing_number_on_link
+    url = "http://www.rubyonrails.com/foo.cgi?trailing_number=value3"
+    assert_equal generate_result(url), auto_link(url)
+  end
+
   def test_auto_link_does_not_timeout_when_parsing_odd_email_input
     inputs = %W(
       foo@...................................