]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/tag.pm
smiley: Generate links relative to the destpage. (Fixes a reversion from 2.41.)
[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                         push @{$links{$params{page}}}, tagpage($tag);
66                         return htmllink($params{page}, $params{destpage},
67                                 tagpage($tag),
68                                 linktext => IkiWiki::pagetitle($1));
69                 }
70                 else {
71                         my $tag=IkiWiki::linkpage($_);
72                         $tags{$params{page}}{$tag}=1;
73                         push @{$links{$params{page}}}, tagpage($tag);
74                         return htmllink($params{page}, $params{destpage},
75                                 tagpage($tag));
76                 }
77         }
78         grep {
79                 $_ ne 'page' && $_ ne 'destpage' && $_ ne 'preview'
80         } keys %params);
81 } # }}}
82
83 sub pagetemplate (@) { #{{{
84         my %params=@_;
85         my $page=$params{page};
86         my $destpage=$params{destpage};
87         my $template=$params{template};
88
89         $template->param(tags => [
90                 map { 
91                         link => htmllink($page, $destpage, tagpage($_),
92                                         rel => "tag")
93                 }, sort keys %{$tags{$page}}
94         ]) if exists $tags{$page} && %{$tags{$page}} && $template->query(name => "tags");
95
96         if ($template->query(name => "categories")) {
97                 # It's an rss/atom template. Add any categories.
98                 if (exists $tags{$page} && %{$tags{$page}}) {
99                         $template->param(categories => [map { category => $_ },
100                                 sort keys %{$tags{$page}}]);
101                 }
102         }
103 } # }}}
104
105 1