add renamepage hooks
[ikiwiki.git] / IkiWiki / Plugin / link.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::link;
3
4 use warnings;
5 use strict;
6 use IkiWiki 2.00;
7
8 my $link_regexp;
9
10 sub import { #{{{
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 } # }}}
15
16 sub checkconfig () { #{{{
17         if ($config{prefix_directives}) {
18                 $link_regexp = qr{
19                         \[\[(?=[^!])            # beginning of link
20                         (?:
21                                 ([^\]\|]+)      # 1: link text
22                                 \|              # followed by '|'
23                         )?                      # optional
24                         
25                         ([^\n\r\]#]+)           # 2: page to link to
26                         (?:
27                                 \#              # '#', beginning of anchor
28                                 ([^\s\]]+)      # 3: anchor text
29                         )?                      # optional
30                         
31                         \]\]                    # end of link
32                 }x;
33         }
34         else {
35                 $link_regexp = qr{
36                         \[\[                    # beginning of link
37                         (?:
38                                 ([^\]\|\n\s]+)  # 1: link text
39                                 \|              # followed by '|'
40                         )?                      # optional
41
42                         ([^\s\]#]+)             # 2: page to link to
43                         (?:
44                                 \#              # '#', beginning of anchor
45                                 ([^\s\]]+)      # 3: anchor text
46                         )?                      # optional
47
48                         \]\]                    # end of link
49                 }x,
50         }
51 } #}}}
52
53 sub linkify (@) { #{{{
54         my %params=@_;
55         my $page=$params{page};
56         my $destpage=$params{destpage};
57
58         $params{content} =~ s{(\\?)$link_regexp}{
59                 defined $2
60                         ? ( $1 
61                                 ? "[[$2|$3".($4 ? "#$4" : "")."]]" 
62                                 : htmllink($page, $destpage, IkiWiki::linkpage($3),
63                                         anchor => $4, linktext => IkiWiki::pagetitle($2)))
64                         : ( $1 
65                                 ? "[[$3".($4 ? "#$4" : "")."]]"
66                                 : htmllink($page, $destpage, IkiWiki::linkpage($3),
67                                         anchor => $4))
68         }eg;
69         
70         return $params{content};
71 } #}}}
72
73 sub scan (@) { #{{{
74         my %params=@_;
75         my $page=$params{page};
76         my $content=$params{content};
77
78         while ($content =~ /(?<!\\)$link_regexp/g) {
79                 push @{$links{$page}}, IkiWiki::linkpage($2);
80         }
81 } # }}}
82
83 sub renamepage (@) { #{{{
84         my %params=@_;
85         my $page=$params{page};
86         my $old=$params{oldpage};
87         my $new=$params{newpage};
88
89         $params{content} =~ s{(?<!\\)$link_regexp}{
90                 my $link=$2;
91                 if (bestlink($page, $2) eq $old) {
92                         if (index($2, "/") == 0) {
93                                 $link="/$new";
94                         }
95                         else {
96                                 $link=$new;
97                         }
98                 }
99                 defined $1
100                         ? ( "[[$1|$link".($3 ? "#$3" : "")."]]" )
101                         : ( "[[$link".   ($3 ? "#$3" : "")."]]" )
102         }eg;
103
104         return $params{content};
105 } #}}}
106
107 1