2 package IkiWiki
::Plugin
::link;
10 my $email_regexp = qr/^.+@.+$/;
11 my $url_regexp = qr/^(?:[^:]+:\/\
/|mailto:).*/i;
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
);
31 if ($config{prefix_directives
}) {
33 \
[\
[(?
=[^!]) # beginning of link
35 ([^\
]\
|]+) # 1: link text
39 ([^\n\r\
]#]+) # 2: page to link to
41 \#
# '#', beginning of anchor
42 ([^\s\
]]+) # 3: anchor text
50 \
[\
[ # beginning of link
52 ([^\
]\
|\n\s
]+) # 1: link text
56 ([^\s\
]#]+) # 2: page to link to
58 \#
# '#', beginning of anchor
59 ([^\s\
]]+) # 3: anchor text
67 sub is_externallink
($$;$$) {
73 if (defined $anchor) {
77 if (! $force && $url =~ /$email_regexp/) {
78 # url looks like an email address, so we assume it
79 # is supposed to be an external link if there is no
80 # page with that name.
81 return (! (bestlink
($page, linkpage
($url))))
84 return ($url =~ /$url_regexp/)
87 sub externallink
($$;$) {
90 my $pagetitle = shift;
92 if (defined $anchor) {
99 # use only the email address as title for mailto: urls
100 if ($pagetitle =~ /^mailto:.*/) {
101 $pagetitle =~ s/^mailto:([^?]+).*/$1/;
105 if ($url !~ /$url_regexp/) {
106 # handle email addresses (without mailto:)
107 $url = "mailto:" . $url;
110 return "<a href=\"$url\">$pagetitle</a>";
115 my $page=$params{page
};
116 my $destpage=$params{destpage
};
118 $params{content
} =~ s
{(\\?
)$link_regexp}{
121 ?
"[[$2|$3".(defined $4 ?
"#$4" : "")."]]"
122 : is_externallink
($page, $3, $4)
123 ? externallink
($3, $4, $2)
124 : htmllink
($page, $destpage, linkpage
($3),
125 anchor
=> $4, linktext
=> pagetitle
($2)))
127 ?
"[[$3".(defined $4 ?
"#$4" : "")."]]"
128 : is_externallink
($page, $3, $4)
129 ? externallink
($3, $4)
130 : htmllink
($page, $destpage, linkpage
($3),
134 return $params{content
};
139 my $page=$params{page
};
140 my $content=$params{content
};
142 while ($content =~ /(?<!\\)$link_regexp/g) {
143 if (! is_externallink
($page, $2, $3, 1)) {
144 add_link
($page, linkpage
($2));
151 my $page=$params{page
};
152 my $old=$params{oldpage
};
153 my $new=$params{newpage
};
155 $params{content
} =~ s
{(?
<!\\)$link_regexp}{
156 if (! is_externallink
($page, $2, $3)) {
159 if (bestlink
($page, linkpage
($linktext)) eq $old) {
160 $link=pagetitle
($new, 1);
162 if ($linktext =~ m/.*\/*?
[A
-Z
]/) {
163 # preserve leading cap of last component
164 my @bits=split("/", $link);
165 $link=join("/", @bits[0..$#bits-1], ucfirst($bits[$#bits]));
167 if (index($linktext, "/") == 0) {
173 ?
( "[[$1|$link".($3 ?
"#$3" : "")."]]" )
174 : ( "[[$link". ($3 ?
"#$3" : "")."]]" )
178 return $params{content
};