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