]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/tag.pm
c07d0131e88f469fca0a7f7cf8b3273663bbe75e
[git.ikiwiki.info.git] / IkiWiki / Plugin / tag.pm
1 #!/usr/bin/perl
2 # Ikiwiki tag plugin.
3 package IkiWiki::Plugin::tag;
5 use warnings;
6 use strict;
7 use IkiWiki 3.00;
9 sub import {
10         hook(type => "checkconfig", id => "tag", call => \&checkconfig);
11         hook(type => "getopt", id => "tag", call => \&getopt);
12         hook(type => "getsetup", id => "tag", call => \&getsetup);
13         hook(type => "preprocess", id => "tag", call => \&preprocess_tag, scan => 1);
14         hook(type => "preprocess", id => "taglink", call => \&preprocess_taglink, scan => 1);
15         hook(type => "pagetemplate", id => "tag", call => \&pagetemplate);
17         IkiWiki::loadplugin("transient");
18 }
20 sub getopt () {
21         eval q{use Getopt::Long};
22         error($@) if $@;
23         Getopt::Long::Configure('pass_through');
24         GetOptions("tagbase=s" => \$config{tagbase});
25 }
27 sub getsetup () {
28         return
29                 plugin => {
30                         safe => 1,
31                         rebuild => undef,
32                 },
33                 tagbase => {
34                         type => "string",
35                         example => "tag",
36                         description => "parent page tags are located under",
37                         safe => 1,
38                         rebuild => 1,
39                 },
40                 tag_autocreate => {
41                         type => "boolean",
42                         example => 1,
43                         description => "autocreate new tag pages?",
44                         safe => 1,
45                         rebuild => undef,
46                 },
47                 tag_autocreate_commit => {
48                         type => "boolean",
49                         example => 1,
50                         default => 1,
51                         description => "commit autocreated tag pages",
52                         safe => 1,
53                         rebuild => 0,
54                 },
55 }
57 sub checkconfig () {
58         if (! defined $config{tag_autocreate_commit}) {
59                 $config{tag_autocreate_commit} = 1;
60         }
61         if (! $config{tag_autocreate_commit}) {
62                 $config{only_committed_changes}=0;
63         }
64 }
66 sub taglink ($) {
67         my $tag=shift;
68         
69         if ($tag !~ m{^/} &&
70             defined $config{tagbase}) {
71                 $tag="/".$config{tagbase}."/".$tag;
72                 $tag=~y#/#/#s; # squash dups
73         }
75         return $tag;
76 }
78 # Returns a tag name from a tag link
79 sub tagname ($) {
80         my $tag=shift;
81         if (defined $config{tagbase}) {
82                 $tag =~ s!^/\Q$config{tagbase}\E/!!;
83         } else {
84                 $tag =~ s!^\.?/!!;
85         }
86         return pagetitle($tag, 1);
87 }
89 sub htmllink_tag ($$$;@) {
90         my $page=shift;
91         my $destpage=shift;
92         my $tag=shift;
93         my %opts=@_;
95         return htmllink($page, $destpage, taglink($tag), %opts);
96 }
98 sub gentag ($) {
99         my $tag=shift;
101         if ($config{tag_autocreate} ||
102             ($config{tagbase} && ! defined $config{tag_autocreate})) {
103                 my $tagpage=taglink($tag);
104                 if ($tagpage=~/^\.\/(.*)/) {
105                         $tagpage=$1;
106                 }
107                 else {
108                         $tagpage=~s/^\///;
109                 }
110                 if (exists $IkiWiki::pagecase{lc $tagpage}) {
111                         $tagpage=$IkiWiki::pagecase{lc $tagpage}
112                 }
114                 my $tagfile = newpagefile($tagpage, $config{default_pageext});
116                 add_autofile($tagfile, "tag", sub {
117                         my $message=sprintf(gettext("creating tag page %s"), $tagpage);
118                         debug($message);
120                         my $template=template("autotag.tmpl");
121                         $template->param(tagname => tagname($tag));
122                         $template->param(tag => $tag);
124                         my $dir = $config{srcdir};
125                         if (! $config{tag_autocreate_commit}) {
126                                 $dir = $IkiWiki::Plugin::transient::transientdir;
127                         }
129                         writefile($tagfile, $dir, $template->output);
130                         if ($config{rcs} && $config{tag_autocreate_commit}) {
131                                 IkiWiki::disable_commit_hook();
132                                 IkiWiki::rcs_add($tagfile);
133                                 IkiWiki::rcs_commit_staged(message => $message);
134                                 IkiWiki::enable_commit_hook();
135                         }
136                 });
137         }
140 sub preprocess_tag (@) {
141         if (! @_) {
142                 return "";
143         }
144         my %params=@_;
145         my $page = $params{page};
146         delete $params{page};
147         delete $params{destpage};
148         delete $params{preview};
150         foreach my $tag (keys %params) {
151                 $tag=linkpage($tag);
152                 
153                 # hidden WikiLink
154                 add_link($page, taglink($tag), 'tag');
155                 
156                 gentag($tag);
157         }
158                 
159         return "";
162 sub preprocess_taglink (@) {
163         if (! @_) {
164                 return "";
165         }
166         my %params=@_;
167         return join(" ", map {
168                 if (/(.*)\|(.*)/) {
169                         my $tag=linkpage($2);
170                         add_link($params{page}, taglink($tag), 'tag');
171                         gentag($tag);
172                         return htmllink_tag($params{page}, $params{destpage}, $tag,
173                                 linktext => pagetitle($1));
174                 }
175                 else {
176                         my $tag=linkpage($_);
177                         add_link($params{page}, taglink($tag), 'tag');
178                         gentag($tag);
179                         return htmllink_tag($params{page}, $params{destpage}, $tag);
180                 }
181         }
182         grep {
183                 $_ ne 'page' && $_ ne 'destpage' && $_ ne 'preview'
184         } keys %params);
187 sub pagetemplate (@) {
188         my %params=@_;
189         my $page=$params{page};
190         my $destpage=$params{destpage};
191         my $template=$params{template};
193         my $tags = $typedlinks{$page}{tag};
195         $template->param(tags => [
196                 map { 
197                         link => htmllink_tag($page, $destpage, $_,
198                                         rel => "tag", linktext => tagname($_))
199                 }, sort keys %$tags
200         ]) if defined $tags && %$tags && $template->query(name => "tags");
202         if ($template->query(name => "categories")) {
203                 # It's an rss/atom template. Add any categories.
204                 if (defined $tags && %$tags) {
205                         eval q{use HTML::Entities};
206                         $template->param(categories =>
207                                 [map { category => HTML::Entities::encode_entities_numeric(tagname($_)) },
208                                         sort keys %$tags]);
209                 }
210         }
213 package IkiWiki::PageSpec;
215 sub match_tagged ($$;@) {
216         my $page=shift;
217         my $glob=IkiWiki::Plugin::tag::taglink(shift);
218         return match_link($page, $glob, linktype => 'tag', @_);