]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/tag.pm
* Set die_on_bad_params => 0 for all templates, to allow users to modify
[ikiwiki.git] / 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         IkiWiki::hook(type => "getopt", id => "tag",
13                 call => \&getopt);
14         IkiWiki::hook(type => "preprocess", id => "tag",
15                 call => \&preprocess);
16         IkiWiki::hook(type => "pagetemplate", id => "tag",
17                 call => \&pagetemplate);
18 } # }}}
19
20 sub getopt () { #{{{
21         eval q{use Getopt::Long};
22         Getopt::Long::Configure('pass_through');
23         GetOptions("tagbase=s" => \$IkiWiki::config{tagbase});
24 } #}}}
25
26 sub tagpage ($) { #{{{
27         my $tag=shift;
28                         
29         if (exists $IkiWiki::config{tagbase} &&
30             defined $IkiWiki::config{tagbase}) {
31                 $tag=$IkiWiki::config{tagbase}."/".$tag;
32         }
33
34         return $tag;
35 } #}}}
36
37 sub preprocess (@) { #{{{
38         if (! @_) {
39                 return "";
40         }
41         my %params=@_;
42         my $page = $params{page};
43         delete $params{page};
44         delete $params{destpage};
45
46         $tags{$page} = [];
47         foreach my $tag (keys %params) {
48                 push @{$tags{$page}}, $tag;
49                 # hidden WikiLink
50                 push @{$IkiWiki::links{$page}}, tagpage($tag);
51         }
52                 
53         return "";
54 } # }}}
55
56 sub pagetemplate (@) { #{{{
57         my %params=@_;
58         my $page=$params{page};
59         my $destpage=$params{destpage};
60         my $template=$params{template};
61
62         $template->param(tags => [
63                 map { 
64                         link => IkiWiki::htmllink($page, $destpage, tagpage($_))
65                 }, @{$tags{$page}}
66         ]) if exists $tags{$page} && @{$tags{$page}} && $template->query(name => "tags");
67
68         if ($template->query(name => "pubdate")) {
69                 # It's an rss template. Add any categories.
70                 if (exists $tags{$page} && @{$tags{$page}}) {
71                         $template->param(categories => [map { category => $_ }, @{$tags{$page}}]);
72                 }
73         }
74 } # }}}
75
76 1