2 package IkiWiki::Plugin::edittemplate;
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);
19 sub needsbuild (@) { #{{{
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};
35 sub preprocess (@) { #{{{
38 return "" if $params{page} ne $params{destpage};
40 if (! exists $params{template} || ! length($params{template})) {
41 error gettext("template not specified")
43 if (! exists $params{match} || ! length($params{match})) {
44 error gettext("match not specified")
47 $pagestate{$params{page}}{edittemplate}{$params{match}}=$params{template};
49 return sprintf(gettext("edittemplate %s registered for %s"),
50 $params{template}, $params{match});
53 sub formbuilder (@) { #{{{
55 my $form=$params{form};
57 return if $form->field("do") ne "create";
58 my $page=$form->field("page");
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.
66 foreach my $field ($form->field) {
67 if ($field eq 'page') {
68 @page_locs=$field->def_value;
69 push @page_locs, $field->options;
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));
88 sub filltemplate ($$) { #{{{
89 my $template_page=shift;
92 my $template_file=$pagesources{$template_page};
93 if (! defined $template_file) {
99 $template=HTML::Template->new(
101 my $text_ref = shift;
102 $$text_ref=&Encode::decode_utf8($$text_ref);
105 filename => srcfile($template_file),
106 die_on_bad_params => 0,
111 # Indicate that the earlier preprocessor directive set
112 # up a template that doesn't work.
113 return "[[!pagetemplate ".gettext("failed to process")." $@]]";
116 $template->param(name => $page);
118 return $template->output;