commit autocreated tag pages
[ikiwiki] / 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
17 sub getopt () {
18         eval q{use Getopt::Long};
19         error($@) if $@;
20         Getopt::Long::Configure('pass_through');
21         GetOptions("tagbase=s" => \$config{tagbase});
22 }
23
24 sub getsetup () {
25         return
26                 plugin => {
27                         safe => 1,
28                         rebuild => undef,
29                 },
30                 tagbase => {
31                         type => "string",
32                         example => "tag",
33                         description => "parent page tags are located under",
34                         safe => 1,
35                         rebuild => 1,
36                 },
37                 tag_autocreate => {
38                         type => "boolean",
39                         example => 0,
40                         description => "autocreate new tag pages?",
41                         safe => 1,
42                         rebuild => undef,
43                 },
44 }
45
46 sub taglink ($) {
47         my $tag=shift;
48         
49         if ($tag !~ m{^\.?/} &&
50             defined $config{tagbase}) {
51                 $tag="/".$config{tagbase}."/".$tag;
52                 $tag=~y#/#/#s; # squash dups
53         }
54
55         return $tag;
56 }
57
58 sub htmllink_tag ($$$;@) {
59         my $page=shift;
60         my $destpage=shift;
61         my $tag=shift;
62         my %opts=@_;
63
64         return htmllink($page, $destpage, taglink($tag), %opts);
65 }
66
67 sub gentag ($) {
68         my $tag=shift;
69
70         if ($config{tag_autocreate}) {
71                 my $tagpage=taglink($tag);
72                 if ($tagpage=~/^\.\/(.*)/) {
73                         $tagpage=$1;
74                 }
75                 else {
76                         $tagpage=~s/^\///;
77                 }
78
79                 my $tagfile = newpagefile($tagpage, $config{default_pageext});
80
81                 add_autofile($tagfile, "tag", sub {
82                         my $message=sprintf(gettext("creating tag page %s"), $tag);
83                         debug($message);
84
85                         my $template=template("autotag.tmpl");
86                         $template->param(tag => $tag);
87                         writefile($tagfile, $config{srcdir}, $template->output);
88                         if ($config{rcs}) {
89                                 IkiWiki::disable_commit_hook();
90                                 IkiWiki::rcs_add($tagfile);
91                                 IkiWiki::rcs_commit_staged($message, undef, undef);
92                                 IkiWiki::enable_commit_hook();
93                         }
94                 });
95         }
96 }
97
98 sub preprocess_tag (@) {
99         if (! @_) {
100                 return "";
101         }
102         my %params=@_;
103         my $page = $params{page};
104         delete $params{page};
105         delete $params{destpage};
106         delete $params{preview};
107
108         foreach my $tag (keys %params) {
109                 $tag=linkpage($tag);
110                 
111                 # hidden WikiLink
112                 add_link($page, taglink($tag), 'tag');
113                 
114                 gentag($tag);
115         }
116                 
117         return "";
118 }
119
120 sub preprocess_taglink (@) {
121         if (! @_) {
122                 return "";
123         }
124         my %params=@_;
125         return join(" ", map {
126                 if (/(.*)\|(.*)/) {
127                         my $tag=linkpage($2);
128                         add_link($params{page}, taglink($tag), 'tag');
129                         gentag($tag);
130                         return htmllink_tag($params{page}, $params{destpage}, $tag,
131                                 linktext => pagetitle($1));
132                 }
133                 else {
134                         my $tag=linkpage($_);
135                         add_link($params{page}, taglink($tag), 'tag');
136                         gentag($tag);
137                         return htmllink_tag($params{page}, $params{destpage}, $tag);
138                 }
139         }
140         grep {
141                 $_ ne 'page' && $_ ne 'destpage' && $_ ne 'preview'
142         } keys %params);
143 }
144
145 sub pagetemplate (@) {
146         my %params=@_;
147         my $page=$params{page};
148         my $destpage=$params{destpage};
149         my $template=$params{template};
150
151         my $tags = $typedlinks{$page}{tag};
152
153         $template->param(tags => [
154                 map { 
155                         link => htmllink_tag($page, $destpage, $_, rel => "tag")
156                 }, sort keys %$tags
157         ]) if defined $tags && %$tags && $template->query(name => "tags");
158
159         if ($template->query(name => "categories")) {
160                 # It's an rss/atom template. Add any categories.
161                 if (defined $tags && %$tags) {
162                         $template->param(categories => [map { category => $_ },
163                                 sort keys %$tags]);
164                 }
165         }
166 }
167
168 package IkiWiki::PageSpec;
169
170 sub match_tagged ($$;@) {
171         return match_link($_[0], IkiWiki::Plugin::tag::taglink($_[1]), linktype => 'tag');
172 }
173
174 1