tag: Make edit link for new tags ensure that the tags are created inside tagbase...
[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         }
48
49         return $tag;
50 } #}}}
51
52 sub taglink ($$$;@) { #{{{
53         my $page=shift;
54         my $destpage=shift;
55         my $tag=shift;
56         my %opts=@_;
57
58         my $link=tagpage($tag);
59
60         # Force tag creation links to create the tag under /tagbase,
61         # if there is a tagbase and this tag used it.
62         $link="/".$link if $tag ne $link;
63
64         return htmllink($page, $destpage, $link, %opts);
65 } #}}}
66
67 sub preprocess_tag (@) { #{{{
68         if (! @_) {
69                 return "";
70         }
71         my %params=@_;
72         my $page = $params{page};
73         delete $params{page};
74         delete $params{destpage};
75         delete $params{preview};
76
77         foreach my $tag (keys %params) {
78                 $tag=IkiWiki::linkpage($tag);
79                 $tags{$page}{$tag}=1;
80                 # hidden WikiLink
81                 push @{$links{$page}}, tagpage($tag);
82         }
83                 
84         return "";
85 } # }}}
86
87 sub preprocess_taglink (@) { #{{{
88         if (! @_) {
89                 return "";
90         }
91         my %params=@_;
92         return join(" ", map {
93                 if (/(.*)\|(.*)/) {
94                         my $tag=IkiWiki::linkpage($2);
95                         $tags{$params{page}}{$tag}=1;
96                         push @{$links{$params{page}}}, tagpage($tag);
97                         return taglink($params{page}, $params{destpage}, $tag,
98                                 linktext => IkiWiki::pagetitle($1));
99                 }
100                 else {
101                         my $tag=IkiWiki::linkpage($_);
102                         $tags{$params{page}}{$tag}=1;
103                         push @{$links{$params{page}}}, tagpage($tag);
104                         return taglink($params{page}, $params{destpage}, $tag);
105                 }
106         }
107         grep {
108                 $_ ne 'page' && $_ ne 'destpage' && $_ ne 'preview'
109         } keys %params);
110 } # }}}
111
112 sub pagetemplate (@) { #{{{
113         my %params=@_;
114         my $page=$params{page};
115         my $destpage=$params{destpage};
116         my $template=$params{template};
117
118         $template->param(tags => [
119                 map { 
120                         link => taglink($page, $destpage, $_, rel => "tag")
121                 }, sort keys %{$tags{$page}}
122         ]) if exists $tags{$page} && %{$tags{$page}} && $template->query(name => "tags");
123
124         if ($template->query(name => "categories")) {
125                 # It's an rss/atom template. Add any categories.
126                 if (exists $tags{$page} && %{$tags{$page}}) {
127                         $template->param(categories => [map { category => $_ },
128                                 sort keys %{$tags{$page}}]);
129                 }
130         }
131 } # }}}
132
133 1