]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/edittemplate.pm
Merge branch 'master' into recentchanges
[git.ikiwiki.info.git] / IkiWiki / Plugin / edittemplate.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::edittemplate;
4 use warnings;
5 use strict;
6 use IkiWiki 2.00;
7 use HTML::Template;
8 use Encode;
10 sub import { #{{{
11         hook(type => "needsbuild", id => "edittemplate",
12                 call => \&needsbuild);
13         hook(type => "preprocess", id => "edittemplate",
14                 call => \&preprocess);
15         hook(type => "formbuilder", id => "edittemplate",
16                 call => \&formbuilder);
17 } #}}}
19 sub needsbuild (@) { #{{{
20         my $needsbuild=shift;
22         foreach my $page (keys %pagestate) {
23                 if (exists $pagestate{$page}{edittemplate}) {
24                         if (grep { $_ eq $pagesources{$page} } @$needsbuild) {
25                                 # remove state, it will be re-added
26                                 # if the preprocessor directive is still
27                                 # there during the rebuild
28                                 delete $pagestate{$page}{edittemplate};
29                         }
30                 }
31         }
32 } #}}}
34 sub preprocess (@) { #{{{
35         my %params=@_;
37         return "" if $params{page} ne $params{destpage};
39         if (! exists $params{template} || ! length($params{template})) {
40                 return return "[[meta ".gettext("template not specified")."]]";
41         }
42         if (! exists $params{match} || ! length($params{match})) {
43                 return return "[[meta ".gettext("match not specified")."]]";
44         }
46         $pagestate{$params{page}}{edittemplate}{$params{match}}=$params{template};
48         return sprintf(gettext("edittemplate %s registered for %s"),
49                 $params{template}, $params{match});
50 } # }}}
52 sub formbuilder (@) { #{{{
53         my %params=@_;
54         my $form=$params{form};
56         return if $form->field("do") ne "create";
57         my $page=$form->field("page");
58         
59         # The tricky bit here is that $page is probably just the base
60         # page name, without any subdir, but the pagespec for a template
61         # probably does include the subdir (ie, "bugs/*"). We don't know
62         # what subdir the user will pick to put the page in. So, try them
63         # all, starting with the one that was made default.
64         my @page_locs=$page;
65         foreach my $field ($form->field) {
66                 if ($field eq 'page') {
67                         @page_locs=$field->def_value;
68                         push @page_locs, $field->options;
69                 }
70         }
72         foreach my $p (@page_locs) {
73                 foreach my $registering_page (keys %pagestate) {
74                         if (exists $pagestate{$registering_page}{edittemplate}) {
75                                 foreach my $pagespec (sort keys %{$pagestate{$registering_page}{edittemplate}}) {
76                                         if (pagespec_match($p, $pagespec, location => $registering_page)) {
77                                                 $form->field(name => "editcontent",
78                                                          value => filltemplate($pagestate{$registering_page}{edittemplate}{$pagespec}, $page));
79                                                 return;
80                                         }
81                                 }
82                         }
83                 }
84         }
85 } #}}}
87 sub filltemplate ($$) { #{{{
88         my $template_page=shift;
89         my $page=shift;
91         my $template_file=$pagesources{$template_page};
92         if (! defined $template_file) {
93                 return;
94         }
96         my $template;
97         eval {
98                 $template=HTML::Template->new(
99                         filter => sub {
100                                 my $text_ref = shift;
101                                 $$text_ref=&Encode::decode_utf8($$text_ref);
102                                 chomp $$text_ref;
103                         },
104                         filename => srcfile($template_file),
105                         die_on_bad_params => 0,
106                         no_includes => 1,
107                 );
108         };
109         if ($@) {
110                 return "[[pagetemplate ".gettext("failed to process")." $@]]";
111         }
113         $template->param(name => $page);
115         return $template->output;
116 } #}}}