]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/edittemplate.pm
improve invalid gettext message
[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 3.00;
7 use HTML::Template;
8 use Encode;
10 sub import {
11         hook(type => "getsetup", id => "edittemplate",
12                 call => \&getsetup);
13         hook(type => "needsbuild", id => "edittemplate",
14                 call => \&needsbuild);
15         hook(type => "preprocess", id => "edittemplate",
16                 call => \&preprocess);
17         hook(type => "formbuilder", id => "edittemplate",
18                 call => \&formbuilder);
19 }
21 sub getsetup () {
22         return
23                 plugin => {
24                         safe => 1,
25                         rebuild => undef,
26                 },
27 }
29 sub needsbuild (@) {
30         my $needsbuild=shift;
32         foreach my $page (keys %pagestate) {
33                 if (exists $pagestate{$page}{edittemplate}) {
34                         if (exists $pagesources{$page} && 
35                             grep { $_ eq $pagesources{$page} } @$needsbuild) {
36                                 # remove state, it will be re-added
37                                 # if the preprocessor directive is still
38                                 # there during the rebuild
39                                 delete $pagestate{$page}{edittemplate};
40                         }
41                 }
42         }
43 }
45 sub preprocess (@) {
46         my %params=@_;
48         return "" if $params{page} ne $params{destpage};
50         if (! exists $params{template} || ! length($params{template})) {
51                 error gettext("template not specified")
52         }
53         if (! exists $params{match} || ! length($params{match})) {
54                 error gettext("match not specified")
55         }
57         my $link=linkpage($params{template});
58         $pagestate{$params{page}}{edittemplate}{$params{match}}=$link;
60         return "" if ($params{silent} && IkiWiki::yesno($params{silent}));
61         add_depends($params{page}, $link);
62         return sprintf(gettext("edittemplate %s registered for %s"),
63                 htmllink($params{page}, $params{destpage}, $link),
64                 $params{match});
65 }
67 sub formbuilder (@) {
68         my %params=@_;
69         my $form=$params{form};
71         return if $form->field("do") ne "create" ||
72                 (defined $form->field("editcontent") && length $form->field("editcontent"));
73         
74         my $page=$form->field("page");
75         
76         # The tricky bit here is that $page is probably just the base
77         # page name, without any subdir, but the pagespec for a template
78         # probably does include the subdir (ie, "bugs/*"). We don't know
79         # what subdir the user will pick to put the page in. So, try them
80         # all, starting with the one that was made default.
81         my @page_locs=$page;
82         foreach my $field ($form->field) {
83                 if ($field eq 'page') {
84                         @page_locs=$field->def_value;
85                         push @page_locs, $field->options;
86                 }
87         }
89         foreach my $p (@page_locs) {
90                 foreach my $registering_page (keys %pagestate) {
91                         if (exists $pagestate{$registering_page}{edittemplate}) {
92                                 foreach my $pagespec (sort keys %{$pagestate{$registering_page}{edittemplate}}) {
93                                         if (pagespec_match($p, $pagespec, location => $registering_page)) {
94                                                 my $template=$pagestate{$registering_page}{edittemplate}{$pagespec};
95                                                 $form->field(name => "editcontent",
96                                                          value =>  filltemplate($template, $page));
97                                                 $form->field(name => "type",
98                                                          value => pagetype($pagesources{$template}))
99                                                                 if $pagesources{$template};
100                                                 return;
101                                         }
102                                 }
103                         }
104                 }
105         }
108 sub filltemplate ($$) {
109         my $template_page=shift;
110         my $page=shift;
112         my $template_file=$pagesources{$template_page};
113         if (! defined $template_file) {
114                 return;
115         }
117         my $template;
118         eval {
119                 $template=HTML::Template->new(
120                         filter => sub {
121                                 my $text_ref = shift;
122                                 $$text_ref=&Encode::decode_utf8($$text_ref);
123                                 chomp $$text_ref;
124                         },
125                         filename => srcfile($template_file),
126                         die_on_bad_params => 0,
127                         no_includes => 1,
128                 );
129         };
130         if ($@) {
131                 # Indicate that the earlier preprocessor directive set 
132                 # up a template that doesn't work.
133                 return "[[!pagetemplate ".gettext("failed to process")." $@]]";
134         }
136         $template->param(name => $page);
138         return $template->output;