]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/edittemplate.pm
edittemplate patch from Willu
[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 => "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         $pagestate{$params{page}}{edittemplate}{$params{match}}=$params{template};
58
59         return "" if ($params{silent} && IkiWiki::yesno($params{silent}));
60
61         my $link=IkiWiki::linkpage($params{template});
62         add_depends($params{page}, $link);
63         my $linkHTML = htmllink($params{page}, $params{destpage}, $link);
64
65         return sprintf(gettext("edittemplate: %s registered for %s"),
66                 $linkHTML, $params{match});
67 } # }}}
68
69 sub formbuilder (@) { #{{{
70         my %params=@_;
71         my $form=$params{form};
72
73         return if $form->field("do") ne "create" ||
74                 length $form->field("editcontent");
75         
76         my $page=$form->field("page");
77         
78         # The tricky bit here is that $page is probably just the base
79         # page name, without any subdir, but the pagespec for a template
80         # probably does include the subdir (ie, "bugs/*"). We don't know
81         # what subdir the user will pick to put the page in. So, try them
82         # all, starting with the one that was made default.
83         my @page_locs=$page;
84         foreach my $field ($form->field) {
85                 if ($field eq 'page') {
86                         @page_locs=$field->def_value;
87                         push @page_locs, $field->options;
88                 }
89         }
90
91         foreach my $p (@page_locs) {
92                 foreach my $registering_page (keys %pagestate) {
93                         if (exists $pagestate{$registering_page}{edittemplate}) {
94                                 foreach my $pagespec (sort keys %{$pagestate{$registering_page}{edittemplate}}) {
95                                         if (pagespec_match($p, $pagespec, location => $registering_page)) {
96                                                 $form->field(name => "editcontent",
97                                                          value => filltemplate($pagestate{$registering_page}{edittemplate}{$pagespec}, $page));
98                                                 $form->field(name => "type",
99                                                          value => pagetype($pagesources{$pagestate{$registering_page}{edittemplate}{$pagespec}}))
100                                                                 if $pagesources{$pagestate{$registering_page}{edittemplate}{$pagespec}};
101                                                 return;
102                                         }
103                                 }
104                         }
105                 }
106         }
107 } #}}}
108
109 sub filltemplate ($$) { #{{{
110         my $template_page=shift;
111         my $page=shift;
112
113         my $template_file=$pagesources{$template_page};
114         if (! defined $template_file) {
115                 return;
116         }
117
118         my $template;
119         eval {
120                 $template=HTML::Template->new(
121                         filter => sub {
122                                 my $text_ref = shift;
123                                 $$text_ref=&Encode::decode_utf8($$text_ref);
124                                 chomp $$text_ref;
125                         },
126                         filename => srcfile($template_file),
127                         die_on_bad_params => 0,
128                         no_includes => 1,
129                 );
130         };
131         if ($@) {
132                 # Indicate that the earlier preprocessor directive set 
133                 # up a template that doesn't work.
134                 return "[[!pagetemplate ".gettext("failed to process")." $@]]";
135         }
136
137         $template->param(name => $page);
138
139         return $template->output;
140 } #}}}
141
142 1