Use add_autofile() in tag.pm
[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                 return if (srcfile($tagfile,1));
75
76                 debug(sprintf(gettext("creating tag page %s"), $tag));
77
78                 my $template=template("autotag.tmpl");
79                 $template->param(tag => $tag);
80                 writefile($tagfile, $config{srcdir}, $template->output);
81
82                 IkiWiki::add_autofile("$config{srcdir}/$tagfile");
83         }
84 }
85
86 sub preprocess_tag (@) {
87         if (! @_) {
88                 return "";
89         }
90         my %params=@_;
91         my $page = $params{page};
92         delete $params{page};
93         delete $params{destpage};
94         delete $params{preview};
95
96         foreach my $tag (keys %params) {
97                 $tag=linkpage($tag);
98                 $tags{$page}{$tag}=1;
99
100                 # add tagpage if necessary
101                 gentag($tag);
102
103                 # hidden WikiLink
104                 add_link($page, tagpage($tag));
105         }
106                 
107         return "";
108 }
109
110 sub preprocess_taglink (@) {
111         if (! @_) {
112                 return "";
113         }
114         my %params=@_;
115         return join(" ", map {
116                 if (/(.*)\|(.*)/) {
117                         my $tag=linkpage($2);
118                         $tags{$params{page}}{$tag}=1;
119                         add_link($params{page}, tagpage($tag));
120                         return taglink($params{page}, $params{destpage}, $tag,
121                                 linktext => pagetitle($1));
122                 }
123                 else {
124                         my $tag=linkpage($_);
125                         $tags{$params{page}}{$tag}=1;
126                         add_link($params{page}, tagpage($tag));
127                         return taglink($params{page}, $params{destpage}, $tag);
128                 }
129         }
130         grep {
131                 $_ ne 'page' && $_ ne 'destpage' && $_ ne 'preview'
132         } keys %params);
133 }
134
135 sub pagetemplate (@) {
136         my %params=@_;
137         my $page=$params{page};
138         my $destpage=$params{destpage};
139         my $template=$params{template};
140
141         $template->param(tags => [
142                 map { 
143                         link => taglink($page, $destpage, $_, rel => "tag")
144                 }, sort keys %{$tags{$page}}
145         ]) if exists $tags{$page} && %{$tags{$page}} && $template->query(name => "tags");
146
147         if ($template->query(name => "categories")) {
148                 # It's an rss/atom template. Add any categories.
149                 if (exists $tags{$page} && %{$tags{$page}}) {
150                         $template->param(categories => [map { category => $_ },
151                                 sort keys %{$tags{$page}}]);
152                 }
153         }
154 }
155
156 package IkiWiki::PageSpec;
157
158 sub match_tagged ($$;@) {
159         my $page = shift;
160         my $glob = shift;
161         return match_link($page, IkiWiki::Plugin::tag::tagpage($glob));
162 }
163
164 1