]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/tag.pm
Added a comment
[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 }
63 sub taglink ($) {
64         my $tag=shift;
65         
66         if ($tag !~ m{^/} &&
67             defined $config{tagbase}) {
68                 $tag="/".$config{tagbase}."/".$tag;
69                 $tag=~y#/#/#s; # squash dups
70         }
72         return $tag;
73 }
75 # Returns a tag name from a tag link
76 sub tagname ($) {
77         my $tag=shift;
78         if (defined $config{tagbase}) {
79                 $tag =~ s!^/\Q$config{tagbase}\E/!!;
80         } else {
81                 $tag =~ s!^\.?/!!;
82         }
83         return pagetitle($tag, 1);
84 }
86 sub htmllink_tag ($$$;@) {
87         my $page=shift;
88         my $destpage=shift;
89         my $tag=shift;
90         my %opts=@_;
92         return htmllink($page, $destpage, taglink($tag), %opts);
93 }
95 sub gentag ($) {
96         my $tag=shift;
98         if ($config{tag_autocreate} ||
99             ($config{tagbase} && ! defined $config{tag_autocreate})) {
100                 my $tagpage=taglink($tag);
101                 if ($tagpage=~/^\.\/(.*)/) {
102                         $tagpage=$1;
103                 }
104                 else {
105                         $tagpage=~s/^\///;
106                 }
107                 if (exists $IkiWiki::pagecase{lc $tagpage}) {
108                         $tagpage=$IkiWiki::pagecase{lc $tagpage}
109                 }
111                 my $tagfile = newpagefile($tagpage, $config{default_pageext});
113                 add_autofile($tagfile, "tag", sub {
114                         my $message=sprintf(gettext("creating tag page %s"), $tagpage);
115                         debug($message);
117                         my $template=template("autotag.tmpl");
118                         $template->param(tagname => tagname($tag));
119                         $template->param(tag => $tag);
121                         my $dir = $config{srcdir};
122                         if (! $config{tag_autocreate_commit}) {
123                                 no warnings 'once';
124                                 $dir = $IkiWiki::Plugin::transient::transientdir;
125                         }
127                         writefile($tagfile, $dir, $template->output);
128                         if ($config{rcs} && $config{tag_autocreate_commit}) {
129                                 IkiWiki::disable_commit_hook();
130                                 IkiWiki::rcs_add($tagfile);
131                                 IkiWiki::rcs_commit_staged(message => $message);
132                                 IkiWiki::enable_commit_hook();
133                         }
134                 });
135         }
138 sub preprocess_tag (@) {
139         if (! @_) {
140                 return "";
141         }
142         my %params=@_;
143         my $page = $params{page};
144         delete $params{page};
145         delete $params{destpage};
146         delete $params{preview};
148         foreach my $tag (keys %params) {
149                 $tag=linkpage($tag);
150                 
151                 # hidden WikiLink
152                 add_link($page, taglink($tag), 'tag');
153                 
154                 gentag($tag);
155         }
156                 
157         return "";
160 sub preprocess_taglink (@) {
161         if (! @_) {
162                 return "";
163         }
164         my %params=@_;
165         return join(" ", map {
166                 if (/(.*)\|(.*)/) {
167                         my $tag=linkpage($2);
168                         add_link($params{page}, taglink($tag), 'tag');
169                         gentag($tag);
170                         return htmllink_tag($params{page}, $params{destpage}, $tag,
171                                 linktext => pagetitle($1));
172                 }
173                 else {
174                         my $tag=linkpage($_);
175                         add_link($params{page}, taglink($tag), 'tag');
176                         gentag($tag);
177                         return htmllink_tag($params{page}, $params{destpage}, $tag);
178                 }
179         }
180         grep {
181                 $_ ne 'page' && $_ ne 'destpage' && $_ ne 'preview'
182         } keys %params);
185 sub pagetemplate (@) {
186         my %params=@_;
187         my $page=$params{page};
188         my $destpage=$params{destpage};
189         my $template=$params{template};
191         my $tags = $typedlinks{$page}{tag};
193         $template->param(tags => [
194                 map { 
195                         link => htmllink_tag($page, $destpage, $_,
196                                         rel => "tag", linktext => tagname($_))
197                 }, sort keys %$tags
198         ]) if defined $tags && %$tags && $template->query(name => "tags");
200         if ($template->query(name => "categories")) {
201                 # It's an rss/atom template. Add any categories.
202                 if (defined $tags && %$tags) {
203                         eval q{use HTML::Entities};
204                         $template->param(categories =>
205                                 [map { category => HTML::Entities::encode_entities_numeric(tagname($_)) },
206                                         sort keys %$tags]);
207                 }
208         }
211 package IkiWiki::PageSpec;
213 sub match_tagged ($$;@) {
214         my $page=shift;
215         my $glob=IkiWiki::Plugin::tag::taglink(shift);
216         return match_link($page, $glob, linktype => 'tag', @_);