* Add exclude option in setup files, works same as --exclude.
[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 preprocess (@) { #{{{
27         if (! @_) {
28                 return "";
29         }
30         my %params=@_;
31         my $page = $params{page};
32         delete $params{page};
33         delete $params{destpage};
34
35         $tags{$page} = [];
36         foreach my $tag (keys %params) {
37                 if (exists $IkiWiki::config{tagbase} &&
38                     defined $IkiWiki::config{tagbase}) {
39                         $tag=$IkiWiki::config{tagbase}."/".$tag;
40                 }
41                 push @{$tags{$page}}, $tag;
42                 # hidden WikiLink
43                 push @{$IkiWiki::links{$page}}, $tag;
44         }
45                 
46         return "";
47 } # }}}
48
49 sub pagetemplate (@) { #{{{
50         my %params=@_;
51         my $page=$params{page};
52         my $destpage=$params{destpage};
53         my $template=$params{template};
54
55         $template->param(tags => [
56                 map { link => IkiWiki::htmllink($page, $destpage, $_) }, 
57                         @{$tags{$page}}
58         ]) if exists $tags{$page} && @{$tags{$page}} && $template->query(name => "tags");
59 } # }}}
60
61 1