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);
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};
49 return "" if $params{page} ne $params{destpage};
51 if (! exists $params{template} || ! length($params{template})) {
52 error gettext("template not specified")
54 if (! exists $params{match} || ! length($params{match})) {
55 error gettext("match not specified")
58 my $link=linkpage($params{template});
59 my $bestlink=bestlink($params{page}, $link);
60 $pagestate{$params{page}}{edittemplate}{$params{match}}=$bestlink;
62 return "" if ($params{silent} && IkiWiki::yesno($params{silent}));
63 add_depends($params{page}, $link, deptype("presence"));
64 return sprintf(gettext("edittemplate %s registered for %s"),
65 htmllink($params{page}, $params{destpage}, $link),
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;
88 # FormBuilder is on the bad crack. See #551499
89 my @options=map { ref $_ ? @$_ : $_ } $field->options;
91 push @page_locs, @options;
94 foreach my $p (@page_locs) {
95 foreach my $registering_page (keys %pagestate) {
96 if (exists $pagestate{$registering_page}{edittemplate}) {
97 foreach my $pagespec (sort keys %{$pagestate{$registering_page}{edittemplate}}) {
98 if (pagespec_match($p, $pagespec, location => $registering_page)) {
99 my $template=$pagestate{$registering_page}{edittemplate}{$pagespec};
100 $form->field(name => "editcontent",
101 value => filltemplate($template, $page));
102 $form->field(name => "type",
103 value => pagetype($pagesources{$template}))
104 if $pagesources{$template};
113 sub filltemplate ($$) {
114 my $template_page=shift;
119 # force page name absolute so it doesn't look in templates/
120 $template=template("/".$template_page);
123 # Indicate that the earlier preprocessor directive set
124 # up a template that doesn't work.
125 return "[[!pagetemplate ".gettext("failed to process template:")." $@]]";
127 if (! defined $template) {
131 $template->param(name => $page);
133 return $template->output;