Make sure deleted tag pages don't get recreated.
[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 3.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                 tag_autocreate => {
40                         type => "boolean",
41                         example => 0,
42                         description => "Autocreate new tag pages",
43                         safe => 1,
44                         rebuild => 1,
45                 },
46 }
47
48 sub tagpage ($) {
49         my $tag=shift;
50                         
51         if ($tag !~ m{^\.?/} &&
52             defined $config{tagbase}) {
53                 $tag="/".$config{tagbase}."/".$tag;
54                 $tag=~y#/#/#s; # squash dups
55         }
56
57         return $tag;
58 }
59
60 sub taglink ($$$;@) {
61         my $page=shift;
62         my $destpage=shift;
63         my $tag=shift;
64         my %opts=@_;
65
66         return htmllink($page, $destpage, tagpage($tag), %opts);
67 }
68
69 sub gentag ($) {
70         my $tag=shift;
71         if (defined $config{tag_autocreate} && $config{tag_autocreate}) {
72                 my $tagfile = newpagefile(tagpage($tag), $config{default_pageext});
73                 $tagfile=~s/^\///;
74
75                 return if (! add_autofile($tagfile, "tag"));
76
77                 debug(sprintf(gettext("creating tag page %s"), $tag));
78
79                 my $template=template("autotag.tmpl");
80                 $template->param(tag => $tag);
81                 writefile($tagfile, $config{srcdir}, $template->output);
82         }
83 }
84
85 sub preprocess_tag (@) {
86         if (! @_) {
87                 return "";
88         }
89         my %params=@_;
90         my $page = $params{page};
91         delete $params{page};
92         delete $params{destpage};
93         delete $params{preview};
94
95         foreach my $tag (keys %params) {
96                 $tag=linkpage($tag);
97                 $tags{$page}{$tag}=1;
98
99                 # add tagpage if necessary
100                 gentag($tag);
101
102                 # hidden WikiLink
103                 add_link($page, tagpage($tag));
104         }
105                 
106         return "";
107 }
108
109 sub preprocess_taglink (@) {
110         if (! @_) {
111                 return "";
112         }
113         my %params=@_;
114         return join(" ", map {
115                 if (/(.*)\|(.*)/) {
116                         my $tag=linkpage($2);
117                         $tags{$params{page}}{$tag}=1;
118                         add_link($params{page}, tagpage($tag));
119                         return taglink($params{page}, $params{destpage}, $tag,
120                                 linktext => pagetitle($1));
121                 }
122                 else {
123                         my $tag=linkpage($_);
124                         $tags{$params{page}}{$tag}=1;
125                         add_link($params{page}, tagpage($tag));
126                         return taglink($params{page}, $params{destpage}, $tag);
127                 }
128         }
129         grep {
130                 $_ ne 'page' && $_ ne 'destpage' && $_ ne 'preview'
131         } keys %params);
132 }
133
134 sub pagetemplate (@) {
135         my %params=@_;
136         my $page=$params{page};
137         my $destpage=$params{destpage};
138         my $template=$params{template};
139
140         $template->param(tags => [
141                 map { 
142                         link => taglink($page, $destpage, $_, rel => "tag")
143                 }, sort keys %{$tags{$page}}
144         ]) if exists $tags{$page} && %{$tags{$page}} && $template->query(name => "tags");
145
146         if ($template->query(name => "categories")) {
147                 # It's an rss/atom template. Add any categories.
148                 if (exists $tags{$page} && %{$tags{$page}}) {
149                         $template->param(categories => [map { category => $_ },
150                                 sort keys %{$tags{$page}}]);
151                 }
152         }
153 }
154
155 package IkiWiki::PageSpec;
156
157 sub match_tagged ($$;@) {
158         my $page = shift;
159         my $glob = shift;
160         return match_link($page, IkiWiki::Plugin::tag::tagpage($glob));
161 }
162
163 1