3 package IkiWiki::Plugin::tag;
10 hook(type => "checkconfig", id => "tag", call => \&checkconfig);
11 hook(type => "getopt", id => "tag", call => \&getopt);
12 hook(type => "getsetup", id => "tag", call => \&getsetup);
13 hook(type => "preprocess", id => "tag", call => \&preprocess_tag, scan => 1);
14 hook(type => "preprocess", id => "taglink", call => \&preprocess_taglink, scan => 1);
15 hook(type => "pagetemplate", id => "tag", call => \&pagetemplate);
17 IkiWiki::loadplugin("transient");
21 eval q{use Getopt::Long};
23 Getopt::Long::Configure('pass_through');
24 GetOptions("tagbase=s" => \$config{tagbase});
36 description => "parent page tags are located under",
43 description => "autocreate new tag pages?",
47 tag_autocreate_commit => {
51 description => "commit autocreated tag pages",
58 if (! defined $config{tag_autocreate_commit}) {
59 $config{tag_autocreate_commit} = 1;
67 defined $config{tagbase}) {
68 $tag="/".$config{tagbase}."/".$tag;
69 $tag=~y#/#/#s; # squash dups
75 # Returns a tag name from a tag link
78 if (defined $config{tagbase}) {
79 $tag =~ s!^/\Q$config{tagbase}\E/!!;
83 return pagetitle($tag, 1);
86 sub htmllink_tag ($$$;@) {
92 return htmllink($page, $destpage, taglink($tag), %opts);
98 if ($config{tag_autocreate} ||
99 ($config{tagbase} && ! defined $config{tag_autocreate})) {
100 my $tagpage=taglink($tag);
101 if ($tagpage=~/^\.\/(.*)/) {
107 if (exists $IkiWiki::pagecase{lc $tagpage}) {
108 $tagpage=$IkiWiki::pagecase{lc $tagpage}
111 my $tagfile = newpagefile($tagpage, $config{default_pageext});
113 add_autofile($tagfile, "tag", sub {
114 my $message=sprintf(gettext("creating tag page %s"), $tagpage);
117 my $template=template("autotag.tmpl");
118 $template->param(tagname => tagname($tag));
119 $template->param(tag => $tag);
121 my $dir = $config{srcdir};
122 if (! $config{tag_autocreate_commit}) {
123 $dir = $IkiWiki::Plugin::transient::transientdir;
126 writefile($tagfile, $dir, $template->output);
127 if ($config{rcs} && $config{tag_autocreate_commit}) {
128 IkiWiki::disable_commit_hook();
129 IkiWiki::rcs_add($tagfile);
130 IkiWiki::rcs_commit_staged(message => $message);
131 IkiWiki::enable_commit_hook();
137 sub preprocess_tag (@) {
142 my $page = $params{page};
143 delete $params{page};
144 delete $params{destpage};
145 delete $params{preview};
147 foreach my $tag (keys %params) {
151 add_link($page, taglink($tag), 'tag');
159 sub preprocess_taglink (@) {
164 return join(" ", map {
166 my $tag=linkpage($2);
167 add_link($params{page}, taglink($tag), 'tag');
169 return htmllink_tag($params{page}, $params{destpage}, $tag,
170 linktext => pagetitle($1));
173 my $tag=linkpage($_);
174 add_link($params{page}, taglink($tag), 'tag');
176 return htmllink_tag($params{page}, $params{destpage}, $tag);
180 $_ ne 'page' && $_ ne 'destpage' && $_ ne 'preview'
184 sub pagetemplate (@) {
186 my $page=$params{page};
187 my $destpage=$params{destpage};
188 my $template=$params{template};
190 my $tags = $typedlinks{$page}{tag};
192 $template->param(tags => [
194 link => htmllink_tag($page, $destpage, $_,
195 rel => "tag", linktext => tagname($_))
197 ]) if defined $tags && %$tags && $template->query(name => "tags");
199 if ($template->query(name => "categories")) {
200 # It's an rss/atom template. Add any categories.
201 if (defined $tags && %$tags) {
202 eval q{use HTML::Entities};
203 $template->param(categories =>
204 [map { category => HTML::Entities::encode_entities_numeric(tagname($_)) },
210 package IkiWiki::PageSpec;
212 sub match_tagged ($$;@) {
214 my $glob=IkiWiki::Plugin::tag::taglink(shift);
215 return match_link($page, $glob, linktype => 'tag', @_);