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