3 package IkiWiki::Plugin::tag;
10 hook(type => "getopt", id => "tag", call => \&getopt);
11 hook(type => "getsetup", id => "tag", call => \&getsetup);
12 hook(type => "checkconfig", id => "tag", call => \&checkconfig);
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);
19 eval q{use Getopt::Long};
21 Getopt::Long::Configure('pass_through');
22 GetOptions("tagbase=s" => \$config{tagbase});
34 description => "parent page tags are located under",
38 # TODO make this a hash so that we can have the user specify the linktype, tagbase and template param for each custom tag type
41 example => ["category", "column"],
42 description => "extra categorization types: they define a new directive each, with a corresponding template parameter, link type and tag base",
49 if (defined $config{tagtypes}) {
50 return @{$config{tagtypes}};
57 foreach my $tagtype (tagtypes()) {
58 debug("defining '$tagtype' tagtype (directive, linktype, tagbase, template param)");
59 hook(type => "preprocess", id => $tagtype, call => sub { preprocess_custom_tag($tagtype, @_) });
67 if (defined $type && $type ne "tag") {
69 } elsif ($tag !~ m{^\.?/} &&
70 defined $config{tagbase}) {
71 $tag="/".$config{tagbase}."/".$tag;
72 $tag=~y#/#/#s; # squash dups
78 sub taglink ($$$$;@) {
85 return htmllink($page, $destpage, tagpage($tag, $tagtype), %opts);
88 sub preprocess_tag (@) {
93 my $page = $params{page};
94 my $tagtype = defined $params{tagtype} ? $params{tagtype} : 'tag' ;
96 delete $params{tagtype};
97 delete $params{destpage};
98 delete $params{preview};
100 foreach my $tag (keys %params) {
103 add_link($page, tagpage($tag, $tagtype), $tagtype);
109 sub preprocess_custom_tag($;@) {
115 $params{tagtype} ||= $tagtype;
116 preprocess_tag(%params);
119 sub preprocess_taglink (@) {
124 my $tagtype = 'tag'; # TODO customizable?
125 return join(" ", map {
127 my $tag=linkpage($2);
128 add_link($params{page}, tagpage($tag, $tagtype), $tagtype);
129 return taglink($params{page}, $params{destpage}, $tag, $tagtype,
130 linktext => pagetitle($1));
133 my $tag=linkpage($_);
134 add_link($params{page}, tagpage($tag, $tagtype), $tagtype);
135 return taglink($params{page}, $params{destpage}, $tag, $tagtype);
139 $_ ne 'page' && $_ ne 'destpage' && $_ ne 'preview'
143 sub pagetemplate (@) {
145 my $page=$params{page};
146 my $destpage=$params{destpage};
147 my $template=$params{template};
149 my $tags = $typedlinks{$page}{tag};
152 if (defined $tags && %$tags) {
153 push @taglist, keys %$tags;
154 $template->param(tags => [
156 link => taglink($page, $destpage, $_, 'tag', rel => "tag")
158 ]) if $template->query(name => "tags");
161 # custom tagtypes have a separate treatment for a number of reasons, including the fact that
162 # their template parameter name is different from the tag/link type ('tags' vs 'tag')
163 # notice the use of rel => $tagtype
164 foreach my $tagtype (tagtypes()) {
165 $tags = $typedlinks{$page}{$tagtype};
166 if (defined $tags && %$tags) {
167 push @taglist, keys %$tags;
168 $template->param($tagtype => [
170 link => taglink($page, $destpage, $_, $tagtype, rel => $tagtype)
172 ]) if $template->query(name => $tagtype);
176 debug("full tag list: " . join(',', @taglist)) if @taglist;
178 if ($template->query(name => "categories")) {
179 # It's an rss/atom template. Add any categories.
181 $template->param(categories => [map { category => $_ },
187 package IkiWiki::PageSpec;
189 sub match_tagged ($$;@) {
191 my $tagtype = defined $params{tagtype} ? $params{tagtype} : 'tag' ;
192 return match_link($_[0], IkiWiki::Plugin::tag::tagpage($_[1], $tagtype), linktype => $tagtype);