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 # Returns a tag name from a tag link
61 if (defined $config{tagbase}) {
62 $tag =~ s!^/\Q$config{tagbase}\E/!!;
69 sub htmllink_tag ($$$;@) {
75 return htmllink($page, $destpage, taglink($tag), %opts);
81 if ($config{tag_autocreate} ||
82 ($config{tagbase} && ! defined $config{tag_autocreate})) {
83 my $tagpage=taglink($tag);
84 if ($tagpage=~/^\.\/(.*)/) {
91 my $tagfile = newpagefile($tagpage, $config{default_pageext});
93 add_autofile($tagfile, "tag", sub {
94 my $message=sprintf(gettext("creating tag page %s"), $tagpage);
97 my $template=template("autotag.tmpl");
98 $template->param(tagname => tagname($tag));
99 $template->param(tag => $tag);
100 writefile($tagfile, $config{srcdir}, $template->output);
102 IkiWiki::disable_commit_hook();
103 IkiWiki::rcs_add($tagfile);
104 IkiWiki::rcs_commit_staged(message => $message);
105 IkiWiki::enable_commit_hook();
111 sub preprocess_tag (@) {
116 my $page = $params{page};
117 delete $params{page};
118 delete $params{destpage};
119 delete $params{preview};
121 foreach my $tag (keys %params) {
125 add_link($page, taglink($tag), 'tag');
133 sub preprocess_taglink (@) {
138 return join(" ", map {
140 my $tag=linkpage($2);
141 add_link($params{page}, taglink($tag), 'tag');
143 return htmllink_tag($params{page}, $params{destpage}, $tag,
144 linktext => pagetitle($1));
147 my $tag=linkpage($_);
148 add_link($params{page}, taglink($tag), 'tag');
150 return htmllink_tag($params{page}, $params{destpage}, $tag);
154 $_ ne 'page' && $_ ne 'destpage' && $_ ne 'preview'
158 sub pagetemplate (@) {
160 my $page=$params{page};
161 my $destpage=$params{destpage};
162 my $template=$params{template};
164 my $tags = $typedlinks{$page}{tag};
166 $template->param(tags => [
168 link => htmllink_tag($page, $destpage, $_,
169 rel => "tag", linktext => tagname($_))
171 ]) if defined $tags && %$tags && $template->query(name => "tags");
173 if ($template->query(name => "categories")) {
174 # It's an rss/atom template. Add any categories.
175 if (defined $tags && %$tags) {
176 $template->param(categories => [map { category => tagname($_) },
182 package IkiWiki::PageSpec;
184 sub match_tagged ($$;@) {
186 my $glob=IkiWiki::Plugin::tag::taglink(shift);
187 return match_link($page, $glob, linktype => 'tag', @_);