indentation
[ikiwiki.git] / IkiWiki / Plugin / tag.pm
blob55064a9a3cef72c92f3ebc3cb43fa9ef7646d256
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 sub htmllink_tag ($$$;@) {
59 my $page=shift;
60 my $destpage=shift;
61 my $tag=shift;
62 my %opts=@_;
64 return htmllink($page, $destpage, taglink($tag), %opts);
67 sub gentag ($) {
68 my $tag=shift;
70 if ($config{tag_autocreate} ||
71 ($config{tagbase} && ! defined $config{tag_autocreate})) {
72 my $tagpage=taglink($tag);
73 if ($tagpage=~/^\.\/(.*)/) {
74 $tagpage=$1;
76 else {
77 $tagpage=~s/^\///;
80 my $tagfile = newpagefile($tagpage, $config{default_pageext});
82 add_autofile($tagfile, "tag", sub {
83 my $message=sprintf(gettext("creating tag page %s"), $tagpage);
84 debug($message);
86 my $template=template("autotag.tmpl");
87 $template->param(tagname => IkiWiki::basename($tag));
88 $template->param(tag => $tag);
89 writefile($tagfile, $config{srcdir}, $template->output);
90 if ($config{rcs}) {
91 IkiWiki::disable_commit_hook();
92 IkiWiki::rcs_add($tagfile);
93 IkiWiki::rcs_commit_staged(message => $message);
94 IkiWiki::enable_commit_hook();
96 });
100 sub preprocess_tag (@) {
101 if (! @_) {
102 return "";
104 my %params=@_;
105 my $page = $params{page};
106 delete $params{page};
107 delete $params{destpage};
108 delete $params{preview};
110 foreach my $tag (keys %params) {
111 $tag=linkpage($tag);
113 # hidden WikiLink
114 add_link($page, taglink($tag), 'tag');
116 gentag($tag);
119 return "";
122 sub preprocess_taglink (@) {
123 if (! @_) {
124 return "";
126 my %params=@_;
127 return join(" ", map {
128 if (/(.*)\|(.*)/) {
129 my $tag=linkpage($2);
130 add_link($params{page}, taglink($tag), 'tag');
131 gentag($tag);
132 return htmllink_tag($params{page}, $params{destpage}, $tag,
133 linktext => pagetitle($1));
135 else {
136 my $tag=linkpage($_);
137 add_link($params{page}, taglink($tag), 'tag');
138 gentag($tag);
139 return htmllink_tag($params{page}, $params{destpage}, $tag);
142 grep {
143 $_ ne 'page' && $_ ne 'destpage' && $_ ne 'preview'
144 } keys %params);
147 sub pagetemplate (@) {
148 my %params=@_;
149 my $page=$params{page};
150 my $destpage=$params{destpage};
151 my $template=$params{template};
153 my $tags = $typedlinks{$page}{tag};
155 $template->param(tags => [
156 map {
157 link => htmllink_tag($page, $destpage, $_, rel => "tag")
158 }, sort keys %$tags
159 ]) if defined $tags && %$tags && $template->query(name => "tags");
161 if ($template->query(name => "categories")) {
162 # It's an rss/atom template. Add any categories.
163 if (defined $tags && %$tags) {
164 $template->param(categories => [map { category => $_ },
165 sort keys %$tags]);
170 package IkiWiki::PageSpec;
172 sub match_tagged ($$;@) {
173 my $page=shift;
174 my $glob=IkiWiki::Plugin::tag::taglink(shift);
175 return match_link($page, $glob, linktype => 'tag', @_);