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?",
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 ($config{tagbase} && ! defined $config{tag_autocreate})) {
72 my $tagpage=taglink($tag);
73 if ($tagpage=~/^\.\/(.*)/) {
80 my $tagfile = newpagefile($tagpage, $config{default_pageext});
82 add_autofile($tagfile, "tag", sub {
83 my $message=sprintf(gettext("creating tag page %s"), $tagpage);
86 my $template=template("autotag.tmpl");
87 $template->param(tagname => IkiWiki::basename($tag));
88 $template->param(tag => $tag);
89 writefile($tagfile, $config{srcdir}, $template->output);
91 IkiWiki::disable_commit_hook();
92 IkiWiki::rcs_add($tagfile);
93 IkiWiki::rcs_commit_staged($message, undef, undef);
94 IkiWiki::enable_commit_hook();
100 sub preprocess_tag (@) {
105 my $page = $params{page};
106 delete $params{page};
107 delete $params{destpage};
108 delete $params{preview};
110 foreach my $tag (keys %params) {
114 add_link($page, taglink($tag), 'tag');
122 sub preprocess_taglink (@) {
127 return join(" ", map {
129 my $tag=linkpage($2);
130 add_link($params{page}, taglink($tag), 'tag');
132 return htmllink_tag($params{page}, $params{destpage}, $tag,
133 linktext => pagetitle($1));
136 my $tag=linkpage($_);
137 add_link($params{page}, taglink($tag), 'tag');
139 return htmllink_tag($params{page}, $params{destpage}, $tag);
143 $_ ne 'page' && $_ ne 'destpage' && $_ ne 'preview'
147 sub pagetemplate (@) {
149 my $page=$params{page};
150 my $destpage=$params{destpage};
151 my $template=$params{template};
153 my $tags = $typedlinks{$page}{tag};
155 $template->param(tags => [
157 link => htmllink_tag($page, $destpage, $_, rel => "tag")
159 ]) if defined $tags && %$tags && $template->query(name => "tags");
161 if ($template->query(name => "categories")) {
162 # It's an rss/atom template. Add any categories.
163 if (defined $tags && %$tags) {
164 $template->param(categories => [map { category => $_ },
170 package IkiWiki::PageSpec;
172 sub match_tagged ($$;@) {
174 my $glob=IkiWiki::Plugin::tag::taglink(shift);
175 return match_link($page, $glob, linktype => 'tag', @_);