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