]> Sergey Matveev's repositories - public-inbox.git/commitdiff
linkify: do not capture trailing '.' or ';' in URLs
authorEric Wong <e@80x24.org>
Tue, 1 Mar 2016 03:44:04 +0000 (03:44 +0000)
committerEric Wong <e@80x24.org>
Tue, 1 Mar 2016 03:44:26 +0000 (03:44 +0000)
It seems common for users to end statements with URLs,
while it is rare for a URL itself to end with a '.' or ';'.
So make a guess and assume the URL was intended to not
include the trailing '.' or ';'

MANIFEST
lib/PublicInbox/Linkify.pm
t/linkify.t [new file with mode: 0644]

index 5d790f9c7ab1811069b643dc53f69051df22733b..259f42ce0a936fadcde800f0dc8d8cc80a8d9651 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -80,6 +80,7 @@ t/httpd-corner.psgi
 t/httpd-corner.t
 t/httpd.t
 t/init.t
+t/linkify.t
 t/main-bin/spamc
 t/mda.t
 t/msgmap.t
index 8f634f482828f64896ee7795e60f0267ee21ee02..4eddedd03646750783be13782bc8ce3cd63dced8 100644 (file)
@@ -25,6 +25,14 @@ sub linkify_1 {
        my ($self, $s) = @_;
        $s =~ s!$LINK_RE!
                my $url = $1;
+               my $end = '';
+
+               # it's fairly common to end URLs in messages with
+               # '.' or ';' to denote the end of a statement.
+               if ($url =~ s/(\.)\z// || $url =~ s/(;)\z//) {
+                       $end = $1;
+               }
+
                # salt this, as this could be exploited to show
                # links in the HTML which don't show up in the raw mail.
                my $key = sha1_hex($url . $SALT);
@@ -32,7 +40,7 @@ sub linkify_1 {
                # only escape ampersands, others do not match LINK_RE
                $url =~ s/&/&#38;/g;
                $self->{$key} = $url;
-               'PI-LINK-'. $key;
+               'PI-LINK-'. $key . $end;
        !ge;
        $s;
 }
diff --git a/t/linkify.t b/t/linkify.t
new file mode 100644 (file)
index 0000000..586691a
--- /dev/null
@@ -0,0 +1,26 @@
+# Copyright (C) 2016 all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+use strict;
+use warnings;
+use Test::More;
+use PublicInbox::Linkify;
+
+{
+       my $l = PublicInbox::Linkify->new;
+       my $u = 'http://example.com/url-with-trailing-period';
+       my $s = $u . '.';
+       $s = $l->linkify_1($s);
+       $s = $l->linkify_2($s);
+       is($s, qq(<a\nhref="$u">$u</a>.), 'trailing period not in URL');
+}
+
+{
+       my $l = PublicInbox::Linkify->new;
+       my $u = 'http://example.com/url-with-trailing-semicolon';
+       my $s = $u . ';';
+       $s = $l->linkify_1($s);
+       $s = $l->linkify_2($s);
+       is($s, qq(<a\nhref="$u">$u</a>;), 'trailing semicolon not in URL');
+}
+
+done_testing();