Merge branch 'master' into po
[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                 plugin => {
29                         safe => 1,
30                         rebuild => undef,
31                 },
32                 tagbase => {
33                         type => "string",
34                         example => "tag",
35                         description => "parent page tags are located under",
36                         safe => 1,
37                         rebuild => 1,
38                 },
39 } #}}}
40
41 sub tagpage ($) { #{{{
42         my $tag=shift;
43                         
44         if ($tag !~ m{^\.?/} &&
45             defined $config{tagbase}) {
46                 $tag="/".$config{tagbase}."/".$tag;
47                 $tag=~y#/#/#s; # squash dups
48         }
49
50         return $tag;
51 } #}}}
52
53 sub taglink ($$$;@) { #{{{
54         my $page=shift;
55         my $destpage=shift;
56         my $tag=shift;
57         my %opts=@_;
58
59         return htmllink($page, $destpage, tagpage($tag), %opts);
60 } #}}}
61
62 sub preprocess_tag (@) { #{{{
63         if (! @_) {
64                 return "";
65         }
66         my %params=@_;
67         my $page = $params{page};
68         delete $params{page};
69         delete $params{destpage};
70         delete $params{preview};
71
72         foreach my $tag (keys %params) {
73                 $tag=linkpage($tag);
74                 $tags{$page}{$tag}=1;
75                 # hidden WikiLink
76                 push @{$links{$page}}, tagpage($tag);
77         }
78                 
79         return "";
80 } # }}}
81
82 sub preprocess_taglink (@) { #{{{
83         if (! @_) {
84                 return "";
85         }
86         my %params=@_;
87         return join(" ", map {
88                 if (/(.*)\|(.*)/) {
89                         my $tag=linkpage($2);
90                         $tags{$params{page}}{$tag}=1;
91                         push @{$links{$params{page}}}, tagpage($tag);
92                         return taglink($params{page}, $params{destpage}, $tag,
93                                 linktext => pagetitle($1));
94                 }
95                 else {
96                         my $tag=linkpage($_);
97                         $tags{$params{page}}{$tag}=1;
98                         push @{$links{$params{page}}}, tagpage($tag);
99                         return taglink($params{page}, $params{destpage}, $tag);
100                 }
101         }
102         grep {
103                 $_ ne 'page' && $_ ne 'destpage' && $_ ne 'preview'
104         } keys %params);
105 } # }}}
106
107 sub pagetemplate (@) { #{{{
108         my %params=@_;
109         my $page=$params{page};
110         my $destpage=$params{destpage};
111         my $template=$params{template};
112
113         $template->param(tags => [
114                 map { 
115                         link => taglink($page, $destpage, $_, rel => "tag")
116                 }, sort keys %{$tags{$page}}
117         ]) if exists $tags{$page} && %{$tags{$page}} && $template->query(name => "tags");
118
119         if ($template->query(name => "categories")) {
120                 # It's an rss/atom template. Add any categories.
121                 if (exists $tags{$page} && %{$tags{$page}}) {
122                         $template->param(categories => [map { category => $_ },
123                                 sort keys %{$tags{$page}}]);
124                 }
125         }
126 } # }}}
127
128 1