]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/template.pm
comments: Fix missing entity encoding in title.
[git.ikiwiki.info.git] / IkiWiki / Plugin / template.pm
1 #!/usr/bin/perl
2 # Structured template plugin.
3 package IkiWiki::Plugin::template;
5 use warnings;
6 use strict;
7 use IkiWiki 3.00;
8 use HTML::Template;
9 use Encode;
11 sub import {
12         hook(type => "getsetup", id => "template", call => \&getsetup);
13         hook(type => "preprocess", id => "template", call => \&preprocess,
14                 scan => 1);
15 }
17 sub getsetup () {
18         return
19                 plugin => {
20                         safe => 1,
21                         rebuild => undef,
22                         section => "widget",
23                 },
24 }
26 sub preprocess (@) {
27         my %params=@_;
29         # This needs to run even in scan mode, in order to process
30         # links and other metadata included via the template.
31         my $scan=! defined wantarray;
33         if (! exists $params{id}) {
34                 error gettext("missing id parameter")
35         }
37         my $template_page="templates/$params{id}";
38         add_depends($params{page}, $template_page);
40         my $template_file;
41         if (exists $pagesources{$template_page}) {
42                 $template_file=srcfile($pagesources{$template_page});
43         }
44         else {
45                 $template_file=IkiWiki::template_file("$params{id}.tmpl")
46         }
47         return sprintf(gettext("template %s not found"),
48                 htmllink($params{page}, $params{destpage}, "/".$template_page))
49                         unless defined $template_file;
51         my $template;
52         eval {
53                 $template=HTML::Template->new(
54                         filter => sub {
55                                 my $text_ref = shift;
56                                 $$text_ref=&Encode::decode_utf8($$text_ref);
57                                 chomp $$text_ref;
58                         },
59                         filename => $template_file,
60                         die_on_bad_params => 0,
61                         no_includes => 1,
62                         blind_cache => 1,
63                 );
64         };
65         if ($@) {
66                 error gettext("failed to process:")." $@"
67         }
69         $params{basename}=IkiWiki::basename($params{page});
71         foreach my $param (keys %params) {
72                 my $value=IkiWiki::preprocess($params{page}, $params{destpage},
73                           IkiWiki::filter($params{page}, $params{destpagea},
74                           $params{$param}), $scan);
75                 if ($template->query(name => $param)) {
76                         $template->param($param =>
77                                 IkiWiki::htmlize($params{page}, $params{destpage},
78                                         pagetype($pagesources{$params{page}}),
79                                         $value));
80                 }
81                 if ($template->query(name => "raw_$param")) {
82                         $template->param("raw_$param" => $value);
83                 }
84         }
86         return IkiWiki::preprocess($params{page}, $params{destpage},
87                IkiWiki::filter($params{page}, $params{destpage},
88                $template->output), $scan);
89 }
91 1