response
[ikiwiki] / IkiWiki / Plugin / edittemplate.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::edittemplate;
3
4 use warnings;
5 use strict;
6 use IkiWiki 3.00;
7 use HTML::Template;
8 use Encode;
9
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 }
20
21 sub getsetup () {
22         return
23                 plugin => {
24                         safe => 1,
25                         rebuild => undef,
26                 },
27 }
28
29 sub needsbuild (@) {
30         my $needsbuild=shift;
31
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 }
44
45 sub preprocess (@) {
46         my %params=@_;
47
48         return "" if $params{page} ne $params{destpage};
49
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         }
56
57         my $link=linkpage($params{template});
58         my $bestlink=bestlink($params{page}, $link);
59         $pagestate{$params{page}}{edittemplate}{$params{match}}=$bestlink;
60
61         return "" if ($params{silent} && IkiWiki::yesno($params{silent}));
62         add_depends($params{page}, $link, deptype("presence"));
63         return sprintf(gettext("edittemplate %s registered for %s"),
64                 htmllink($params{page}, $params{destpage}, $link),
65                 $params{match});
66 }
67
68 sub formbuilder (@) {
69         my %params=@_;
70         my $form=$params{form};
71
72         return if $form->field("do") ne "create" ||
73                 (defined $form->field("editcontent") && length $form->field("editcontent"));
74         
75         my $page=$form->field("page");
76         
77         # The tricky bit here is that $page is probably just the base
78         # page name, without any subdir, but the pagespec for a template
79         # probably does include the subdir (ie, "bugs/*"). We don't know
80         # what subdir the user will pick to put the page in. So, try them
81         # all, starting with the one that was made default.
82         my @page_locs=$page;
83         foreach my $field ($form->field) {
84                 if ($field eq 'page') {
85                         @page_locs=$field->def_value;
86
87                         # FormBuilder is on the bad crack. See #551499
88                         my @options=map { ref $_ ? @$_ : $_ } $field->options;
89
90                         push @page_locs, @options;
91                 }
92         }
93         foreach my $p (@page_locs) {
94                 foreach my $registering_page (keys %pagestate) {
95                         if (exists $pagestate{$registering_page}{edittemplate}) {
96                                 foreach my $pagespec (sort keys %{$pagestate{$registering_page}{edittemplate}}) {
97                                         if (pagespec_match($p, $pagespec, location => $registering_page)) {
98                                                 my $template=$pagestate{$registering_page}{edittemplate}{$pagespec};
99                                                 $form->field(name => "editcontent",
100                                                          value =>  filltemplate($template, $page));
101                                                 $form->field(name => "type",
102                                                          value => pagetype($pagesources{$template}))
103                                                                 if $pagesources{$template};
104                                                 return;
105                                         }
106                                 }
107                         }
108                 }
109         }
110 }
111
112 sub filltemplate ($$) {
113         my $template_page=shift;
114         my $page=shift;
115
116         my $template_file=$pagesources{$template_page};
117         if (! defined $template_file) {
118                 return;
119         }
120
121         my $template;
122         eval {
123                 $template=HTML::Template->new(
124                         filter => sub {
125                                 my $text_ref = shift;
126                                 $$text_ref=&Encode::decode_utf8($$text_ref);
127                                 chomp $$text_ref;
128                         },
129                         filename => srcfile($template_file),
130                         die_on_bad_params => 0,
131                         no_includes => 1,
132                 );
133         };
134         if ($@) {
135                 # Indicate that the earlier preprocessor directive set 
136                 # up a template that doesn't work.
137                 return "[[!pagetemplate ".gettext("failed to process")." $@]]";
138         }
139
140         $template->param(name => $page);
141
142         return $template->output;
143 }
144
145 1