some comments
[ikiwiki] / 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 1