A suggestion to simply extract/merge this functionality from/with another plugin.
[ikiwiki] / IkiWiki / Plugin / pagetemplate.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::pagetemplate;
3
4 use warnings;
5 use strict;
6 use IkiWiki 3.00;
7
8 my %templates;
9
10 sub import {
11         hook(type => "getsetup", id => "pagetemplate", call => \&getsetup);
12         hook(type => "preprocess", id => "pagetemplate", call => \&preprocess);
13         hook(type => "templatefile", id => "pagetemplate", call => \&templatefile);
14 }
15
16 sub getsetup () {
17         return 
18                 plugin => {
19                         safe => 1,
20                         rebuild => undef,
21                 },
22 }
23
24 sub preprocess (@) {
25         my %params=@_;
26
27         if (! exists $params{template} ||
28             $params{template} !~ /^[-A-Za-z0-9._+]+$/ ||
29             ! defined IkiWiki::template_file($params{template})) {
30                  error gettext("bad or missing template")
31         }
32
33         if ($params{page} eq $params{destpage}) {
34                 $templates{$params{page}}=$params{template};
35         }
36
37         return "";
38 }
39
40 sub templatefile (@) {
41         my %params=@_;
42
43         if (exists $templates{$params{page}}) {
44                 return $templates{$params{page}};
45         }
46         
47         return undef;
48 }
49
50 1