* Work on firming up the plugin interface:
[ikiwiki] / IkiWiki / Plugin / tag.pm
1 #!/usr/bin/perl
2 # Ikiwiki tag plugin.
3 package IkiWiki::Plugin::tag;
4
5 use warnings;
6 use strict;
7 use IkiWiki;
8
9 my %tags;
10
11 sub import { #{{{
12         hook(type => "getopt", id => "tag", call => \&getopt);
13         hook(type => "preprocess", id => "tag", call => \&preprocess);
14         hook(type => "pagetemplate", id => "tag", call => \&pagetemplate);
15 } # }}}
16
17 sub getopt () { #{{{
18         eval q{use Getopt::Long};
19         Getopt::Long::Configure('pass_through');
20         GetOptions("tagbase=s" => \$config{tagbase});
21 } #}}}
22
23 sub tagpage ($) { #{{{
24         my $tag=shift;
25                         
26         if (exists $config{tagbase} &&
27             defined $config{tagbase}) {
28                 $tag=$config{tagbase}."/".$tag;
29         }
30
31         return $tag;
32 } #}}}
33
34 sub preprocess (@) { #{{{
35         if (! @_) {
36                 return "";
37         }
38         my %params=@_;
39         my $page = $params{page};
40         delete $params{page};
41         delete $params{destpage};
42
43         $tags{$page} = [];
44         foreach my $tag (keys %params) {
45                 push @{$tags{$page}}, $tag;
46                 # hidden WikiLink
47                 push @{$links{$page}}, tagpage($tag);
48         }
49                 
50         return "";
51 } # }}}
52
53 sub pagetemplate (@) { #{{{
54         my %params=@_;
55         my $page=$params{page};
56         my $destpage=$params{destpage};
57         my $template=$params{template};
58
59         $template->param(tags => [
60                 map { 
61                         link => htmllink($page, $destpage, tagpage($_))
62                 }, @{$tags{$page}}
63         ]) if exists $tags{$page} && @{$tags{$page}} && $template->query(name => "tags");
64
65         if ($template->query(name => "pubdate")) {
66                 # It's an rss template. Add any categories.
67                 if (exists $tags{$page} && @{$tags{$page}}) {
68                         $template->param(categories => [map { category => $_ }, @{$tags{$page}}]);
69                 }
70         }
71 } # }}}
72
73 1