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;
61 if (! $config{tag_autocreate_commit}) {
62 $config{only_committed_changes}=0;
70 defined $config{tagbase}) {
71 $tag="/".$config{tagbase}."/".$tag;
72 $tag=~y#/#/#s; # squash dups
78 # Returns a tag name from a tag link
81 if (defined $config{tagbase}) {
82 $tag =~ s!^/\Q$config{tagbase}\E/!!;
86 return pagetitle($tag, 1);
89 sub htmllink_tag ($$$;@) {
95 return htmllink($page, $destpage, taglink($tag), %opts);
101 if ($config{tag_autocreate} ||
102 ($config{tagbase} && ! defined $config{tag_autocreate})) {
103 my $tagpage=taglink($tag);
104 if ($tagpage=~/^\.\/(.*)/) {
110 if (exists $IkiWiki::pagecase{lc $tagpage}) {
111 $tagpage=$IkiWiki::pagecase{lc $tagpage}
114 my $tagfile = newpagefile($tagpage, $config{default_pageext});
116 add_autofile($tagfile, "tag", sub {
117 my $message=sprintf(gettext("creating tag page %s"), $tagpage);
120 my $template=template("autotag.tmpl");
121 $template->param(tagname => tagname($tag));
122 $template->param(tag => $tag);
124 my $dir = $config{srcdir};
125 if (! $config{tag_autocreate_commit}) {
126 $dir = $IkiWiki::Plugin::transient::transientdir;
129 writefile($tagfile, $dir, $template->output);
130 if ($config{rcs} && $config{tag_autocreate_commit}) {
131 IkiWiki::disable_commit_hook();
132 IkiWiki::rcs_add($tagfile);
133 IkiWiki::rcs_commit_staged(message => $message);
134 IkiWiki::enable_commit_hook();
140 sub preprocess_tag (@) {
145 my $page = $params{page};
146 delete $params{page};
147 delete $params{destpage};
148 delete $params{preview};
150 foreach my $tag (keys %params) {
154 add_link($page, taglink($tag), 'tag');
162 sub preprocess_taglink (@) {
167 return join(" ", map {
169 my $tag=linkpage($2);
170 add_link($params{page}, taglink($tag), 'tag');
172 return htmllink_tag($params{page}, $params{destpage}, $tag,
173 linktext => pagetitle($1));
176 my $tag=linkpage($_);
177 add_link($params{page}, taglink($tag), 'tag');
179 return htmllink_tag($params{page}, $params{destpage}, $tag);
183 $_ ne 'page' && $_ ne 'destpage' && $_ ne 'preview'
187 sub pagetemplate (@) {
189 my $page=$params{page};
190 my $destpage=$params{destpage};
191 my $template=$params{template};
193 my $tags = $typedlinks{$page}{tag};
195 $template->param(tags => [
197 link => htmllink_tag($page, $destpage, $_,
198 rel => "tag", linktext => tagname($_))
200 ]) if defined $tags && %$tags && $template->query(name => "tags");
202 if ($template->query(name => "categories")) {
203 # It's an rss/atom template. Add any categories.
204 if (defined $tags && %$tags) {
205 eval q{use HTML::Entities};
206 $template->param(categories =>
207 [map { category => HTML::Entities::encode_entities_numeric(tagname($_)) },
213 package IkiWiki::PageSpec;
215 sub match_tagged ($$;@) {
217 my $glob=IkiWiki::Plugin::tag::taglink(shift);
218 return match_link($page, $glob, linktype => 'tag', @_);