2 package IkiWiki::Plugin::link;
11 hook(type => "checkconfig", id => "link", call => \&checkconfig);
12 hook(type => "linkify", id => "link", call => \&linkify);
13 hook(type => "scan", id => "link", call => \&scan);
14 hook(type => "renamepage", id => "link", call => \&renamepage);
17 sub checkconfig () { #{{{
18 if ($config{prefix_directives}) {
20 \[\[(?=[^!]) # beginning of link
22 ([^\]\|]+) # 1: link text
26 ([^\n\r\]#]+) # 2: page to link to
28 \# # '#', beginning of anchor
29 ([^\s\]]+) # 3: anchor text
37 \[\[ # beginning of link
39 ([^\]\|\n\s]+) # 1: link text
43 ([^\s\]#]+) # 2: page to link to
45 \# # '#', beginning of anchor
46 ([^\s\]]+) # 3: anchor text
54 sub linkify (@) { #{{{
56 my $page=$params{page};
57 my $destpage=$params{destpage};
59 $params{content} =~ s{(\\?)$link_regexp}{
62 ? "[[$2|$3".($4 ? "#$4" : "")."]]"
63 : htmllink($page, $destpage, IkiWiki::linkpage($3),
64 anchor => $4, linktext => IkiWiki::pagetitle($2)))
66 ? "[[$3".($4 ? "#$4" : "")."]]"
67 : htmllink($page, $destpage, IkiWiki::linkpage($3),
71 return $params{content};
76 my $page=$params{page};
77 my $content=$params{content};
79 while ($content =~ /(?<!\\)$link_regexp/g) {
80 push @{$links{$page}}, IkiWiki::linkpage($2);
84 sub renamepage (@) { #{{{
86 my $page=$params{page};
87 my $old=$params{oldpage};
88 my $new=$params{newpage};
90 $params{content} =~ s{(?<!\\)$link_regexp}{
93 if (bestlink($page, $2) eq $old) {
94 $link=IkiWiki::pagetitle($new, 1);
95 if ($linktext =~ m/.*\/*?[A-Z]/) {
96 # preserve leading cap of last component
97 my @bits=split("/", $link);
98 $link=join("/", @bits[0..$#bits-1], ucfirst($bits[$#bits]));
100 if (index($linktext, "/") == 0) {
106 ? ( "[[$1|$link".($3 ? "#$3" : "")."]]" )
107 : ( "[[$link". ($3 ? "#$3" : "")."]]" )
110 return $params{content};