2008-02-12 04:48:27 +01:00
|
|
|
#!/usr/bin/perl
|
|
|
|
package IkiWiki::Plugin::link;
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
2008-12-23 22:34:19 +01:00
|
|
|
use IkiWiki 3.00;
|
2008-02-12 04:48:27 +01:00
|
|
|
|
|
|
|
my $link_regexp;
|
|
|
|
|
2011-06-30 01:09:23 +02:00
|
|
|
my $email_regexp = qr/^.+@.+\..+$/;
|
2010-06-14 01:18:33 +02:00
|
|
|
my $url_regexp = qr/^(?:[^:]+:\/\/|mailto:).*/i;
|
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub import {
|
2008-08-03 22:40:12 +02:00
|
|
|
hook(type => "getsetup", id => "link", call => \&getsetup);
|
2008-02-12 04:48:27 +01:00
|
|
|
hook(type => "checkconfig", id => "link", call => \&checkconfig);
|
|
|
|
hook(type => "linkify", id => "link", call => \&linkify);
|
|
|
|
hook(type => "scan", id => "link", call => \&scan);
|
2008-07-24 00:57:27 +02:00
|
|
|
hook(type => "renamepage", id => "link", call => \&renamepage);
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-02-12 04:48:27 +01:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub getsetup () {
|
2008-08-03 22:40:12 +02:00
|
|
|
return
|
|
|
|
plugin => {
|
|
|
|
safe => 1,
|
|
|
|
rebuild => 1,
|
2010-02-12 04:24:15 +01:00
|
|
|
section => "core",
|
2008-08-03 22:40:12 +02:00
|
|
|
},
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-08-03 22:40:12 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub checkconfig () {
|
2008-02-12 04:48:27 +01:00
|
|
|
if ($config{prefix_directives}) {
|
|
|
|
$link_regexp = qr{
|
|
|
|
\[\[(?=[^!]) # beginning of link
|
|
|
|
(?:
|
|
|
|
([^\]\|]+) # 1: link text
|
|
|
|
\| # followed by '|'
|
|
|
|
)? # optional
|
|
|
|
|
|
|
|
([^\n\r\]#]+) # 2: page to link to
|
|
|
|
(?:
|
|
|
|
\# # '#', beginning of anchor
|
|
|
|
([^\s\]]+) # 3: anchor text
|
|
|
|
)? # optional
|
|
|
|
|
|
|
|
\]\] # end of link
|
|
|
|
}x;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$link_regexp = qr{
|
|
|
|
\[\[ # beginning of link
|
|
|
|
(?:
|
|
|
|
([^\]\|\n\s]+) # 1: link text
|
|
|
|
\| # followed by '|'
|
|
|
|
)? # optional
|
|
|
|
|
|
|
|
([^\s\]#]+) # 2: page to link to
|
|
|
|
(?:
|
|
|
|
\# # '#', beginning of anchor
|
|
|
|
([^\s\]]+) # 3: anchor text
|
|
|
|
)? # optional
|
|
|
|
|
|
|
|
\]\] # end of link
|
2010-06-14 01:18:33 +02:00
|
|
|
}x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-30 00:35:29 +02:00
|
|
|
sub is_externallink ($$;$) {
|
2010-06-14 01:18:33 +02:00
|
|
|
my $page = shift;
|
|
|
|
my $url = shift;
|
2010-06-23 19:57:27 +02:00
|
|
|
my $anchor = shift;
|
|
|
|
|
|
|
|
if (defined $anchor) {
|
|
|
|
$url.="#".$anchor;
|
|
|
|
}
|
|
|
|
|
2011-06-30 01:09:23 +02:00
|
|
|
return ($url =~ /$url_regexp|$email_regexp/)
|
2010-06-14 01:18:33 +02:00
|
|
|
}
|
|
|
|
|
2010-06-23 19:57:27 +02:00
|
|
|
sub externallink ($$;$) {
|
2010-06-14 01:18:33 +02:00
|
|
|
my $url = shift;
|
2010-06-23 19:57:27 +02:00
|
|
|
my $anchor = shift;
|
2010-06-14 01:18:33 +02:00
|
|
|
my $pagetitle = shift;
|
|
|
|
|
2010-06-23 19:57:27 +02:00
|
|
|
if (defined $anchor) {
|
|
|
|
$url.="#".$anchor;
|
|
|
|
}
|
|
|
|
|
2010-06-14 01:18:33 +02:00
|
|
|
# build pagetitle
|
2010-06-23 19:40:10 +02:00
|
|
|
if (! $pagetitle) {
|
2010-06-14 01:18:33 +02:00
|
|
|
$pagetitle = $url;
|
|
|
|
# use only the email address as title for mailto: urls
|
|
|
|
if ($pagetitle =~ /^mailto:.*/) {
|
|
|
|
$pagetitle =~ s/^mailto:([^?]+).*/$1/;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-23 19:40:10 +02:00
|
|
|
if ($url !~ /$url_regexp/) {
|
|
|
|
# handle email addresses (without mailto:)
|
2010-06-14 01:18:33 +02:00
|
|
|
$url = "mailto:" . $url;
|
|
|
|
}
|
|
|
|
|
|
|
|
return "<a href=\"$url\">$pagetitle</a>";
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-02-12 04:48:27 +01:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub linkify (@) {
|
2008-02-12 04:48:27 +01:00
|
|
|
my %params=@_;
|
|
|
|
my $page=$params{page};
|
|
|
|
my $destpage=$params{destpage};
|
|
|
|
|
|
|
|
$params{content} =~ s{(\\?)$link_regexp}{
|
|
|
|
defined $2
|
|
|
|
? ( $1
|
2010-06-23 19:57:27 +02:00
|
|
|
? "[[$2|$3".(defined $4 ? "#$4" : "")."]]"
|
|
|
|
: is_externallink($page, $3, $4)
|
|
|
|
? externallink($3, $4, $2)
|
2010-06-14 01:18:33 +02:00
|
|
|
: htmllink($page, $destpage, linkpage($3),
|
|
|
|
anchor => $4, linktext => pagetitle($2)))
|
2008-02-12 04:48:27 +01:00
|
|
|
: ( $1
|
2010-06-23 19:57:27 +02:00
|
|
|
? "[[$3".(defined $4 ? "#$4" : "")."]]"
|
|
|
|
: is_externallink($page, $3, $4)
|
|
|
|
? externallink($3, $4)
|
2010-06-14 01:18:33 +02:00
|
|
|
: htmllink($page, $destpage, linkpage($3),
|
|
|
|
anchor => $4))
|
2008-02-12 04:48:27 +01:00
|
|
|
}eg;
|
|
|
|
|
|
|
|
return $params{content};
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-02-12 04:48:27 +01:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub scan (@) {
|
2008-02-12 04:48:27 +01:00
|
|
|
my %params=@_;
|
|
|
|
my $page=$params{page};
|
|
|
|
my $content=$params{content};
|
|
|
|
|
|
|
|
while ($content =~ /(?<!\\)$link_regexp/g) {
|
2011-06-30 00:35:29 +02:00
|
|
|
if (! is_externallink($page, $2, $3)) {
|
2010-06-14 01:18:33 +02:00
|
|
|
add_link($page, linkpage($2));
|
|
|
|
}
|
2008-02-12 04:48:27 +01:00
|
|
|
}
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-02-12 04:48:27 +01:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub renamepage (@) {
|
2008-07-24 00:14:20 +02:00
|
|
|
my %params=@_;
|
|
|
|
my $page=$params{page};
|
|
|
|
my $old=$params{oldpage};
|
|
|
|
my $new=$params{newpage};
|
|
|
|
|
|
|
|
$params{content} =~ s{(?<!\\)$link_regexp}{
|
2010-06-23 19:57:27 +02:00
|
|
|
if (! is_externallink($page, $2, $3)) {
|
2010-06-14 01:18:33 +02:00
|
|
|
my $linktext=$2;
|
|
|
|
my $link=$linktext;
|
|
|
|
if (bestlink($page, linkpage($linktext)) eq $old) {
|
|
|
|
$link=pagetitle($new, 1);
|
|
|
|
$link=~s/ /_/g;
|
|
|
|
if ($linktext =~ m/.*\/*?[A-Z]/) {
|
|
|
|
# preserve leading cap of last component
|
|
|
|
my @bits=split("/", $link);
|
|
|
|
$link=join("/", @bits[0..$#bits-1], ucfirst($bits[$#bits]));
|
|
|
|
}
|
|
|
|
if (index($linktext, "/") == 0) {
|
|
|
|
# absolute link
|
|
|
|
$link="/$link";
|
|
|
|
}
|
2008-07-24 00:14:20 +02:00
|
|
|
}
|
2010-06-14 01:18:33 +02:00
|
|
|
defined $1
|
|
|
|
? ( "[[$1|$link".($3 ? "#$3" : "")."]]" )
|
|
|
|
: ( "[[$link". ($3 ? "#$3" : "")."]]" )
|
2008-07-24 00:14:20 +02:00
|
|
|
}
|
|
|
|
}eg;
|
|
|
|
|
|
|
|
return $params{content};
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-07-24 00:14:20 +02:00
|
|
|
|
2008-02-12 04:48:27 +01:00
|
|
|
1
|