3 package IkiWiki::Plugin::tag;
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);
20 eval q{use Getopt::Long};
22 Getopt::Long::Configure('pass_through');
23 GetOptions("tagbase=s" => \$config{tagbase});
26 sub getsetup () { #{{{
35 description => "parent page tags are located under",
41 sub tagpage ($) { #{{{
44 if ($tag !~ m{^\.?/} &&
45 defined $config{tagbase}) {
46 $tag="/".$config{tagbase}."/".$tag;
47 $tag=~y#/#/#s; # squash dups
53 sub taglink ($$$;@) { #{{{
59 my $link=tagpage($tag);
61 # Force tag creation links to create the tag under /tagbase,
62 # if there is a tagbase and this tag used it.
63 $link="/".$link if $tag ne $link;
65 return htmllink($page, $destpage, $link, %opts);
68 sub preprocess_tag (@) { #{{{
73 my $page = $params{page};
75 delete $params{destpage};
76 delete $params{preview};
78 foreach my $tag (keys %params) {
82 push @{$links{$page}}, tagpage($tag);
88 sub preprocess_taglink (@) { #{{{
93 return join(" ", map {
96 $tags{$params{page}}{$tag}=1;
97 push @{$links{$params{page}}}, tagpage($tag);
98 return taglink($params{page}, $params{destpage}, $tag,
99 linktext => pagetitle($1));
102 my $tag=linkpage($_);
103 $tags{$params{page}}{$tag}=1;
104 push @{$links{$params{page}}}, tagpage($tag);
105 return taglink($params{page}, $params{destpage}, $tag);
109 $_ ne 'page' && $_ ne 'destpage' && $_ ne 'preview'
113 sub pagetemplate (@) { #{{{
115 my $page=$params{page};
116 my $destpage=$params{destpage};
117 my $template=$params{template};
119 $template->param(tags => [
121 link => taglink($page, $destpage, $_, rel => "tag")
122 }, sort keys %{$tags{$page}}
123 ]) if exists $tags{$page} && %{$tags{$page}} && $template->query(name => "tags");
125 if ($template->query(name => "categories")) {
126 # It's an rss/atom template. Add any categories.
127 if (exists $tags{$page} && %{$tags{$page}}) {
128 $template->param(categories => [map { category => $_ },
129 sort keys %{$tags{$page}}]);