Reorganize index file, add a format version field.
[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 => "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         hook(type => "refresh", id => "edittemplate",
20                 call => \&refresh);
21 } #}}}
22
23 sub getsetup () { #{{{
24         return
25                 plugin => {
26                         safe => 1,
27                         rebuild => undef,
28                 },
29 } #}}}
30
31 sub needsbuild (@) { #{{{
32         my $needsbuild=shift;
33
34         foreach my $page (keys %pagestate) {
35                 if (exists $pagestate{$page}{edittemplate}) {
36                         if (exists $pagesources{$page} && 
37                             grep { $_ eq $pagesources{$page} } @$needsbuild) {
38                                 # remove state, it will be re-added
39                                 # if the preprocessor directive is still
40                                 # there during the rebuild
41                                 delete $pagestate{$page}{edittemplate};
42                         }
43                 }
44         }
45 } #}}}
46
47 sub preprocess (@) { #{{{
48         my %params=@_;
49
50         return "" if $params{page} ne $params{destpage};
51
52         if (! exists $params{template} || ! length($params{template})) {
53                 error gettext("template not specified")
54         }
55         if (! exists $params{match} || ! length($params{match})) {
56                 error gettext("match not specified")
57         }
58
59         my $link=linkpage($params{template});
60         $pagestate{$params{page}}{edittemplate}{$params{match}}=$link;
61
62         return "" if ($params{silent} && IkiWiki::yesno($params{silent}));
63         add_depends($params{page}, $link);
64         return sprintf(gettext("edittemplate %s registered for %s"),
65                 htmllink($params{page}, $params{destpage}, $link),
66                 $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                 (defined $form->field("editcontent") && 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                                                 my $template=$pagestate{$registering_page}{edittemplate}{$pagespec};
97                                                 $form->field(name => "editcontent",
98                                                          value =>  filltemplate($template, $page));
99                                                 $form->field(name => "type",
100                                                          value => pagetype($pagesources{$template}))
101                                                                 if $pagesources{$template};
102                                                 return;
103                                         }
104                                 }
105                         }
106                 }
107         }
108 } #}}}
109
110 sub filltemplate ($$) { #{{{
111         my $template_page=shift;
112         my $page=shift;
113
114         my $template_file=$pagesources{$template_page};
115         if (! defined $template_file) {
116                 return;
117         }
118
119         my $template;
120         eval {
121                 $template=HTML::Template->new(
122                         filter => sub {
123                                 my $text_ref = shift;
124                                 $$text_ref=&Encode::decode_utf8($$text_ref);
125                                 chomp $$text_ref;
126                         },
127                         filename => srcfile($template_file),
128                         die_on_bad_params => 0,
129                         no_includes => 1,
130                 );
131         };
132         if ($@) {
133                 # Indicate that the earlier preprocessor directive set 
134                 # up a template that doesn't work.
135                 return "[[!pagetemplate ".gettext("failed to process")." $@]]";
136         }
137
138         $template->param(name => $page);
139
140         return $template->output;
141 } #}}}
142
143 sub refresh () {
144         
145 }
146
147 1