2 package IkiWiki::Plugin::link;
10 my $email_regexp = qr/^.+@.+\..+$/;
11 my $url_regexp = qr/^(?:[^:]+:\/\/|mailto:).*/i;
14 hook(type => "getsetup", id => "link", call => \&getsetup);
15 hook(type => "checkconfig", id => "link", call => \&checkconfig);
16 hook(type => "linkify", id => "link", call => \&linkify);
17 hook(type => "scan", id => "link", call => \&scan);
18 hook(type => "renamepage", id => "link", call => \&renamepage);
31 if ($config{prefix_directives}) {
33 \[\[(?=[^!]) # beginning of link
35 ([^\]\|]+) # 1: link text
39 ([^\n\r\]#]+) # 2: page to link to
41 \# # '#', beginning of anchor
42 ([^\s\]]+) # 3: anchor text
50 \[\[ # beginning of link
52 ([^\]\|\n\s]+) # 1: link text
56 ([^\s\]#]+) # 2: page to link to
58 \# # '#', beginning of anchor
59 ([^\s\]]+) # 3: anchor text
67 sub is_externallink ($$;$) {
72 if (defined $anchor) {
76 return ($url =~ /$url_regexp|$email_regexp/)
79 sub externallink ($$;$) {
82 my $pagetitle = shift;
84 if (defined $anchor) {
91 # use only the email address as title for mailto: urls
92 if ($pagetitle =~ /^mailto:.*/) {
93 $pagetitle =~ s/^mailto:([^?]+).*/$1/;
97 if ($url !~ /$url_regexp/) {
98 # handle email addresses (without mailto:)
99 $url = "mailto:" . $url;
102 return "<a href=\"$url\">$pagetitle</a>";
107 my $page=$params{page};
108 my $destpage=$params{destpage};
110 $params{content} =~ s{(\\?)$link_regexp}{
113 ? "[[$2|$3".(defined $4 ? "#$4" : "")."]]"
114 : is_externallink($page, $3, $4)
115 ? externallink($3, $4, $2)
116 : htmllink($page, $destpage, linkpage($3),
117 anchor => $4, linktext => pagetitle($2)))
119 ? "[[$3".(defined $4 ? "#$4" : "")."]]"
120 : is_externallink($page, $3, $4)
121 ? externallink($3, $4)
122 : htmllink($page, $destpage, linkpage($3),
126 return $params{content};
131 my $page=$params{page};
132 my $content=$params{content};
134 while ($content =~ /(?<!\\)$link_regexp/g) {
135 if (! is_externallink($page, $2, $3)) {
136 add_link($page, linkpage($2));
143 my $page=$params{page};
144 my $old=$params{oldpage};
145 my $new=$params{newpage};
147 $params{content} =~ s{(?<!\\)($link_regexp)}{
148 if (! is_externallink($page, $3, $4)) {
151 if (bestlink($page, linkpage($linktext)) eq $old) {
152 $link=pagetitle($new, 1);
154 if ($linktext =~ m/.*\/*?[A-Z]/) {
155 # preserve leading cap of last component
156 my @bits=split("/", $link);
157 $link=join("/", @bits[0..$#bits-1], ucfirst($bits[$#bits]));
159 if (index($linktext, "/") == 0) {
165 ? ( "[[$2|$link".($4 ? "#$4" : "")."]]" )
166 : ( "[[$link". ($4 ? "#$4" : "")."]]" )
173 return $params{content};