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