Merge branch 'master' of ssh://git.ikiwiki.info/srv/git/ikiwiki.info
[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 2.00;
8
9 my %tags;
10
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 } # }}}
18
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 } #}}}
25
26 sub getsetup () { #{{{
27         return
28                 plugin => {
29                         safe => 1,
30                         rebuild => undef,
31                 },
32                 tagbase => {
33                         type => "string",
34                         example => "tag",
35                         description => "parent page tags are located under",
36                         safe => 1,
37                         rebuild => 1,
38                 },
39 } #}}}
40
41 sub tagpage ($) { #{{{
42         my $tag=shift;
43                         
44         if ($tag !~ m{^\.?/} &&
45             defined $config{tagbase}) {
46                 $tag="/".$config{tagbase}."/".$tag;
47                 $tag=~y#/#/#s; # squash dups
48         }
49
50         return $tag;
51 } #}}}
52
53 sub taglink ($$$;@) { #{{{
54         my $page=shift;
55         my $destpage=shift;
56         my $tag=shift;
57         my %opts=@_;
58
59         my $link=tagpage($tag);
60
61         # Force tag creation links to create the tag under /tagbase,
62         # if there is a tagbase and this tag used it.
63         $link="/".$link if $tag ne $link;
64
65         return htmllink($page, $destpage, $link, %opts);
66 } #}}}
67
68 sub preprocess_tag (@) { #{{{
69         if (! @_) {
70                 return "";
71         }
72         my %params=@_;
73         my $page = $params{page};
74         delete $params{page};
75         delete $params{destpage};
76         delete $params{preview};
77
78         foreach my $tag (keys %params) {
79                 $tag=linkpage($tag);
80                 $tags{$page}{$tag}=1;
81                 # hidden WikiLink
82                 push @{$links{$page}}, tagpage($tag);
83         }
84                 
85         return "";
86 } # }}}
87
88 sub preprocess_taglink (@) { #{{{
89         if (! @_) {
90                 return "";
91         }
92         my %params=@_;
93         return join(" ", map {
94                 if (/(.*)\|(.*)/) {
95                         my $tag=linkpage($2);
96                         $tags{$params{page}}{$tag}=1;
97                         push @{$links{$params{page}}}, tagpage($tag);
98                         return taglink($params{page}, $params{destpage}, $tag,
99                                 linktext => pagetitle($1));
100                 }
101                 else {
102                         my $tag=linkpage($_);
103                         $tags{$params{page}}{$tag}=1;
104                         push @{$links{$params{page}}}, tagpage($tag);
105                         return taglink($params{page}, $params{destpage}, $tag);
106                 }
107         }
108         grep {
109                 $_ ne 'page' && $_ ne 'destpage' && $_ ne 'preview'
110         } keys %params);
111 } # }}}
112
113 sub pagetemplate (@) { #{{{
114         my %params=@_;
115         my $page=$params{page};
116         my $destpage=$params{destpage};
117         my $template=$params{template};
118
119         $template->param(tags => [
120                 map { 
121                         link => taglink($page, $destpage, $_, rel => "tag")
122                 }, sort keys %{$tags{$page}}
123         ]) if exists $tags{$page} && %{$tags{$page}} && $template->query(name => "tags");
124
125         if ($template->query(name => "categories")) {
126                 # It's an rss/atom template. Add any categories.
127                 if (exists $tags{$page} && %{$tags{$page}}) {
128                         $template->param(categories => [map { category => $_ },
129                                 sort keys %{$tags{$page}}]);
130                 }
131         }
132 } # }}}
133
134 1