inline: Fix regression in feed titles. Closes: #610878 (Thanks, Paul Wise)
[ikiwiki.git] / IkiWiki / Plugin / tag.pm
blobfd5ce1e8a91488ce5d5b20709001fd3d177def97
1 #!/usr/bin/perl
2 # Ikiwiki tag plugin.
3 package IkiWiki::Plugin::tag;
5 use warnings;
6 use strict;
7 use IkiWiki 3.00;
9 sub import {
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);
17 sub getopt () {
18 eval q{use Getopt::Long};
19 error($@) if $@;
20 Getopt::Long::Configure('pass_through');
21 GetOptions("tagbase=s" => \$config{tagbase});
24 sub getsetup () {
25 return
26 plugin => {
27 safe => 1,
28 rebuild => undef,
30 tagbase => {
31 type => "string",
32 example => "tag",
33 description => "parent page tags are located under",
34 safe => 1,
35 rebuild => 1,
37 tag_autocreate => {
38 type => "boolean",
39 example => 1,
40 description => "autocreate new tag pages?",
41 safe => 1,
42 rebuild => undef,
46 sub taglink ($) {
47 my $tag=shift;
49 if ($tag !~ m{^/} &&
50 defined $config{tagbase}) {
51 $tag="/".$config{tagbase}."/".$tag;
52 $tag=~y#/#/#s; # squash dups
55 return $tag;
58 # Returns a tag name from a tag link
59 sub tagname ($) {
60 my $tag=shift;
61 if (defined $config{tagbase}) {
62 $tag =~ s!^/\Q$config{tagbase}\E/!!;
63 } else {
64 $tag =~ s!^\.?/!!;
66 return pagetitle($tag, 1);
69 sub htmllink_tag ($$$;@) {
70 my $page=shift;
71 my $destpage=shift;
72 my $tag=shift;
73 my %opts=@_;
75 return htmllink($page, $destpage, taglink($tag), %opts);
78 sub gentag ($) {
79 my $tag=shift;
81 if ($config{tag_autocreate} ||
82 ($config{tagbase} && ! defined $config{tag_autocreate})) {
83 my $tagpage=taglink($tag);
84 if ($tagpage=~/^\.\/(.*)/) {
85 $tagpage=$1;
87 else {
88 $tagpage=~s/^\///;
91 my $tagfile = newpagefile($tagpage, $config{default_pageext});
93 add_autofile($tagfile, "tag", sub {
94 my $message=sprintf(gettext("creating tag page %s"), $tagpage);
95 debug($message);
97 my $template=template("autotag.tmpl");
98 $template->param(tagname => tagname($tag));
99 $template->param(tag => $tag);
100 writefile($tagfile, $config{srcdir}, $template->output);
101 if ($config{rcs}) {
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 (@) {
112 if (! @_) {
113 return "";
115 my %params=@_;
116 my $page = $params{page};
117 delete $params{page};
118 delete $params{destpage};
119 delete $params{preview};
121 foreach my $tag (keys %params) {
122 $tag=linkpage($tag);
124 # hidden WikiLink
125 add_link($page, taglink($tag), 'tag');
127 gentag($tag);
130 return "";
133 sub preprocess_taglink (@) {
134 if (! @_) {
135 return "";
137 my %params=@_;
138 return join(" ", map {
139 if (/(.*)\|(.*)/) {
140 my $tag=linkpage($2);
141 add_link($params{page}, taglink($tag), 'tag');
142 gentag($tag);
143 return htmllink_tag($params{page}, $params{destpage}, $tag,
144 linktext => pagetitle($1));
146 else {
147 my $tag=linkpage($_);
148 add_link($params{page}, taglink($tag), 'tag');
149 gentag($tag);
150 return htmllink_tag($params{page}, $params{destpage}, $tag);
153 grep {
154 $_ ne 'page' && $_ ne 'destpage' && $_ ne 'preview'
155 } keys %params);
158 sub pagetemplate (@) {
159 my %params=@_;
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 => [
167 map {
168 link => htmllink_tag($page, $destpage, $_,
169 rel => "tag", linktext => tagname($_))
170 }, sort keys %$tags
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($_) },
177 sort keys %$tags]);
182 package IkiWiki::PageSpec;
184 sub match_tagged ($$;@) {
185 my $page=shift;
186 my $glob=IkiWiki::Plugin::tag::taglink(shift);
187 return match_link($page, $glob, linktype => 'tag', @_);