]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/edittemplate.pm
redirect after removal
[ikiwiki.git] / 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 (exists $pagesources{$page} && 
25                             grep { $_ eq $pagesources{$page} } @$needsbuild) {
26                                 # remove state, it will be re-added
27                                 # if the preprocessor directive is still
28                                 # there during the rebuild
29                                 delete $pagestate{$page}{edittemplate};
30                         }
31                 }
32         }
33 } #}}}
34
35 sub preprocess (@) { #{{{
36         my %params=@_;
37
38         return "" if $params{page} ne $params{destpage};
39
40         if (! exists $params{template} || ! length($params{template})) {
41                 error gettext("template not specified")
42         }
43         if (! exists $params{match} || ! length($params{match})) {
44                 error gettext("match not specified")
45         }
46
47         $pagestate{$params{page}}{edittemplate}{$params{match}}=$params{template};
48
49         return sprintf(gettext("edittemplate %s registered for %s"),
50                 $params{template}, $params{match});
51 } # }}}
52
53 sub formbuilder (@) { #{{{
54         my %params=@_;
55         my $form=$params{form};
56
57         return if $form->field("do") ne "create";
58         my $page=$form->field("page");
59         
60         # The tricky bit here is that $page is probably just the base
61         # page name, without any subdir, but the pagespec for a template
62         # probably does include the subdir (ie, "bugs/*"). We don't know
63         # what subdir the user will pick to put the page in. So, try them
64         # all, starting with the one that was made default.
65         my @page_locs=$page;
66         foreach my $field ($form->field) {
67                 if ($field eq 'page') {
68                         @page_locs=$field->def_value;
69                         push @page_locs, $field->options;
70                 }
71         }
72
73         foreach my $p (@page_locs) {
74                 foreach my $registering_page (keys %pagestate) {
75                         if (exists $pagestate{$registering_page}{edittemplate}) {
76                                 foreach my $pagespec (sort keys %{$pagestate{$registering_page}{edittemplate}}) {
77                                         if (pagespec_match($p, $pagespec, location => $registering_page)) {
78                                                 $form->field(name => "editcontent",
79                                                          value => filltemplate($pagestate{$registering_page}{edittemplate}{$pagespec}, $page));
80                                                 return;
81                                         }
82                                 }
83                         }
84                 }
85         }
86 } #}}}
87
88 sub filltemplate ($$) { #{{{
89         my $template_page=shift;
90         my $page=shift;
91
92         my $template_file=$pagesources{$template_page};
93         if (! defined $template_file) {
94                 return;
95         }
96
97         my $template;
98         eval {
99                 $template=HTML::Template->new(
100                         filter => sub {
101                                 my $text_ref = shift;
102                                 $$text_ref=&Encode::decode_utf8($$text_ref);
103                                 chomp $$text_ref;
104                         },
105                         filename => srcfile($template_file),
106                         die_on_bad_params => 0,
107                         no_includes => 1,
108                 );
109         };
110         if ($@) {
111                 # Indicate that the earlier preprocessor directive set 
112                 # up a template that doesn't work.
113                 return "[[!pagetemplate ".gettext("failed to process")." $@]]";
114         }
115
116         $template->param(name => $page);
117
118         return $template->output;
119 } #}}}
120
121 1