]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/tag.pm
remove broken ./tag support
[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 3.00;
8
9 sub import {
10         hook(type => "getopt", id => "tag", call => \&getopt);
11         hook(type => "getsetup", id => "tag", call => \&getsetup);
12         hook(type => "preprocess", id => "tag", call => \&preprocess_tag, scan => 1);
13         hook(type => "preprocess", id => "taglink", call => \&preprocess_taglink, scan => 1);
14         hook(type => "pagetemplate", id => "tag", call => \&pagetemplate);
15 }
16
17 sub getopt () {
18         eval q{use Getopt::Long};
19         error($@) if $@;
20         Getopt::Long::Configure('pass_through');
21         GetOptions("tagbase=s" => \$config{tagbase});
22 }
23
24 sub getsetup () {
25         return
26                 plugin => {
27                         safe => 1,
28                         rebuild => undef,
29                 },
30                 tagbase => {
31                         type => "string",
32                         example => "tag",
33                         description => "parent page tags are located under",
34                         safe => 1,
35                         rebuild => 1,
36                 },
37 }
38
39 sub tagpage ($) {
40         my $tag=shift;
41                         
42         if ($tag !~ m{^/} &&
43             defined $config{tagbase}) {
44                 $tag="/".$config{tagbase}."/".$tag;
45                 $tag=~y#/#/#s; # squash dups
46         }
47
48         return $tag;
49 }
50
51 sub taglink ($$$;@) {
52         my $page=shift;
53         my $destpage=shift;
54         my $tag=shift;
55         my %opts=@_;
56
57         return htmllink($page, $destpage, tagpage($tag), %opts);
58 }
59
60 sub preprocess_tag (@) {
61         if (! @_) {
62                 return "";
63         }
64         my %params=@_;
65         my $page = $params{page};
66         delete $params{page};
67         delete $params{destpage};
68         delete $params{preview};
69
70         foreach my $tag (keys %params) {
71                 $tag=linkpage($tag);
72                 # hidden WikiLink
73                 add_link($page, tagpage($tag), 'tag');
74         }
75                 
76         return "";
77 }
78
79 sub preprocess_taglink (@) {
80         if (! @_) {
81                 return "";
82         }
83         my %params=@_;
84         return join(" ", map {
85                 if (/(.*)\|(.*)/) {
86                         my $tag=linkpage($2);
87                         add_link($params{page}, tagpage($tag), 'tag');
88                         return taglink($params{page}, $params{destpage}, $tag,
89                                 linktext => pagetitle($1));
90                 }
91                 else {
92                         my $tag=linkpage($_);
93                         add_link($params{page}, tagpage($tag), 'tag');
94                         return taglink($params{page}, $params{destpage}, $tag);
95                 }
96         }
97         grep {
98                 $_ ne 'page' && $_ ne 'destpage' && $_ ne 'preview'
99         } keys %params);
100 }
101
102 sub pagetemplate (@) {
103         my %params=@_;
104         my $page=$params{page};
105         my $destpage=$params{destpage};
106         my $template=$params{template};
107
108         my $tags = $typedlinks{$page}{tag};
109
110         $template->param(tags => [
111                 map { 
112                         link => taglink($page, $destpage, $_, rel => "tag")
113                 }, sort keys %$tags
114         ]) if defined $tags && %$tags && $template->query(name => "tags");
115
116         if ($template->query(name => "categories")) {
117                 # It's an rss/atom template. Add any categories.
118                 if (defined $tags && %$tags) {
119                         $template->param(categories => [map { category => $_ },
120                                 sort keys %$tags]);
121                 }
122         }
123 }
124
125 package IkiWiki::PageSpec;
126
127 sub match_tagged ($$;@) {
128         my $page=shift;
129         my $glob=shift;
130         return match_link($page, IkiWiki::Plugin::tag::tagpage($glob), linktype => 'tag', @_);
131 }
132
133 1