refactor autofiles
[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 => 1,
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 (defined $config{tag_autocreate} && $config{tag_autocreate}) {
70                 my $tagfile = newpagefile(tagpage($tag), $config{default_pageext});
71                 $tagfile=~s/^\///;
72
73                 add_autofile($tagfile, 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                 # add tagpage if necessary
100                 gentag($tag);
101         }
102                 
103         return "";
104 }
105
106 sub preprocess_taglink (@) {
107         if (! @_) {
108                 return "";
109         }
110         my %params=@_;
111         return join(" ", map {
112                 if (/(.*)\|(.*)/) {
113                         my $tag=linkpage($2);
114                         add_link($params{page}, tagpage($tag), '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                         return taglink($params{page}, $params{destpage}, $tag);
122                 }
123         }
124         grep {
125                 $_ ne 'page' && $_ ne 'destpage' && $_ ne 'preview'
126         } keys %params);
127 }
128
129 sub pagetemplate (@) {
130         my %params=@_;
131         my $page=$params{page};
132         my $destpage=$params{destpage};
133         my $template=$params{template};
134
135         my $tags = $typedlinks{$page}{tag};
136
137         $template->param(tags => [
138                 map { 
139                         link => taglink($page, $destpage, $_, rel => "tag")
140                 }, sort keys %$tags
141         ]) if defined $tags && %$tags && $template->query(name => "tags");
142
143         if ($template->query(name => "categories")) {
144                 # It's an rss/atom template. Add any categories.
145                 if (defined $tags && %$tags) {
146                         $template->param(categories => [map { category => $_ },
147                                 sort keys %$tags]);
148                 }
149         }
150 }
151
152 package IkiWiki::PageSpec;
153
154 sub match_tagged ($$;@) {
155         return match_link($_[0], IkiWiki::Plugin::tag::tagpage($_[1]), linktype => 'tag');
156 }
157
158 1