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 => $tag);
87 writefile($tagfile, $config{srcdir}, $template->output);
89 IkiWiki::disable_commit_hook();
90 IkiWiki::rcs_add($tagfile);
91 IkiWiki::rcs_commit_staged($message, undef, undef);
92 IkiWiki::enable_commit_hook();
98 sub preprocess_tag (@) {
103 my $page = $params{page};
104 delete $params{page};
105 delete $params{destpage};
106 delete $params{preview};
108 foreach my $tag (keys %params) {
112 add_link($page, taglink($tag), 'tag');
120 sub preprocess_taglink (@) {
125 return join(" ", map {
127 my $tag=linkpage($2);
128 add_link($params{page}, taglink($tag), 'tag');
130 return htmllink_tag($params{page}, $params{destpage}, $tag,
131 linktext => pagetitle($1));
134 my $tag=linkpage($_);
135 add_link($params{page}, taglink($tag), 'tag');
137 return htmllink_tag($params{page}, $params{destpage}, $tag);
141 $_ ne 'page' && $_ ne 'destpage' && $_ ne 'preview'
145 sub pagetemplate (@) {
147 my $page=$params{page};
148 my $destpage=$params{destpage};
149 my $template=$params{template};
151 my $tags = $typedlinks{$page}{tag};
153 $template->param(tags => [
155 link => htmllink_tag($page, $destpage, $_, rel => "tag")
157 ]) if defined $tags && %$tags && $template->query(name => "tags");
159 if ($template->query(name => "categories")) {
160 # It's an rss/atom template. Add any categories.
161 if (defined $tags && %$tags) {
162 $template->param(categories => [map { category => $_ },
168 package IkiWiki::PageSpec;
170 sub match_tagged ($$;@) {
171 return match_link($_[0], IkiWiki::Plugin::tag::taglink($_[1]), linktype => 'tag');