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);
16 IkiWiki::loadplugin("transient");
20 eval q{use Getopt::Long};
22 Getopt::Long::Configure('pass_through');
23 GetOptions("tagbase=s" => \$config{tagbase});
35 description => "parent page tags are located under",
42 description => "autocreate new tag pages?",
46 tag_autocreate_commit => {
50 description => "commit autocreated tag pages",
60 defined $config{tagbase}) {
61 $tag="/".$config{tagbase}."/".$tag;
62 $tag=~y#/#/#s; # squash dups
68 # Returns a tag name from a tag link
71 if (defined $config{tagbase}) {
72 $tag =~ s!^/\Q$config{tagbase}\E/!!;
76 return pagetitle($tag, 1);
79 sub htmllink_tag ($$$;@) {
85 return htmllink($page, $destpage, taglink($tag), %opts);
91 if ($config{tag_autocreate} ||
92 ($config{tagbase} && ! defined $config{tag_autocreate})) {
93 my $tagpage=taglink($tag);
94 if ($tagpage=~/^\.\/(.*)/) {
101 my $tagfile = newpagefile($tagpage, $config{default_pageext});
103 add_autofile($tagfile, "tag", sub {
104 my $message=sprintf(gettext("creating tag page %s"), $tagpage);
107 my $template=template("autotag.tmpl");
108 $template->param(tagname => tagname($tag));
109 $template->param(tag => $tag);
111 my $dir = $config{srcdir};
112 if (! $config{tag_autocreate_commit}) {
113 $dir = $IkiWiki::Plugin::transient::transientdir;
116 writefile($tagfile, $dir, $template->output);
117 if ($config{rcs} && $config{tag_autocreate_commit}) {
118 IkiWiki::disable_commit_hook();
119 IkiWiki::rcs_add($tagfile);
120 IkiWiki::rcs_commit_staged(message => $message);
121 IkiWiki::enable_commit_hook();
127 sub preprocess_tag (@) {
132 my $page = $params{page};
133 delete $params{page};
134 delete $params{destpage};
135 delete $params{preview};
137 foreach my $tag (keys %params) {
141 add_link($page, taglink($tag), 'tag');
149 sub preprocess_taglink (@) {
154 return join(" ", map {
156 my $tag=linkpage($2);
157 add_link($params{page}, taglink($tag), 'tag');
159 return htmllink_tag($params{page}, $params{destpage}, $tag,
160 linktext => pagetitle($1));
163 my $tag=linkpage($_);
164 add_link($params{page}, taglink($tag), 'tag');
166 return htmllink_tag($params{page}, $params{destpage}, $tag);
170 $_ ne 'page' && $_ ne 'destpage' && $_ ne 'preview'
174 sub pagetemplate (@) {
176 my $page=$params{page};
177 my $destpage=$params{destpage};
178 my $template=$params{template};
180 my $tags = $typedlinks{$page}{tag};
182 $template->param(tags => [
184 link => htmllink_tag($page, $destpage, $_,
185 rel => "tag", linktext => tagname($_))
187 ]) if defined $tags && %$tags && $template->query(name => "tags");
189 if ($template->query(name => "categories")) {
190 # It's an rss/atom template. Add any categories.
191 if (defined $tags && %$tags) {
192 $template->param(categories => [map { category => tagname($_) },
198 package IkiWiki::PageSpec;
200 sub match_tagged ($$;@) {
202 my $glob=IkiWiki::Plugin::tag::taglink(shift);
203 return match_link($page, $glob, linktype => 'tag', @_);