]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/link.pm
515a62bced96396a906dd6c84e2c5ec0335c0200
[git.ikiwiki.info.git] / IkiWiki / Plugin / link.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::link;
4 use warnings;
5 use strict;
6 use IkiWiki 2.00;
8 my $link_regexp;
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 } # }}}
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
43                         ([^\s\]#]+)             # 2: page to link to
44                         (?:
45                                 \#              # '#', beginning of anchor
46                                 ([^\s\]]+)      # 3: anchor text
47                         )?                      # optional
49                         \]\]                    # end of link
50                 }x,
51         }
52 } #}}}
54 sub linkify (@) { #{{{
55         my %params=@_;
56         my $page=$params{page};
57         my $destpage=$params{destpage};
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 } #}}}
74 sub scan (@) { #{{{
75         my %params=@_;
76         my $page=$params{page};
77         my $content=$params{content};
79         while ($content =~ /(?<!\\)$link_regexp/g) {
80                 push @{$links{$page}}, IkiWiki::linkpage($2);
81         }
82 } # }}}
84 sub renamepage (@) { #{{{
85         my %params=@_;
86         my $page=$params{page};
87         my $old=$params{oldpage};
88         my $new=$params{newpage};
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 of last component
97                                 my @bits=split("/", $link);
98                                 $link=join("/", @bits[0..$#bits-1], ucfirst($bits[$#bits]));
99                         }
100                         if (index($linktext, "/") == 0) {
101                                 # absolute link
102                                 $link="/$link";
103                         }
104                 }
105                 defined $1
106                         ? ( "[[$1|$link".($3 ? "#$3" : "")."]]" )
107                         : ( "[[$link".   ($3 ? "#$3" : "")."]]" )
108         }eg;
110         return $params{content};
111 } #}}}