3 package IkiWiki::Plugin::tag;
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);
18 eval q{use Getopt::Long};
20 Getopt::Long::Configure('pass_through');
21 GetOptions("tagbase=s" => \$config{tagbase});
33 description => "parent page tags are located under",
40 description => "autocreate new tag pages?",
49 if ($tag !~ m{^\.?/} &&
50 defined $config{tagbase}) {
51 $tag="/".$config{tagbase}."/".$tag;
52 $tag=~y#/#/#s; # squash dups
58 sub htmllink_tag ($$$;@) {
64 return htmllink($page, $destpage, taglink($tag), %opts);
70 if ($config{tag_autocreate}) {
71 my $tagpage=taglink($tag);
72 if ($tagpage=~/^\.\/(.*)/) {
79 my $tagfile = newpagefile($tagpage, $config{default_pageext});
81 add_autofile($tagfile, "tag", sub {
82 my $message=sprintf(gettext("creating tag page %s"), $tag);
85 my $template=template("autotag.tmpl");
86 $template->param(tag => IkiWiki::basename($tag));
87 $template->param(tagpage => $tagpage);
88 writefile($tagfile, $config{srcdir}, $template->output);
90 IkiWiki::disable_commit_hook();
91 IkiWiki::rcs_add($tagfile);
92 IkiWiki::rcs_commit_staged($message, undef, undef);
93 IkiWiki::enable_commit_hook();
99 sub preprocess_tag (@) {
104 my $page = $params{page};
105 delete $params{page};
106 delete $params{destpage};
107 delete $params{preview};
109 foreach my $tag (keys %params) {
113 add_link($page, taglink($tag), 'tag');
121 sub preprocess_taglink (@) {
126 return join(" ", map {
128 my $tag=linkpage($2);
129 add_link($params{page}, taglink($tag), 'tag');
131 return htmllink_tag($params{page}, $params{destpage}, $tag,
132 linktext => pagetitle($1));
135 my $tag=linkpage($_);
136 add_link($params{page}, taglink($tag), 'tag');
138 return htmllink_tag($params{page}, $params{destpage}, $tag);
142 $_ ne 'page' && $_ ne 'destpage' && $_ ne 'preview'
146 sub pagetemplate (@) {
148 my $page=$params{page};
149 my $destpage=$params{destpage};
150 my $template=$params{template};
152 my $tags = $typedlinks{$page}{tag};
154 $template->param(tags => [
156 link => htmllink_tag($page, $destpage, $_, rel => "tag")
158 ]) if defined $tags && %$tags && $template->query(name => "tags");
160 if ($template->query(name => "categories")) {
161 # It's an rss/atom template. Add any categories.
162 if (defined $tags && %$tags) {
163 $template->param(categories => [map { category => $_ },
169 package IkiWiki::PageSpec;
171 sub match_tagged ($$;@) {
173 my $glob=IkiWiki::Plugin::tag::taglink(shift);
174 return match_link($page, $glob, linktype => 'tag', @_);