]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/edittemplate.pm
edittemplate: Look for template pages under templates/ like everything else (still...
[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 3.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                         section => "web",
27                 },
28 }
29
30 sub needsbuild (@) {
31         my $needsbuild=shift;
32
33         foreach my $page (keys %pagestate) {
34                 if (exists $pagestate{$page}{edittemplate}) {
35                         if (exists $pagesources{$page} && 
36                             grep { $_ eq $pagesources{$page} } @$needsbuild) {
37                                 # remove state, it will be re-added
38                                 # if the preprocessor directive is still
39                                 # there during the rebuild
40                                 delete $pagestate{$page}{edittemplate};
41                         }
42                 }
43         }
44 }
45
46 sub preprocess (@) {
47         my %params=@_;
48
49         return "" if $params{page} ne $params{destpage};
50
51         if (! exists $params{template} || ! length($params{template})) {
52                 error gettext("template not specified")
53         }
54         if (! exists $params{match} || ! length($params{match})) {
55                 error gettext("match not specified")
56         }
57
58         my $link=linkpage($params{template});
59         add_depends($params{page}, $link, deptype("presence"));
60         my $bestlink=bestlink($params{page}, $link);
61         if (! length $bestlink) {
62                 add_depends($params{page}, "templates/$link", deptype("presence"));
63                 $link="/templates/".$link;
64                 $bestlink=bestlink($params{page}, $link);
65         }
66         $pagestate{$params{page}}{edittemplate}{$params{match}}=$bestlink;
67
68         return "" if ($params{silent} && IkiWiki::yesno($params{silent})) &&
69                 length $bestlink;
70         return sprintf(gettext("edittemplate %s registered for %s"),
71                 htmllink($params{page}, $params{destpage}, $link),
72                 $params{match});
73 }
74
75 sub formbuilder (@) {
76         my %params=@_;
77         my $form=$params{form};
78
79         return if $form->field("do") ne "create" ||
80                 (defined $form->field("editcontent") && length $form->field("editcontent"));
81         
82         my $page=$form->field("page");
83         
84         # The tricky bit here is that $page is probably just the base
85         # page name, without any subdir, but the pagespec for a template
86         # probably does include the subdir (ie, "bugs/*"). We don't know
87         # what subdir the user will pick to put the page in. So, try them
88         # all, starting with the one that was made default.
89         my @page_locs=$page;
90         foreach my $field ($form->field) {
91                 if ($field eq 'page') {
92                         @page_locs=$field->def_value;
93
94                         # FormBuilder is on the bad crack. See #551499
95                         my @options=map { ref $_ ? @$_ : $_ } $field->options;
96
97                         push @page_locs, @options;
98                 }
99         }
100         foreach my $p (@page_locs) {
101                 foreach my $registering_page (keys %pagestate) {
102                         if (exists $pagestate{$registering_page}{edittemplate}) {
103                                 foreach my $pagespec (sort keys %{$pagestate{$registering_page}{edittemplate}}) {
104                                         if (pagespec_match($p, $pagespec, location => $registering_page)) {
105                                                 my $template=$pagestate{$registering_page}{edittemplate}{$pagespec};
106                                                 $form->field(name => "editcontent",
107                                                          value =>  filltemplate($template, $page));
108                                                 $form->field(name => "type",
109                                                          value => pagetype($pagesources{$template}))
110                                                                 if $pagesources{$template};
111                                                 return;
112                                         }
113                                 }
114                         }
115                 }
116         }
117 }
118
119 sub filltemplate ($$) {
120         my $template_page=shift;
121         my $page=shift;
122
123         my $template;
124         eval {
125                 # force page name absolute so it doesn't look in templates/
126                 $template=template("/".$template_page);
127         };
128         if ($@) {
129                 # Indicate that the earlier preprocessor directive set 
130                 # up a template that doesn't work.
131                 return "[[!pagetemplate ".gettext("failed to process template:")." $@]]";
132         }
133         if (! defined $template) {
134                 return;
135         }
136
137         $template->param(name => $page);
138
139         return $template->output;
140 }
141
142 1