]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/tag.pm
foldage
[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 ($tag !~ m{^\.?/} &&
29             exists $config{tagbase} &&
30             defined $config{tagbase}) {
31                 $tag=$config{tagbase}."/".$tag;
32         }
33
34         return $tag;
35 } #}}}
36
37 sub preprocess_tag (@) { #{{{
38         if (! @_) {
39                 return "";
40         }
41         my %params=@_;
42         my $page = $params{page};
43         delete $params{page};
44         delete $params{destpage};
45         delete $params{preview};
46
47         foreach my $tag (keys %params) {
48                 $tag=IkiWiki::linkpage($tag);
49                 $tags{$page}{$tag}=1;
50                 # hidden WikiLink
51                 push @{$links{$page}}, tagpage($tag);
52         }
53                 
54         return "";
55 } # }}}
56
57 sub preprocess_taglink (@) { #{{{
58         if (! @_) {
59                 return "";
60         }
61         my %params=@_;
62         return join(" ", map {
63                 if (/(.*)\|(.*)/) {
64                         my $tag=IkiWiki::linkpage($2);
65                         $tags{$params{page}}{$tag}=1;
66                         push @{$links{$params{page}}}, tagpage($tag);
67                         return htmllink($params{page}, $params{destpage},
68                                 tagpage($tag),
69                                 linktext => IkiWiki::pagetitle($1));
70                 }
71                 else {
72                         my $tag=IkiWiki::linkpage($_);
73                         $tags{$params{page}}{$tag}=1;
74                         push @{$links{$params{page}}}, tagpage($tag);
75                         return htmllink($params{page}, $params{destpage},
76                                 tagpage($tag));
77                 }
78         }
79         grep {
80                 $_ ne 'page' && $_ ne 'destpage' && $_ ne 'preview'
81         } keys %params);
82 } # }}}
83
84 sub pagetemplate (@) { #{{{
85         my %params=@_;
86         my $page=$params{page};
87         my $destpage=$params{destpage};
88         my $template=$params{template};
89
90         $template->param(tags => [
91                 map { 
92                         link => htmllink($page, $destpage, tagpage($_),
93                                         rel => "tag")
94                 }, sort keys %{$tags{$page}}
95         ]) if exists $tags{$page} && %{$tags{$page}} && $template->query(name => "tags");
96
97         if ($template->query(name => "categories")) {
98                 # It's an rss/atom template. Add any categories.
99                 if (exists $tags{$page} && %{$tags{$page}}) {
100                         $template->param(categories => [map { category => $_ },
101                                 sort keys %{$tags{$page}}]);
102                 }
103         }
104 } # }}}
105
106 1