2006-07-27 23:38:02 +02:00
|
|
|
#!/usr/bin/perl
|
|
|
|
# Ikiwiki tag plugin.
|
|
|
|
package IkiWiki::Plugin::tag;
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
2007-04-27 04:55:52 +02:00
|
|
|
use IkiWiki 2.00;
|
2006-07-27 23:38:02 +02:00
|
|
|
|
2006-07-28 01:08:03 +02:00
|
|
|
my %tags;
|
2006-07-27 23:38:02 +02:00
|
|
|
|
|
|
|
sub import { #{{{
|
2006-09-10 00:50:27 +02:00
|
|
|
hook(type => "getopt", id => "tag", call => \&getopt);
|
2007-09-20 20:59:27 +02:00
|
|
|
hook(type => "preprocess", id => "tag", call => \&preprocess_tag, scan => 1);
|
|
|
|
hook(type => "preprocess", id => "taglink", call => \&preprocess_taglink, scan => 1);
|
2006-09-10 00:50:27 +02:00
|
|
|
hook(type => "pagetemplate", id => "tag", call => \&pagetemplate);
|
2006-07-27 23:38:02 +02:00
|
|
|
} # }}}
|
|
|
|
|
2006-07-28 07:26:49 +02:00
|
|
|
sub getopt () { #{{{
|
|
|
|
eval q{use Getopt::Long};
|
2006-11-08 22:03:33 +01:00
|
|
|
error($@) if $@;
|
2006-07-28 07:26:49 +02:00
|
|
|
Getopt::Long::Configure('pass_through');
|
2006-09-10 00:50:27 +02:00
|
|
|
GetOptions("tagbase=s" => \$config{tagbase});
|
2006-07-28 07:26:49 +02:00
|
|
|
} #}}}
|
|
|
|
|
2006-07-29 09:25:17 +02:00
|
|
|
sub tagpage ($) { #{{{
|
|
|
|
my $tag=shift;
|
|
|
|
|
2006-09-10 00:50:27 +02:00
|
|
|
if (exists $config{tagbase} &&
|
|
|
|
defined $config{tagbase}) {
|
|
|
|
$tag=$config{tagbase}."/".$tag;
|
2006-07-29 09:25:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $tag;
|
|
|
|
} #}}}
|
|
|
|
|
2007-09-20 20:59:27 +02:00
|
|
|
sub preprocess_tag (@) { #{{{
|
2006-07-27 23:38:02 +02:00
|
|
|
if (! @_) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
my %params=@_;
|
|
|
|
my $page = $params{page};
|
|
|
|
delete $params{page};
|
2006-07-28 01:47:13 +02:00
|
|
|
delete $params{destpage};
|
2007-03-07 06:53:47 +01:00
|
|
|
delete $params{preview};
|
2006-07-27 23:38:02 +02:00
|
|
|
|
|
|
|
foreach my $tag (keys %params) {
|
2007-04-04 23:08:01 +02:00
|
|
|
$tag=IkiWiki::linkpage($tag);
|
2007-02-14 05:05:08 +01:00
|
|
|
$tags{$page}{$tag}=1;
|
2006-07-27 23:38:02 +02:00
|
|
|
# hidden WikiLink
|
2006-09-10 00:50:27 +02:00
|
|
|
push @{$links{$page}}, tagpage($tag);
|
2006-07-27 23:38:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
} # }}}
|
|
|
|
|
2007-09-20 20:59:27 +02:00
|
|
|
sub preprocess_taglink (@) { #{{{
|
|
|
|
if (! @_) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
my %params=@_;
|
2007-09-22 19:07:51 +02:00
|
|
|
return join(" ", map {
|
|
|
|
if (/(.*)\|(.*)/) {
|
|
|
|
my $tag=IkiWiki::linkpage($2);
|
|
|
|
$tags{$params{page}}{$tag}=1;
|
2007-09-24 18:41:29 +02:00
|
|
|
push @{$links{$params{page}}}, tagpage($tag);
|
2007-09-22 19:07:51 +02:00
|
|
|
return htmllink($params{page}, $params{destpage},
|
|
|
|
tagpage($tag),
|
|
|
|
linktext => IkiWiki::pagetitle($1));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
my $tag=IkiWiki::linkpage($_);
|
|
|
|
$tags{$params{page}}{$tag}=1;
|
2007-09-24 18:41:29 +02:00
|
|
|
push @{$links{$params{page}}}, tagpage($tag);
|
2007-09-22 19:07:51 +02:00
|
|
|
return htmllink($params{page}, $params{destpage},
|
|
|
|
tagpage($tag));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
grep {
|
|
|
|
$_ ne 'page' && $_ ne 'destpage' && $_ ne 'preview'
|
|
|
|
} keys %params);
|
2007-09-20 20:59:27 +02:00
|
|
|
} # }}}
|
|
|
|
|
2006-07-28 01:41:58 +02:00
|
|
|
sub pagetemplate (@) { #{{{
|
|
|
|
my %params=@_;
|
|
|
|
my $page=$params{page};
|
|
|
|
my $destpage=$params{destpage};
|
|
|
|
my $template=$params{template};
|
2006-07-28 01:08:03 +02:00
|
|
|
|
2006-07-28 02:21:13 +02:00
|
|
|
$template->param(tags => [
|
2006-07-29 09:25:17 +02:00
|
|
|
map {
|
2007-08-05 22:48:13 +02:00
|
|
|
link => htmllink($page, $destpage, tagpage($_),
|
|
|
|
rel => "tag")
|
2007-02-14 05:05:08 +01:00
|
|
|
}, sort keys %{$tags{$page}}
|
|
|
|
]) if exists $tags{$page} && %{$tags{$page}} && $template->query(name => "tags");
|
2006-07-29 09:25:17 +02:00
|
|
|
|
2007-01-14 21:14:36 +01:00
|
|
|
if ($template->query(name => "categories")) {
|
|
|
|
# It's an rss/atom template. Add any categories.
|
2007-02-14 05:05:08 +01:00
|
|
|
if (exists $tags{$page} && %{$tags{$page}}) {
|
|
|
|
$template->param(categories => [map { category => $_ },
|
|
|
|
sort keys %{$tags{$page}}]);
|
2006-07-29 09:25:17 +02:00
|
|
|
}
|
|
|
|
}
|
2006-07-28 01:08:03 +02:00
|
|
|
} # }}}
|
|
|
|
|
2006-07-27 23:38:02 +02:00
|
|
|
1
|