2 package IkiWiki::Plugin::edittemplate;
11 hook(type => "getsetup", id => "edittemplate",
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",
23 sub getsetup () { #{{{
31 sub needsbuild (@) { #{{{
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};
47 sub preprocess (@) { #{{{
50 return "" if $params{page} ne $params{destpage};
52 if (! exists $params{template} || ! length($params{template})) {
53 error gettext("template not specified")
55 if (! exists $params{match} || ! length($params{match})) {
56 error gettext("match not specified")
59 my $link=linkpage($params{template});
60 $pagestate{$params{page}}{edittemplate}{$params{match}}=$link;
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),
69 sub formbuilder (@) { #{{{
71 my $form=$params{form};
73 return if $form->field("do") ne "create" ||
74 (defined $form->field("editcontent") && length $form->field("editcontent"));
76 my $page=$form->field("page");
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.
84 foreach my $field ($form->field) {
85 if ($field eq 'page') {
86 @page_locs=$field->def_value;
87 push @page_locs, $field->options;
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};
110 sub filltemplate ($$) { #{{{
111 my $template_page=shift;
114 my $template_file=$pagesources{$template_page};
115 if (! defined $template_file) {
121 $template=HTML::Template->new(
123 my $text_ref = shift;
124 $$text_ref=&Encode::decode_utf8($$text_ref);
127 filename => srcfile($template_file),
128 die_on_bad_params => 0,
133 # Indicate that the earlier preprocessor directive set
134 # up a template that doesn't work.
135 return "[[!pagetemplate ".gettext("failed to process")." $@]]";
138 $template->param(name => $page);
140 return $template->output;