the rename hook can now change the list of pages to rename
[ikiwiki] / IkiWiki / Plugin / template.pm
1 #!/usr/bin/perl
2 # Structured template plugin.
3 package IkiWiki::Plugin::template;
4
5 use warnings;
6 use strict;
7 use IkiWiki 2.00;
8 use HTML::Template;
9 use Encode;
10
11 sub import {
12         hook(type => "getsetup", id => "template", call => \&getsetup);
13         hook(type => "preprocess", id => "template", call => \&preprocess);
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{id}) {
28                 error gettext("missing id parameter")
29         }
30
31         my $template_page="templates/$params{id}";
32         add_depends($params{page}, $template_page);
33
34         my $template_file=$pagesources{$template_page};
35         return sprintf(gettext("template %s not found"),
36                 htmllink($params{page}, $params{destpage}, "/".$template_page))
37                         unless defined $template_file;
38
39         my $template;
40         eval {
41                 $template=HTML::Template->new(
42                         filter => sub {
43                                 my $text_ref = shift;
44                                 $$text_ref=&Encode::decode_utf8($$text_ref);
45                                 chomp $$text_ref;
46                         },
47                         filename => srcfile($template_file),
48                         die_on_bad_params => 0,
49                         no_includes => 1,
50                         blind_cache => 1,
51                 );
52         };
53         if ($@) {
54                 error gettext("failed to process:")." $@"
55         }
56
57         $params{basename}=IkiWiki::basename($params{page});
58
59         foreach my $param (keys %params) {
60                 if ($template->query(name => $param)) {
61                         $template->param($param =>
62                                 IkiWiki::htmlize($params{page}, $params{destpage},
63                                         pagetype($pagesources{$params{page}}),
64                                         $params{$param}));
65                 }
66                 if ($template->query(name => "raw_$param")) {
67                         $template->param("raw_$param" => $params{$param});
68                 }
69         }
70
71         return IkiWiki::preprocess($params{page}, $params{destpage},
72                 IkiWiki::filter($params{page}, $params{destpage},
73                 $template->output));
74 }
75
76 1