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};
51 return "" if $params{page} ne $params{destpage};
53 if (! exists $params{template} || ! length($params{template})) {
54 error gettext("template not specified")
56 if (! exists $params{match} || ! length($params{match})) {
57 error gettext("match not specified")
60 my $link=linkpage($params{template});
61 add_depends($params{page}, $link, deptype("presence"));
62 my $bestlink=bestlink($params{page}, $link);
63 if (! length $bestlink) {
64 add_depends($params{page}, "templates/$link", deptype("presence"));
65 $link="/templates/".$link;
66 $bestlink=bestlink($params{page}, $link);
68 $pagestate{$params{page}}{edittemplate}{$params{match}}=$bestlink;
70 return "" if ($params{silent} && IkiWiki::yesno($params{silent})) &&
72 return sprintf(gettext("edittemplate %s registered for %s"),
73 htmllink($params{page}, $params{destpage}, $link),
79 my $form=$params{form};
81 return if $form->field("do") ne "create" ||
82 (defined $form->field("editcontent") && length $form->field("editcontent"));
84 my $page=$form->field("page");
86 # The tricky bit here is that $page is probably just the base
87 # page name, without any subdir, but the pagespec for a template
88 # probably does include the subdir (ie, "bugs/*"). We don't know
89 # what subdir the user will pick to put the page in. So, try them
90 # all, starting with the one that was made default.
92 foreach my $field ($form->field) {
93 if ($field eq 'page') {
94 @page_locs=$field->def_value;
96 # FormBuilder is on the bad crack. See #551499
97 my @options=map { ref $_ ? @$_ : $_ } $field->options;
99 push @page_locs, @options;
102 foreach my $p (@page_locs) {
103 foreach my $registering_page (keys %pagestate) {
104 if (exists $pagestate{$registering_page}{edittemplate}) {
105 foreach my $pagespec (sort keys %{$pagestate{$registering_page}{edittemplate}}) {
106 if (pagespec_match($p, $pagespec, location => $registering_page)) {
107 my $template=$pagestate{$registering_page}{edittemplate}{$pagespec};
108 $form->field(name => "editcontent",
109 value => filltemplate($template, $page));
110 $form->field(name => "type",
111 value => pagetype($pagesources{$template}))
112 if $pagesources{$template};
121 sub filltemplate ($$) {
122 my $template_page=shift;
127 # force page name absolute so it doesn't look in templates/
128 $template=template("/".$template_page);
131 # Indicate that the earlier preprocessor directive set
132 # up a template that doesn't work.
133 return "[[!pagetemplate ".gettext("failed to process template:")." $@]]";
136 $template->param(name => $page);
138 return $template->output;