make taglink support the same method for specifying link text as a regular
[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 => "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);
16 } # }}}
17
18 sub getopt () { #{{{
19         eval q{use Getopt::Long};
20         error($@) if $@;
21         Getopt::Long::Configure('pass_through');
22         GetOptions("tagbase=s" => \$config{tagbase});
23 } #}}}
24
25 sub tagpage ($) { #{{{
26         my $tag=shift;
27                         
28         if (exists $config{tagbase} &&
29             defined $config{tagbase}) {
30                 $tag=$config{tagbase}."/".$tag;
31         }
32
33         return $tag;
34 } #}}}
35
36 sub preprocess_tag (@) { #{{{
37         if (! @_) {
38                 return "";
39         }
40         my %params=@_;
41         my $page = $params{page};
42         delete $params{page};
43         delete $params{destpage};
44         delete $params{preview};
45
46         foreach my $tag (keys %params) {
47                 $tag=IkiWiki::linkpage($tag);
48                 $tags{$page}{$tag}=1;
49                 # hidden WikiLink
50                 push @{$links{$page}}, tagpage($tag);
51         }
52                 
53         return "";
54 } # }}}
55
56 sub preprocess_taglink (@) { #{{{
57         if (! @_) {
58                 return "";
59         }
60         my %params=@_;
61         return join(" ", map {
62                 if (/(.*)\|(.*)/) {
63                         my $tag=IkiWiki::linkpage($2);
64                         $tags{$params{page}}{$tag}=1;
65                         return htmllink($params{page}, $params{destpage},
66                                 tagpage($tag),
67                                 linktext => IkiWiki::pagetitle($1));
68                 }
69                 else {
70                         my $tag=IkiWiki::linkpage($_);
71                         $tags{$params{page}}{$tag}=1;
72                         return htmllink($params{page}, $params{destpage},
73                                 tagpage($tag));
74                 }
75         }
76         grep {
77                 $_ ne 'page' && $_ ne 'destpage' && $_ ne 'preview'
78         } keys %params);
79 } # }}}
80
81 sub pagetemplate (@) { #{{{
82         my %params=@_;
83         my $page=$params{page};
84         my $destpage=$params{destpage};
85         my $template=$params{template};
86
87         $template->param(tags => [
88                 map { 
89                         link => htmllink($page, $destpage, tagpage($_),
90                                         rel => "tag")
91                 }, sort keys %{$tags{$page}}
92         ]) if exists $tags{$page} && %{$tags{$page}} && $template->query(name => "tags");
93
94         if ($template->query(name => "categories")) {
95                 # It's an rss/atom template. Add any categories.
96                 if (exists $tags{$page} && %{$tags{$page}}) {
97                         $template->param(categories => [map { category => $_ },
98                                 sort keys %{$tags{$page}}]);
99                 }
100         }
101 } # }}}
102
103 1