]> Sergey Matveev's repositories - public-inbox.git/commitdiff
linkify: require parentheses pairs in URLs
authorEric Wong <e@80x24.org>
Thu, 18 Apr 2019 23:49:42 +0000 (23:49 +0000)
committerEric Wong <e@80x24.org>
Thu, 18 Apr 2019 23:50:32 +0000 (23:50 +0000)
Dangling parentheses with trailing punctuation usually means the
parentheses is not intended as part of the URL.

lib/PublicInbox/Linkify.pm
t/linkify.t

index 71193bc28732f28ca715323b04d31e53467ab189..d4778e7de371ecc5c3059363bb0d0ec1cc334686 100644 (file)
@@ -47,8 +47,16 @@ sub linkify_1 {
                        if ($url =~ s/$re//) {
                                $end = $1;
                        }
-               } elsif ($url =~ s/([\.,;])\z//) {
-                       $end = $1;
+               } elsif ($url =~ s/(\))?([\.,;])\z//) {
+                       $end = $2;
+                       # require ')' to be paired with '('
+                       if (defined $1) { # ')'
+                               if (index($url, '(') < 0) {
+                                       $end = ")$end";
+                               } else {
+                                       $url .= ')';
+                               }
+                       }
                } elsif ($url !~ /\(/ && $url =~ s/\)\z//) {
                        $end = ')';
                }
index bef4ffd6fefb360533c007cb53a74428315f8f34..fe218b91f95c4a89e0d3eaea0e6a2d6052b39c53 100644 (file)
@@ -50,6 +50,11 @@ use PublicInbox::Linkify;
        $s = $l->linkify_2($s);
        is($s, qq(hello <a\nhref="$u">$u</a> world), 'URL preserved');
 
+       $s = "$u. hi";
+       $s = $l->linkify_1($s);
+       $s = $l->linkify_2($s);
+       is($s, qq(<a\nhref="$u">$u</a>. hi), 'paired () in URL OK');
+
        $u .= "?query=a";
        $s = "hello $u world";
        $s = $l->linkify_1($s);
@@ -117,4 +122,14 @@ use PublicInbox::Linkify;
        }
 }
 
+# dangling ')'  cf. see MaintNotes in git.git todo branch
+{
+       my $l = PublicInbox::Linkify->new;
+       my $s = '(see http://example.com/).';
+       $s = $l->linkify_1($s);
+       $s = $l->linkify_2($s);
+       like($s, qr!\(see <a[^>]+>http://example\.com/</a>\)\.!s,
+               'punctuation with unpaired ) OK')
+}
+
 done_testing();