]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/edittemplate.pm
edittemplate: Make silent mode not disable display when the template page does not...
[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                         section => "web",
27                 },
28 }
30 sub needsbuild (@) {
31         my $needsbuild=shift;
33         foreach my $page (keys %pagestate) {
34                 if (exists $pagestate{$page}{edittemplate}) {
35                         if (exists $pagesources{$page} && 
36                             grep { $_ eq $pagesources{$page} } @$needsbuild) {
37                                 # remove state, it will be re-added
38                                 # if the preprocessor directive is still
39                                 # there during the rebuild
40                                 delete $pagestate{$page}{edittemplate};
41                         }
42                 }
43         }
44 }
46 sub preprocess (@) {
47         my %params=@_;
49         return "" if $params{page} ne $params{destpage};
51         if (! exists $params{template} || ! length($params{template})) {
52                 error gettext("template not specified")
53         }
54         if (! exists $params{match} || ! length($params{match})) {
55                 error gettext("match not specified")
56         }
58         my $link=linkpage($params{template});
59         my $bestlink=bestlink($params{page}, $link);
60         $pagestate{$params{page}}{edittemplate}{$params{match}}=$bestlink;
62         add_depends($params{page}, $link, deptype("presence"));
63         return "" if ($params{silent} && IkiWiki::yesno($params{silent})) &&
64                 length $bestlink;
65         return sprintf(gettext("edittemplate %s registered for %s"),
66                 htmllink($params{page}, $params{destpage}, $link),
67                 $params{match});
68 }
70 sub formbuilder (@) {
71         my %params=@_;
72         my $form=$params{form};
74         return if $form->field("do") ne "create" ||
75                 (defined $form->field("editcontent") && length $form->field("editcontent"));
76         
77         my $page=$form->field("page");
78         
79         # The tricky bit here is that $page is probably just the base
80         # page name, without any subdir, but the pagespec for a template
81         # probably does include the subdir (ie, "bugs/*"). We don't know
82         # what subdir the user will pick to put the page in. So, try them
83         # all, starting with the one that was made default.
84         my @page_locs=$page;
85         foreach my $field ($form->field) {
86                 if ($field eq 'page') {
87                         @page_locs=$field->def_value;
89                         # FormBuilder is on the bad crack. See #551499
90                         my @options=map { ref $_ ? @$_ : $_ } $field->options;
92                         push @page_locs, @options;
93                 }
94         }
95         foreach my $p (@page_locs) {
96                 foreach my $registering_page (keys %pagestate) {
97                         if (exists $pagestate{$registering_page}{edittemplate}) {
98                                 foreach my $pagespec (sort keys %{$pagestate{$registering_page}{edittemplate}}) {
99                                         if (pagespec_match($p, $pagespec, location => $registering_page)) {
100                                                 my $template=$pagestate{$registering_page}{edittemplate}{$pagespec};
101                                                 $form->field(name => "editcontent",
102                                                          value =>  filltemplate($template, $page));
103                                                 $form->field(name => "type",
104                                                          value => pagetype($pagesources{$template}))
105                                                                 if $pagesources{$template};
106                                                 return;
107                                         }
108                                 }
109                         }
110                 }
111         }
114 sub filltemplate ($$) {
115         my $template_page=shift;
116         my $page=shift;
118         my $template;
119         eval {
120                 # force page name absolute so it doesn't look in templates/
121                 $template=template("/".$template_page);
122         };
123         if ($@) {
124                 # Indicate that the earlier preprocessor directive set 
125                 # up a template that doesn't work.
126                 return "[[!pagetemplate ".gettext("failed to process template:")." $@]]";
127         }
128         if (! defined $template) {
129                 return;
130         }
132         $template->param(name => $page);
134         return $template->output;