]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/tag.pm
add checkbox to rename subpages too
[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             exists $config{tagbase} &&
46             defined $config{tagbase}) {
47                 $tag=$config{tagbase}."/".$tag;
48         }
49
50         return $tag;
51 } #}}}
52
53 sub preprocess_tag (@) { #{{{
54         if (! @_) {
55                 return "";
56         }
57         my %params=@_;
58         my $page = $params{page};
59         delete $params{page};
60         delete $params{destpage};
61         delete $params{preview};
62
63         foreach my $tag (keys %params) {
64                 $tag=IkiWiki::linkpage($tag);
65                 $tags{$page}{$tag}=1;
66                 # hidden WikiLink
67                 push @{$links{$page}}, tagpage($tag);
68         }
69                 
70         return "";
71 } # }}}
72
73 sub preprocess_taglink (@) { #{{{
74         if (! @_) {
75                 return "";
76         }
77         my %params=@_;
78         return join(" ", map {
79                 if (/(.*)\|(.*)/) {
80                         my $tag=IkiWiki::linkpage($2);
81                         $tags{$params{page}}{$tag}=1;
82                         push @{$links{$params{page}}}, tagpage($tag);
83                         return htmllink($params{page}, $params{destpage},
84                                 tagpage($tag),
85                                 linktext => IkiWiki::pagetitle($1));
86                 }
87                 else {
88                         my $tag=IkiWiki::linkpage($_);
89                         $tags{$params{page}}{$tag}=1;
90                         push @{$links{$params{page}}}, tagpage($tag);
91                         return htmllink($params{page}, $params{destpage},
92                                 tagpage($tag));
93                 }
94         }
95         grep {
96                 $_ ne 'page' && $_ ne 'destpage' && $_ ne 'preview'
97         } keys %params);
98 } # }}}
99
100 sub pagetemplate (@) { #{{{
101         my %params=@_;
102         my $page=$params{page};
103         my $destpage=$params{destpage};
104         my $template=$params{template};
105
106         $template->param(tags => [
107                 map { 
108                         link => htmllink($page, $destpage, tagpage($_),
109                                         rel => "tag")
110                 }, sort keys %{$tags{$page}}
111         ]) if exists $tags{$page} && %{$tags{$page}} && $template->query(name => "tags");
112
113         if ($template->query(name => "categories")) {
114                 # It's an rss/atom template. Add any categories.
115                 if (exists $tags{$page} && %{$tags{$page}}) {
116                         $template->param(categories => [map { category => $_ },
117                                 sort keys %{$tags{$page}}]);
118                 }
119         }
120 } # }}}
121
122 1