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