implement typed links; add tagged_is_strict config option
[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                 tagged_is_strict => {
38                         type => "boolean",
39                         default => 0,
40                         description => "if 1, tagged() doesn't match normal WikiLinks to tag pages",
41                         safe => 1,
42                         rebuild => 1,
43                 },
44 }
45
46 sub tagpage ($) {
47         my $tag=shift;
48                         
49         if ($tag !~ m{^\.?/} &&
50             defined $config{tagbase}) {
51                 $tag="/".$config{tagbase}."/".$tag;
52                 $tag=~y#/#/#s; # squash dups
53         }
54
55         return $tag;
56 }
57
58 sub taglink ($$$;@) {
59         my $page=shift;
60         my $destpage=shift;
61         my $tag=shift;
62         my %opts=@_;
63
64         return htmllink($page, $destpage, tagpage($tag), %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=linkpage($tag);
79                 # hidden WikiLink
80                 add_link($page, tagpage($tag), 'tag');
81         }
82                 
83         return "";
84 }
85
86 sub preprocess_taglink (@) {
87         if (! @_) {
88                 return "";
89         }
90         my %params=@_;
91         return join(" ", map {
92                 if (/(.*)\|(.*)/) {
93                         my $tag=linkpage($2);
94                         add_link($params{page}, tagpage($tag), 'tag');
95                         return taglink($params{page}, $params{destpage}, $tag,
96                                 linktext => pagetitle($1));
97                 }
98                 else {
99                         my $tag=linkpage($_);
100                         add_link($params{page}, tagpage($tag), 'tag');
101                         return taglink($params{page}, $params{destpage}, $tag);
102                 }
103         }
104         grep {
105                 $_ ne 'page' && $_ ne 'destpage' && $_ ne 'preview'
106         } keys %params);
107 }
108
109 sub pagetemplate (@) {
110         my %params=@_;
111         my $page=$params{page};
112         my $destpage=$params{destpage};
113         my $template=$params{template};
114
115         my $tags = $typedlinks{$page}{tag};
116
117         $template->param(tags => [
118                 map { 
119                         link => taglink($page, $destpage, $_, rel => "tag")
120                 }, sort keys %$tags
121         ]) if defined $tags && %$tags && $template->query(name => "tags");
122
123         if ($template->query(name => "categories")) {
124                 # It's an rss/atom template. Add any categories.
125                 if (defined $tags && %$tags) {
126                         $template->param(categories => [map { category => $_ },
127                                 sort keys %$tags]);
128                 }
129         }
130 }
131
132 package IkiWiki::PageSpec;
133
134 sub match_tagged ($$;@) {
135         my $page = shift;
136         my $glob = shift;
137
138         if ($IkiWiki::config{tagged_is_strict}) {
139                 return match_link($page, IkiWiki::Plugin::tag::tagpage($glob), linktype => 'tag');
140         }
141         else {
142                 return match_link($page, IkiWiki::Plugin::tag::tagpage($glob));
143         }
144 }
145
146 1