]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/link.pm
Correctly handle filenames starting with a dash in add/rm/mv.
[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 3.00;
8 my $link_regexp;
10 my $email_regexp = qr/^.+@.+\..+$/;
11 my $url_regexp = qr/^(?:[^:]+:\/\/|mailto:).*/i;
13 sub import {
14         hook(type => "getsetup", id => "link", call => \&getsetup);
15         hook(type => "checkconfig", id => "link", call => \&checkconfig);
16         hook(type => "linkify", id => "link", call => \&linkify);
17         hook(type => "scan", id => "link", call => \&scan);
18         hook(type => "renamepage", id => "link", call => \&renamepage);
19 }
21 sub getsetup () {
22         return
23                 plugin => {
24                         safe => 1,
25                         rebuild => 1,
26                         section => "core",
27                 },
28 }
30 sub checkconfig () {
31         if ($config{prefix_directives}) {
32                 $link_regexp = qr{
33                         \[\[(?=[^!])            # beginning of link
34                         (?:
35                                 ([^\]\|]+)      # 1: link text
36                                 \|              # followed by '|'
37                         )?                      # optional
38                         
39                         ([^\n\r\]#]+)           # 2: page to link to
40                         (?:
41                                 \#              # '#', beginning of anchor
42                                 ([^\s\]]+)      # 3: anchor text
43                         )?                      # optional
44                         
45                         \]\]                    # end of link
46                 }x;
47         }
48         else {
49                 $link_regexp = qr{
50                         \[\[                    # beginning of link
51                         (?:
52                                 ([^\]\|\n\s]+)  # 1: link text
53                                 \|              # followed by '|'
54                         )?                      # optional
56                         ([^\s\]#]+)             # 2: page to link to
57                         (?:
58                                 \#              # '#', beginning of anchor
59                                 ([^\s\]]+)      # 3: anchor text
60                         )?                      # optional
62                         \]\]                    # end of link
63                 }x;
64         }
65 }
67 sub is_externallink ($$;$) {
68         my $page = shift;
69         my $url = shift;
70         my $anchor = shift;
71         
72         if (defined $anchor) {
73                 $url.="#".$anchor;
74         }
76         return ($url =~ /$url_regexp|$email_regexp/)
77 }
79 sub externallink ($$;$) {
80         my $url = shift;
81         my $anchor = shift;
82         my $pagetitle = shift;
84         if (defined $anchor) {
85                 $url.="#".$anchor;
86         }
88         # build pagetitle
89         if (! $pagetitle) {
90                 $pagetitle = $url;
91                 # use only the email address as title for mailto: urls
92                 if ($pagetitle =~ /^mailto:.*/) {
93                         $pagetitle =~ s/^mailto:([^?]+).*/$1/;
94                 }
95         }
97         if ($url !~ /$url_regexp/) {
98                 # handle email addresses (without mailto:)
99                 $url = "mailto:" . $url;
100         }
102         return "<a href=\"$url\">$pagetitle</a>";
105 sub linkify (@) {
106         my %params=@_;
107         my $page=$params{page};
108         my $destpage=$params{destpage};
110         $params{content} =~ s{(\\?)$link_regexp}{
111                 defined $2
112                         ? ( $1 
113                                 ? "[[$2|$3".(defined $4 ? "#$4" : "")."]]" 
114                                 : is_externallink($page, $3, $4)
115                                         ? externallink($3, $4, $2)
116                                         : htmllink($page, $destpage, linkpage($3),
117                                                 anchor => $4, linktext => pagetitle($2)))
118                         : ( $1 
119                                 ? "[[$3".(defined $4 ? "#$4" : "")."]]"
120                                 : is_externallink($page, $3, $4)
121                                         ? externallink($3, $4)
122                                         : htmllink($page, $destpage, linkpage($3),
123                                                 anchor => $4))
124         }eg;
125         
126         return $params{content};
129 sub scan (@) {
130         my %params=@_;
131         my $page=$params{page};
132         my $content=$params{content};
134         while ($content =~ /(?<!\\)$link_regexp/g) {
135                 if (! is_externallink($page, $2, $3)) {
136                         add_link($page, linkpage($2));
137                 }
138         }
141 sub renamepage (@) {
142         my %params=@_;
143         my $page=$params{page};
144         my $old=$params{oldpage};
145         my $new=$params{newpage};
147         $params{content} =~ s{(?<!\\)($link_regexp)}{
148                 if (! is_externallink($page, $3, $4)) {
149                         my $linktext=$3;
150                         my $link=$linktext;
151                         if (bestlink($page, linkpage($linktext)) eq $old) {
152                                 $link=pagetitle($new, 1);
153                                 $link=~s/ /_/g;
154                                 if ($linktext =~ m/.*\/*?[A-Z]/) {
155                                         # preserve leading cap of last component
156                                         my @bits=split("/", $link);
157                                         $link=join("/", @bits[0..$#bits-1], ucfirst($bits[$#bits]));
158                                 }
159                                 if (index($linktext, "/") == 0) {
160                                         # absolute link
161                                         $link="/$link";
162                                 }
163                         }
164                         defined $2
165                                 ? ( "[[$2|$link".($4 ? "#$4" : "")."]]" )
166                                 : ( "[[$link".   ($4 ? "#$4" : "")."]]" )
167                 }
168                 else {
169                         $1
170                 }
171         }eg;
173         return $params{content};