+}
+
+sub preprocess_taglink (@) {
+ if (! @_) {
+ return "";
+ }
+ my %params=@_;
+ return join(" ", map {
+ if (/(.*)\|(.*)/) {
+ my $tag=linkpage($2);
+ add_link($params{page}, taglink($tag), 'tag');
+ gentag($tag);
+ return htmllink_tag($params{page}, $params{destpage}, $tag,
+ linktext => pagetitle($1));
+ }
+ else {
+ my $tag=linkpage($_);
+ add_link($params{page}, taglink($tag), 'tag');
+ gentag($tag);
+ return htmllink_tag($params{page}, $params{destpage}, $tag);
+ }
+ }
+ grep {
+ $_ ne 'page' && $_ ne 'destpage' && $_ ne 'preview'
+ } keys %params);
+}
+
+sub pagetemplate (@) {
+ my %params=@_;
+ my $page=$params{page};
+ my $destpage=$params{destpage};
+ my $template=$params{template};
+
+ my $tags = $typedlinks{$page}{tag};
+
+ $template->param(tags => [
+ map {
+ link => htmllink_tag($page, $destpage, $_, rel => "tag")
+ }, sort keys %$tags
+ ]) if defined $tags && %$tags && $template->query(name => "tags");
+
+ if ($template->query(name => "categories")) {
+ # It's an rss/atom template. Add any categories.
+ if (defined $tags && %$tags) {
+ $template->param(categories => [map { category => $_ },
+ sort keys %$tags]);
+ }
+ }
+}
+
+package IkiWiki::PageSpec;
+
+sub match_tagged ($$;@) {
+ my $page=shift;
+ my $glob=IkiWiki::Plugin::tag::taglink(shift);
+ return match_link($page, $glob, linktype => 'tag', @_);
+}