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_setup", id => "edittemplate",
16 call => \&formbuilder_setup);
19 sub needsbuild (@) { #{{{
22 foreach my $page (keys %pagestate) {
23 if (exists $pagestate{$page}{edittemplate}) {
24 if (grep { $_ eq $pagesources{$page} } @$needsbuild) {
25 # remove state, it will be re-added
26 # if the preprocessor directive is still
27 # there during the rebuild
28 delete $pagestate{$page}{edittemplate};
34 sub preprocess (@) { #{{{
37 return "" if $params{page} ne $params{destpage};
39 if (! exists $params{template} || ! length($params{template})) {
40 return return "[[meta ".gettext("template not specified")."]]";
42 if (! exists $params{match} || ! length($params{match})) {
43 return return "[[meta ".gettext("match not specified")."]]";
46 $pagestate{$params{page}}{edittemplate}{$params{match}}=$params{template};
48 return sprintf(gettext("edittemplate %s registered for %s"),
49 $params{template}, $params{match});
52 sub formbuilder_setup (@) { #{{{
54 my $form=$params{form};
56 return if $form->title ne "editpage"
57 || $form->field("do") ne "create";
59 my $page=$form->field("page");
60 my $from=$form->field("from");
62 # The tricky bit here is that $page is probably just the base
63 # page name, without any subdir, but the pagespec for a template
64 # probably does include the subdir (ie, "bugs/*"). We don't know
65 # what subdir the user will pick to put the page in. So, generate
66 # an ordered list and the first template to match will be used.
68 # This code corresponds to the code in editpage() that generates
69 # the list of possible page names, unfortunatly, that code runs
70 # later, so that list can't be simply reused.
73 push @page_locs, "$from/$page";
77 push @page_locs, $dir.$page;
81 foreach my $p (@page_locs) {
82 foreach my $registering_page (keys %pagestate) {
83 if (exists $pagestate{$registering_page}{edittemplate}) {
84 foreach my $pagespec (sort keys %{$pagestate{$registering_page}{edittemplate}}) {
85 if (pagespec_match($p, $pagespec, location => $registering_page)) {
86 $form->field(name => "editcontent",
87 value => filltemplate($pagestate{$registering_page}{edittemplate}{$pagespec}, $page));
96 sub filltemplate ($$) { #{{{
97 my $template_page=shift;
100 my $template_file=$pagesources{$template_page};
101 if (! defined $template_file) {
107 $template=HTML::Template->new(
109 my $text_ref = shift;
110 $$text_ref=&Encode::decode_utf8($$text_ref);
113 filename => srcfile($template_file),
114 die_on_bad_params => 0,
119 return "[[pagetemplate ".gettext("failed to process")." $@]]";
122 $template->param(name => $page);
124 return $template->output;