Merge branch 'master' into autoconfig
[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 2.00;
8
9 my %tags;
10
11 sub import { #{{{
12         hook(type => "getopt", id => "tag", call => \&getopt);
13         hook(type => "getsetup", id => "tag", call => \&getsetup);
14         hook(type => "preprocess", id => "tag", call => \&preprocess_tag, scan => 1);
15         hook(type => "preprocess", id => "taglink", call => \&preprocess_taglink, scan => 1);
16         hook(type => "pagetemplate", id => "tag", call => \&pagetemplate);
17 } # }}}
18
19 sub getopt () { #{{{
20         eval q{use Getopt::Long};
21         error($@) if $@;
22         Getopt::Long::Configure('pass_through');
23         GetOptions("tagbase=s" => \$config{tagbase});
24 } #}}}
25
26 sub getsetup () { #{{{
27         return
28                 tagbase => {
29                         type => "string",
30                         example => "tag",
31                         description => "parent page tags are located under",
32                         safe => 1,
33                         rebuild => 1,
34                 },
35 } #}}}
36
37 sub tagpage ($) { #{{{
38         my $tag=shift;
39                         
40         if ($tag !~ m{^\.?/} &&
41             exists $config{tagbase} &&
42             defined $config{tagbase}) {
43                 $tag=$config{tagbase}."/".$tag;
44         }
45
46         return $tag;
47 } #}}}
48
49 sub preprocess_tag (@) { #{{{
50         if (! @_) {
51                 return "";
52         }
53         my %params=@_;
54         my $page = $params{page};
55         delete $params{page};
56         delete $params{destpage};
57         delete $params{preview};
58
59         foreach my $tag (keys %params) {
60                 $tag=IkiWiki::linkpage($tag);
61                 $tags{$page}{$tag}=1;
62                 # hidden WikiLink
63                 push @{$links{$page}}, tagpage($tag);
64         }
65                 
66         return "";
67 } # }}}
68
69 sub preprocess_taglink (@) { #{{{
70         if (! @_) {
71                 return "";
72         }
73         my %params=@_;
74         return join(" ", map {
75                 if (/(.*)\|(.*)/) {
76                         my $tag=IkiWiki::linkpage($2);
77                         $tags{$params{page}}{$tag}=1;
78                         push @{$links{$params{page}}}, tagpage($tag);
79                         return htmllink($params{page}, $params{destpage},
80                                 tagpage($tag),
81                                 linktext => IkiWiki::pagetitle($1));
82                 }
83                 else {
84                         my $tag=IkiWiki::linkpage($_);
85                         $tags{$params{page}}{$tag}=1;
86                         push @{$links{$params{page}}}, tagpage($tag);
87                         return htmllink($params{page}, $params{destpage},
88                                 tagpage($tag));
89                 }
90         }
91         grep {
92                 $_ ne 'page' && $_ ne 'destpage' && $_ ne 'preview'
93         } keys %params);
94 } # }}}
95
96 sub pagetemplate (@) { #{{{
97         my %params=@_;
98         my $page=$params{page};
99         my $destpage=$params{destpage};
100         my $template=$params{template};
101
102         $template->param(tags => [
103                 map { 
104                         link => htmllink($page, $destpage, tagpage($_),
105                                         rel => "tag")
106                 }, sort keys %{$tags{$page}}
107         ]) if exists $tags{$page} && %{$tags{$page}} && $template->query(name => "tags");
108
109         if ($template->query(name => "categories")) {
110                 # It's an rss/atom template. Add any categories.
111                 if (exists $tags{$page} && %{$tags{$page}}) {
112                         $template->param(categories => [map { category => $_ },
113                                 sort keys %{$tags{$page}}]);
114                 }
115         }
116 } # }}}
117
118 1