]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/edittemplate.pm
edittemplate: Don't wipe out edits on preview.
[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 2.00;
7 use HTML::Template;
8 use Encode;
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 } #}}}
19 sub needsbuild (@) { #{{{
20         my $needsbuild=shift;
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 } #}}}
35 sub preprocess (@) { #{{{
36         my %params=@_;
38         return "" if $params{page} ne $params{destpage};
40         if (! exists $params{template} || ! length($params{template})) {
41                 return "[[meta ".gettext("template not specified")."]]";
42         }
43         if (! exists $params{match} || ! length($params{match})) {
44                 return "[[meta ".gettext("match not specified")."]]";
45         }
47         $pagestate{$params{page}}{edittemplate}{$params{match}}=$params{template};
49         return sprintf(gettext("edittemplate %s registered for %s"),
50                 $params{template}, $params{match});
51 } # }}}
53 sub formbuilder (@) { #{{{
54         my %params=@_;
55         my $form=$params{form};
57         return if $form->field("do") ne "create" ||
58                 length $form->field("editcontent");
59         
60         my $page=$form->field("page");
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, try them
66         # all, starting with the one that was made default.
67         my @page_locs=$page;
68         foreach my $field ($form->field) {
69                 if ($field eq 'page') {
70                         @page_locs=$field->def_value;
71                         push @page_locs, $field->options;
72                 }
73         }
75         foreach my $p (@page_locs) {
76                 foreach my $registering_page (keys %pagestate) {
77                         if (exists $pagestate{$registering_page}{edittemplate}) {
78                                 foreach my $pagespec (sort keys %{$pagestate{$registering_page}{edittemplate}}) {
79                                         if (pagespec_match($p, $pagespec, location => $registering_page)) {
80                                                 $form->field(name => "editcontent",
81                                                          value => filltemplate($pagestate{$registering_page}{edittemplate}{$pagespec}, $page));
82                                                 return;
83                                         }
84                                 }
85                         }
86                 }
87         }
88 } #}}}
90 sub filltemplate ($$) { #{{{
91         my $template_page=shift;
92         my $page=shift;
94         my $template_file=$pagesources{$template_page};
95         if (! defined $template_file) {
96                 return;
97         }
99         my $template;
100         eval {
101                 $template=HTML::Template->new(
102                         filter => sub {
103                                 my $text_ref = shift;
104                                 $$text_ref=&Encode::decode_utf8($$text_ref);
105                                 chomp $$text_ref;
106                         },
107                         filename => srcfile($template_file),
108                         die_on_bad_params => 0,
109                         no_includes => 1,
110                 );
111         };
112         if ($@) {
113                 return "[[pagetemplate ".gettext("failed to process")." $@]]";
114         }
116         $template->param(name => $page);
118         return $template->output;
119 } #}}}