]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/tag.pm
Merge branch 'master' into autoconfig
[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 2.00;
9 my %tags;
11 sub import { #{{{
12         hook(type => "getopt", id => "tag", call => \&getopt);
13         hook(type => "getsetup", id => "tag", call => \&getsetup);
14         hook(type => "preprocess", id => "tag", call => \&preprocess_tag, scan => 1);
15         hook(type => "preprocess", id => "taglink", call => \&preprocess_taglink, scan => 1);
16         hook(type => "pagetemplate", id => "tag", call => \&pagetemplate);
17 } # }}}
19 sub getopt () { #{{{
20         eval q{use Getopt::Long};
21         error($@) if $@;
22         Getopt::Long::Configure('pass_through');
23         GetOptions("tagbase=s" => \$config{tagbase});
24 } #}}}
26 sub getsetup () { #{{{
27         return
28                 tagbase => {
29                         type => "string",
30                         default => "",
31                         example => "tag",
32                         description => "parent page tags are located under",
33                         safe => 1,
34                         rebuild => 1,
35                 },
36 } #}}}
38 sub tagpage ($) { #{{{
39         my $tag=shift;
40                         
41         if ($tag !~ m{^\.?/} &&
42             exists $config{tagbase} &&
43             defined $config{tagbase}) {
44                 $tag=$config{tagbase}."/".$tag;
45         }
47         return $tag;
48 } #}}}
50 sub preprocess_tag (@) { #{{{
51         if (! @_) {
52                 return "";
53         }
54         my %params=@_;
55         my $page = $params{page};
56         delete $params{page};
57         delete $params{destpage};
58         delete $params{preview};
60         foreach my $tag (keys %params) {
61                 $tag=IkiWiki::linkpage($tag);
62                 $tags{$page}{$tag}=1;
63                 # hidden WikiLink
64                 push @{$links{$page}}, tagpage($tag);
65         }
66                 
67         return "";
68 } # }}}
70 sub preprocess_taglink (@) { #{{{
71         if (! @_) {
72                 return "";
73         }
74         my %params=@_;
75         return join(" ", map {
76                 if (/(.*)\|(.*)/) {
77                         my $tag=IkiWiki::linkpage($2);
78                         $tags{$params{page}}{$tag}=1;
79                         push @{$links{$params{page}}}, tagpage($tag);
80                         return htmllink($params{page}, $params{destpage},
81                                 tagpage($tag),
82                                 linktext => IkiWiki::pagetitle($1));
83                 }
84                 else {
85                         my $tag=IkiWiki::linkpage($_);
86                         $tags{$params{page}}{$tag}=1;
87                         push @{$links{$params{page}}}, tagpage($tag);
88                         return htmllink($params{page}, $params{destpage},
89                                 tagpage($tag));
90                 }
91         }
92         grep {
93                 $_ ne 'page' && $_ ne 'destpage' && $_ ne 'preview'
94         } keys %params);
95 } # }}}
97 sub pagetemplate (@) { #{{{
98         my %params=@_;
99         my $page=$params{page};
100         my $destpage=$params{destpage};
101         my $template=$params{template};
103         $template->param(tags => [
104                 map { 
105                         link => htmllink($page, $destpage, tagpage($_),
106                                         rel => "tag")
107                 }, sort keys %{$tags{$page}}
108         ]) if exists $tags{$page} && %{$tags{$page}} && $template->query(name => "tags");
110         if ($template->query(name => "categories")) {
111                 # It's an rss/atom template. Add any categories.
112                 if (exists $tags{$page} && %{$tags{$page}}) {
113                         $template->param(categories => [map { category => $_ },
114                                 sort keys %{$tags{$page}}]);
115                 }
116         }
117 } # }}}