added match_guid function to meta plugin
[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 3.00;
8 use Encode;
9
10 sub import {
11         hook(type => "getsetup", id => "template", call => \&getsetup);
12         hook(type => "preprocess", id => "template", call => \&preprocess,
13                 scan => 1);
14 }
15
16 sub getsetup () {
17         return
18                 plugin => {
19                         safe => 1,
20                         rebuild => undef,
21                         section => "widget",
22                 },
23 }
24
25 sub preprocess (@) {
26         my %params=@_;
27
28         # This needs to run even in scan mode, in order to process
29         # links and other metadata included via the template.
30         my $scan=! defined wantarray;
31
32         if (! exists $params{id}) {
33                 error gettext("missing id parameter")
34         }
35
36         # The bare id is used, so a page templates/$id can be used as 
37         # the template.
38         my $template;
39         eval {
40                 $template=template_depends($params{id}, $params{page},
41                         blind_cache => 1);
42         };
43         if ($@) {
44                 error gettext("failed to process template:")." $@";
45         }
46         if (! $template) {
47                 error sprintf(gettext("%s not found"),
48                         htmllink($params{page}, $params{destpage},
49                                 "/templates/$params{id}"))
50         }
51
52         $params{basename}=IkiWiki::basename($params{page});
53
54         foreach my $param (keys %params) {
55                 my $value=IkiWiki::preprocess($params{page}, $params{destpage},
56                           IkiWiki::filter($params{page}, $params{destpagea},
57                           $params{$param}), $scan);
58                 if ($template->query(name => $param)) {
59                         my $htmlvalue=IkiWiki::htmlize($params{page}, $params{destpage},
60                                         pagetype($pagesources{$params{page}}),
61                                         $value);
62                         chomp $htmlvalue;
63                         $template->param($param => $htmlvalue);
64                 }
65                 if ($template->query(name => "raw_$param")) {
66                         chomp $value;
67                         $template->param("raw_$param" => $value);
68                 }
69         }
70
71         return IkiWiki::preprocess($params{page}, $params{destpage},
72                IkiWiki::filter($params{page}, $params{destpage},
73                $template->output), $scan);
74 }
75
76 1