1 # Copyright (C) 2016-2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
6 use PublicInbox::Linkify;
9 my $l = PublicInbox::Linkify->new;
10 my $u = 'http://example.com/url-with-trailing-period';
12 $s = $l->linkify_1($s);
13 $s = $l->linkify_2($s);
14 is($s, qq(<a\nhref="$u">$u</a>.), 'trailing period not in URL');
18 my $l = PublicInbox::Linkify->new;
19 my $u = 'http://i-forgot-trailing-slash.example.com';
21 $s = $l->linkify_1($s);
22 $s = $l->linkify_2($s);
23 is($s, qq(<a\nhref="$u">$u</a>), 'missing trailing slash OK');
26 # handle URLs in parenthesized statements
28 my $l = PublicInbox::Linkify->new;
29 my $u = 'http://example.com/';
31 $s = $l->linkify_1($s);
32 $s = $l->linkify_2($s);
33 is($s, qq{(see: <a\nhref="$u">$u</a>)}, 'trailing ) not in URL');
37 my $l = PublicInbox::Linkify->new;
38 my $u = 'http://example.com/url-with-trailing-semicolon';
40 $s = $l->linkify_1($s);
41 $s = $l->linkify_2($s);
42 is($s, qq(<a\nhref="$u">$u</a>;), 'trailing semicolon not in URL');
46 my $l = PublicInbox::Linkify->new;
47 my $u = 'http://example.com/url-with-(parens)';
48 my $s = "hello $u world";
49 $s = $l->linkify_1($s);
50 $s = $l->linkify_2($s);
51 is($s, qq(hello <a\nhref="$u">$u</a> world), 'URL preserved');
54 $s = $l->linkify_1($s);
55 $s = $l->linkify_2($s);
56 is($s, qq(<a\nhref="$u">$u</a>. hi), 'paired () in URL OK');
59 $s = "hello $u world";
60 $s = $l->linkify_1($s);
61 $s = $l->linkify_2($s);
62 is($s, qq(hello <a\nhref="$u">$u</a> world), 'query preserved');
65 $s = "hello $u world";
66 $s = $l->linkify_1($s);
67 $s = $l->linkify_2($s);
68 is($s, qq(hello <a\nhref="$u">$u</a> world),
69 'query + fragment preserved');
71 $u = "http://example.com/";
72 $s = "hello $u world";
73 $s = $l->linkify_1($s);
74 $s = $l->linkify_2($s);
75 is($s, qq(hello <a\nhref="$u">$u</a> world), "root URL preserved");
77 $u = "http://example.com/#fragment";
78 $s = "hello $u world";
79 $s = $l->linkify_1($s);
80 $s = $l->linkify_2($s);
81 is($s, qq(hello <a\nhref="$u">$u</a> world), "root + fragment");
84 # Markdown compatibility
86 my $l = PublicInbox::Linkify->new;
87 my $u = 'http://example.com/';
88 my $s = "[markdown]($u)";
89 $s = $l->linkify_1($s);
90 $s = $l->linkify_2($s);
91 is($s, qq![markdown](<a\nhref="$u">$u</a>)!, 'Markdown-compatible');
93 $s = qq![markdown]($u "title")!;
94 $s = $l->linkify_1($s);
95 $s = $l->linkify_2($s);
96 is($s, qq![markdown](<a\nhref="$u">$u</a> "title")!,
97 'Markdown title compatible');
99 $s = qq![markdown]($u).!;
100 $s = $l->linkify_1($s);
101 $s = $l->linkify_2($s);
102 is($s, qq![markdown](<a\nhref="$u">$u</a>).!,
103 'Markdown-compatible end of sentence');
106 # Perl and Ruby code compatibility
108 my $l = PublicInbox::Linkify->new;
109 my $u = 'http://example.com/';
110 foreach my $q ("'%s'", '"%s"', 'q!%s!', 'q(%s)') {
112 my $s = sprintf("my \$var = $q;", $u);
113 $s = $l->linkify_1($s);
114 $s = $l->linkify_2($s);
115 like($s, qr/>\Q$u\E</, "no quote($q) in URL");
117 # applies to Ruby, too
118 $s = sprintf("$q,", $u);
119 $s = $l->linkify_1($s);
120 $s = $l->linkify_2($s);
121 like($s, qr/>\Q$u\E</, "no quote($q) in URL array");
125 # dangling ')' cf. see MaintNotes in git.git todo branch
127 my $l = PublicInbox::Linkify->new;
128 my $s = '(see http://example.com/).';
129 $s = $l->linkify_1($s);
130 $s = $l->linkify_2($s);
131 like($s, qr!\(see <a[^>]+>http://example\.com/</a>\)\.!s,
132 'punctuation with unpaired ) OK')
135 if ('IDN example: <ACDB98F4-178C-43C3-99C4-A1D03DD6A8F5@sb.org>') {
137 my $u = "http://www.\x{6708}.example.com/";
139 my $l = PublicInbox::Linkify->new;
140 $s = $l->linkify_1($s);
141 $s = $l->linkify_2($s);
143 href="http://www.$hc.example.com/">http://www.$hc.example.com/</a>};
144 is($s, $expect, 'IDN message escaped properly');