Merge branch 'master' into autotag
[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
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"), $tagpage);
83                         debug($message);
84
85                         my $template=template("autotag.tmpl");
86                         $template->param(tagname => IkiWiki::basename($tag));
87                         $template->param(tag => $tag);
88                         writefile($tagfile, $config{srcdir}, $template->output);
89                         if ($config{rcs}) {
90                                 IkiWiki::disable_commit_hook();
91                                 IkiWiki::rcs_add($tagfile);
92                                 IkiWiki::rcs_commit_staged($message, undef, undef);
93                                 IkiWiki::enable_commit_hook();
94                         }
95                 });
96         }
97 }
98
99 sub preprocess_tag (@) {
100         if (! @_) {
101                 return "";
102         }
103         my %params=@_;
104         my $page = $params{page};
105         delete $params{page};
106         delete $params{destpage};
107         delete $params{preview};
108
109         foreach my $tag (keys %params) {
110                 $tag=linkpage($tag);
111                 
112                 # hidden WikiLink
113                 add_link($page, taglink($tag), 'tag');
114                 
115                 gentag($tag);
116         }
117                 
118         return "";
119 }
120
121 sub preprocess_taglink (@) {
122         if (! @_) {
123                 return "";
124         }
125         my %params=@_;
126         return join(" ", map {
127                 if (/(.*)\|(.*)/) {
128                         my $tag=linkpage($2);
129                         add_link($params{page}, taglink($tag), 'tag');
130                         gentag($tag);
131                         return htmllink_tag($params{page}, $params{destpage}, $tag,
132                                 linktext => pagetitle($1));
133                 }
134                 else {
135                         my $tag=linkpage($_);
136                         add_link($params{page}, taglink($tag), 'tag');
137                         gentag($tag);
138                         return htmllink_tag($params{page}, $params{destpage}, $tag);
139                 }
140         }
141         grep {
142                 $_ ne 'page' && $_ ne 'destpage' && $_ ne 'preview'
143         } keys %params);
144 }
145
146 sub pagetemplate (@) {
147         my %params=@_;
148         my $page=$params{page};
149         my $destpage=$params{destpage};
150         my $template=$params{template};
151
152         my $tags = $typedlinks{$page}{tag};
153
154         $template->param(tags => [
155                 map { 
156                         link => htmllink_tag($page, $destpage, $_, rel => "tag")
157                 }, sort keys %$tags
158         ]) if defined $tags && %$tags && $template->query(name => "tags");
159
160         if ($template->query(name => "categories")) {
161                 # It's an rss/atom template. Add any categories.
162                 if (defined $tags && %$tags) {
163                         $template->param(categories => [map { category => $_ },
164                                 sort keys %$tags]);
165                 }
166         }
167 }
168
169 package IkiWiki::PageSpec;
170
171 sub match_tagged ($$;@) {
172         my $page=shift;
173         my $glob=IkiWiki::Plugin::tag::taglink(shift);
174         return match_link($page, $glob, linktype => 'tag', @_);
175 }
176
177 1