]> Sergey Matveev's repositories - public-inbox.git/commitdiff
linkify: support proto://hostname without trailing slash
authorEric Wong <e@80x24.org>
Fri, 1 Feb 2019 05:27:52 +0000 (05:27 +0000)
committerEric Wong <e@80x24.org>
Fri, 1 Feb 2019 05:27:52 +0000 (05:27 +0000)
Sometimes users will write "http://example.com" without the
trailing slash, which every browser and tool I've tested seems
to understand.

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

index 274f38209fa3a2c1a880b20a2c9c28a4a94e603a..aa472cdb7167473d2706e069073eb9ef7683aa51 100644 (file)
@@ -16,11 +16,12 @@ use Digest::SHA qw/sha1_hex/;
 
 my $SALT = rand;
 my $LINK_RE = qr{(\()?\b((?:ftps?|https?|nntps?|gopher)://
-                [\@:\w\.-]+/
+                [\@:\w\.-]+(?:/
                 (?:[a-z0-9\-\._~!\$\&\';\(\)\*\+,;=:@/%]*)
                 (?:\?[a-z0-9\-\._~!\$\&\';\(\)\*\+,;=:@/%]+)?
                 (?:\#[a-z0-9\-\._~!\$\&\';\(\)\*\+,;=:@/%\?]+)?
-                )}xi;
+                )?
+               )}xi;
 
 sub new { bless {}, $_[0] }
 
index a55ed22a19118d686bbde4c48e7abf4494c9b038..f0b3a6d0b34bdb438a0fc940a9bbb3da2bbce87b 100644 (file)
@@ -14,6 +14,15 @@ use PublicInbox::Linkify;
        is($s, qq(<a\nhref="$u">$u</a>.), 'trailing period not in URL');
 }
 
+{
+       my $l = PublicInbox::Linkify->new;
+       my $u = 'http://i-forgot-trailing-slash.example.com';
+       my $s = $u;
+       $s = $l->linkify_1($s);
+       $s = $l->linkify_2($s);
+       is($s, qq(<a\nhref="$u">$u</a>), 'missing trailing slash OK');
+}
+
 # handle URLs in parenthesized statements
 {
        my $l = PublicInbox::Linkify->new;