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