]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/link.pm
fix link
[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 } # }}}
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
42                         ([^\s\]#]+)             # 2: page to link to
43                         (?:
44                                 \#              # '#', beginning of anchor
45                                 ([^\s\]]+)      # 3: anchor text
46                         )?                      # optional
48                         \]\]                    # end of link
49                 }x,
50         }
51 } #}}}
53 sub linkify (@) { #{{{
54         my %params=@_;
55         my $page=$params{page};
56         my $destpage=$params{destpage};
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 } #}}}
73 sub scan (@) { #{{{
74         my %params=@_;
75         my $page=$params{page};
76         my $content=$params{content};
78         while ($content =~ /(?<!\\)$link_regexp/g) {
79                 push @{$links{$page}}, IkiWiki::linkpage($2);
80         }
81 } # }}}
83 1