]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/tag.pm
tag: optionally write autocreated tags to the transient underlay
[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 sub import {
10         hook(type => "getopt", id => "tag", call => \&getopt);
11         hook(type => "getsetup", id => "tag", call => \&getsetup);
12         hook(type => "preprocess", id => "tag", call => \&preprocess_tag, scan => 1);
13         hook(type => "preprocess", id => "taglink", call => \&preprocess_taglink, scan => 1);
14         hook(type => "pagetemplate", id => "tag", call => \&pagetemplate);
15
16         IkiWiki::loadplugin("transient");
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 => 1,
42                         description => "autocreate new tag pages?",
43                         safe => 1,
44                         rebuild => undef,
45                 },
46                 tag_autocreate_commit => {
47                         type => "boolean",
48                         example => 1,
49                         default => 1,
50                         description => "commit autocreated tag pages",
51                         safe => 1,
52                         rebuild => 0,
53                 },
54 }
55
56 sub taglink ($) {
57         my $tag=shift;
58         
59         if ($tag !~ m{^/} &&
60             defined $config{tagbase}) {
61                 $tag="/".$config{tagbase}."/".$tag;
62                 $tag=~y#/#/#s; # squash dups
63         }
64
65         return $tag;
66 }
67
68 # Returns a tag name from a tag link
69 sub tagname ($) {
70         my $tag=shift;
71         if (defined $config{tagbase}) {
72                 $tag =~ s!^/\Q$config{tagbase}\E/!!;
73         } else {
74                 $tag =~ s!^\.?/!!;
75         }
76         return pagetitle($tag, 1);
77 }
78
79 sub htmllink_tag ($$$;@) {
80         my $page=shift;
81         my $destpage=shift;
82         my $tag=shift;
83         my %opts=@_;
84
85         return htmllink($page, $destpage, taglink($tag), %opts);
86 }
87
88 sub gentag ($) {
89         my $tag=shift;
90
91         if ($config{tag_autocreate} ||
92             ($config{tagbase} && ! defined $config{tag_autocreate})) {
93                 my $tagpage=taglink($tag);
94                 if ($tagpage=~/^\.\/(.*)/) {
95                         $tagpage=$1;
96                 }
97                 else {
98                         $tagpage=~s/^\///;
99                 }
100
101                 my $tagfile = newpagefile($tagpage, $config{default_pageext});
102
103                 add_autofile($tagfile, "tag", sub {
104                         my $message=sprintf(gettext("creating tag page %s"), $tagpage);
105                         debug($message);
106
107                         my $template=template("autotag.tmpl");
108                         $template->param(tagname => tagname($tag));
109                         $template->param(tag => $tag);
110
111                         my $dir = $config{srcdir};
112                         if (! $config{tag_autocreate_commit}) {
113                                 $dir = $IkiWiki::Plugin::transient::transientdir;
114                         }
115
116                         writefile($tagfile, $dir, $template->output);
117                         if ($config{rcs} && $config{tag_autocreate_commit}) {
118                                 IkiWiki::disable_commit_hook();
119                                 IkiWiki::rcs_add($tagfile);
120                                 IkiWiki::rcs_commit_staged(message => $message);
121                                 IkiWiki::enable_commit_hook();
122                         }
123                 });
124         }
125 }
126
127 sub preprocess_tag (@) {
128         if (! @_) {
129                 return "";
130         }
131         my %params=@_;
132         my $page = $params{page};
133         delete $params{page};
134         delete $params{destpage};
135         delete $params{preview};
136
137         foreach my $tag (keys %params) {
138                 $tag=linkpage($tag);
139                 
140                 # hidden WikiLink
141                 add_link($page, taglink($tag), 'tag');
142                 
143                 gentag($tag);
144         }
145                 
146         return "";
147 }
148
149 sub preprocess_taglink (@) {
150         if (! @_) {
151                 return "";
152         }
153         my %params=@_;
154         return join(" ", map {
155                 if (/(.*)\|(.*)/) {
156                         my $tag=linkpage($2);
157                         add_link($params{page}, taglink($tag), 'tag');
158                         gentag($tag);
159                         return htmllink_tag($params{page}, $params{destpage}, $tag,
160                                 linktext => pagetitle($1));
161                 }
162                 else {
163                         my $tag=linkpage($_);
164                         add_link($params{page}, taglink($tag), 'tag');
165                         gentag($tag);
166                         return htmllink_tag($params{page}, $params{destpage}, $tag);
167                 }
168         }
169         grep {
170                 $_ ne 'page' && $_ ne 'destpage' && $_ ne 'preview'
171         } keys %params);
172 }
173
174 sub pagetemplate (@) {
175         my %params=@_;
176         my $page=$params{page};
177         my $destpage=$params{destpage};
178         my $template=$params{template};
179
180         my $tags = $typedlinks{$page}{tag};
181
182         $template->param(tags => [
183                 map { 
184                         link => htmllink_tag($page, $destpage, $_,
185                                         rel => "tag", linktext => tagname($_))
186                 }, sort keys %$tags
187         ]) if defined $tags && %$tags && $template->query(name => "tags");
188
189         if ($template->query(name => "categories")) {
190                 # It's an rss/atom template. Add any categories.
191                 if (defined $tags && %$tags) {
192                         $template->param(categories => [map { category => tagname($_) },
193                                 sort keys %$tags]);
194                 }
195         }
196 }
197
198 package IkiWiki::PageSpec;
199
200 sub match_tagged ($$;@) {
201         my $page=shift;
202         my $glob=IkiWiki::Plugin::tag::taglink(shift);
203         return match_link($page, $glob, linktype => 'tag', @_);
204 }
205
206 1