* Change formbuilder hook to not be responsible for displaying a form,
[ikiwiki] / IkiWiki / Plugin / edittemplate.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::edittemplate;
3
4 use warnings;
5 use strict;
6 use IkiWiki 2.00;
7 use HTML::Template;
8 use Encode;
9
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 } #}}}
18
19 sub needsbuild (@) { #{{{
20         my $needsbuild=shift;
21
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 } #}}}
33
34 sub preprocess (@) { #{{{
35         my %params=@_;
36
37         return "" if $params{page} ne $params{destpage};
38
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         }
45
46         $pagestate{$params{page}}{edittemplate}{$params{match}}=$params{template};
47
48         return sprintf(gettext("edittemplate %s registered for %s"),
49                 $params{template}, $params{match});
50 } # }}}
51
52 sub formbuilder (@) { #{{{
53         my %params=@_;
54         my $form=$params{form};
55         
56         return if $form->title ne "editpage"
57                   || $form->field("do") ne "create";
58         
59         my $page=$form->field("page");
60         my $from=$form->field("from");
61         
62         # The tricky bit here is that $page is probably just the base
63         # page name, without any subdir, but the pagespec for a template
64         # probably does include the subdir (ie, "bugs/*"). We don't know
65         # what subdir the user will pick to put the page in. So, generate
66         # an ordered list and the first template to match will be used.
67         #
68         # This code corresponds to the code in editpage() that generates
69         # the list of possible page names, unfortunatly, that code runs
70         # later, so that list can't be simply reused.
71         my @page_locs=$page;
72         if (defined $from) {
73                 push @page_locs, "$from/$page";
74                 my $dir=$from.="/";
75                 while (length $dir) {
76                         $dir=~s![^/]+/+$!!;
77                         push @page_locs, $dir.$page;
78                 }
79         }
80
81         foreach my $p (@page_locs) {
82                 foreach my $registering_page (keys %pagestate) {
83                         if (exists $pagestate{$registering_page}{edittemplate}) {
84                                 foreach my $pagespec (sort keys %{$pagestate{$registering_page}{edittemplate}}) {
85                                         if (pagespec_match($p, $pagespec, location => $registering_page)) {
86                                                 $form->field(name => "editcontent",
87                                                          value => filltemplate($pagestate{$registering_page}{edittemplate}{$pagespec}, $page));
88                                                 return;
89                                         }
90                                 }
91                         }
92                 }
93         }
94 } #}}}
95
96 sub filltemplate ($$) { #{{{
97         my $template_page=shift;
98         my $page=shift;
99
100         my $template_file=$pagesources{$template_page};
101         if (! defined $template_file) {
102                 return;
103         }
104
105         my $template;
106         eval {
107                 $template=HTML::Template->new(
108                         filter => sub {
109                                 my $text_ref = shift;
110                                 $$text_ref=&Encode::decode_utf8($$text_ref);
111                                 chomp $$text_ref;
112                         },
113                         filename => srcfile($template_file),
114                         die_on_bad_params => 0,
115                         no_includes => 1,
116                 );
117         };
118         if ($@) {
119                 return "[[pagetemplate ".gettext("failed to process")." $@]]";
120         }
121
122         $template->param(name => $page);
123
124         return $template->output;
125 } #}}}
126
127 1