* Add templatefile hook.
[ikiwiki] / IkiWiki / Plugin / pagetemplate.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::pagetemplate;
3
4 use warnings;
5 use strict;
6 use IkiWiki 2.00;
7
8 my %templates;
9
10 sub import { #{{{
11         hook(type => "preprocess", id => "pagetemplate", call => \&preprocess);
12         hook(type => "templatefile", id => "pagetemplate", call => \&templatefile);
13 } # }}}
14
15 sub preprocess (@) { #{{{
16         my %params=@_;
17
18         if (! exists $params{template} ||
19             $params{template} !~ /^[-A-Za-z0-9._+]+$/ ||
20             ! defined IkiWiki::template_file($params{template})) {
21                  return "[[pagetemplate ".gettext("bad or missing template")."]]";
22         }
23
24         if ($params{page} eq $params{destpage}) {
25                 $templates{$params{page}}=$params{template};
26         }
27
28 } # }}}
29
30 sub templatefile (@) { #{{{
31         my %params=@_;
32
33         if (exists $templates{$params{page}}) {
34                 return $templates{$params{page}};
35         }
36         
37         return undef;
38 } # }}}
39
40 1