summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/edittemplate.pm
blob: d1716a31564031813e7d9f273ab7ff3ca02f3f5c (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::edittemplate;
  3. use warnings;
  4. use strict;
  5. use IkiWiki 2.00;
  6. use HTML::Template;
  7. use Encode;
  8. sub import { #{{{
  9. hook(type => "needsbuild", id => "edittemplate",
  10. call => \&needsbuild);
  11. hook(type => "preprocess", id => "edittemplate",
  12. call => \&preprocess);
  13. hook(type => "formbuilder", id => "edittemplate",
  14. call => \&formbuilder);
  15. } #}}}
  16. sub needsbuild (@) { #{{{
  17. my $needsbuild=shift;
  18. foreach my $page (keys %pagestate) {
  19. if (exists $pagestate{$page}{edittemplate}) {
  20. if (exists $pagesources{$page} &&
  21. grep { $_ eq $pagesources{$page} } @$needsbuild) {
  22. # remove state, it will be re-added
  23. # if the preprocessor directive is still
  24. # there during the rebuild
  25. delete $pagestate{$page}{edittemplate};
  26. }
  27. }
  28. }
  29. } #}}}
  30. sub preprocess (@) { #{{{
  31. my %params=@_;
  32. return "" if $params{page} ne $params{destpage};
  33. if (! exists $params{template} || ! length($params{template})) {
  34. error gettext("template not specified")
  35. }
  36. if (! exists $params{match} || ! length($params{match})) {
  37. error gettext("match not specified")
  38. }
  39. $pagestate{$params{page}}{edittemplate}{$params{match}}=$params{template};
  40. return sprintf(gettext("edittemplate %s registered for %s"),
  41. $params{template}, $params{match});
  42. } # }}}
  43. sub formbuilder (@) { #{{{
  44. my %params=@_;
  45. my $form=$params{form};
  46. return if $form->field("do") ne "create";
  47. my $page=$form->field("page");
  48. # The tricky bit here is that $page is probably just the base
  49. # page name, without any subdir, but the pagespec for a template
  50. # probably does include the subdir (ie, "bugs/*"). We don't know
  51. # what subdir the user will pick to put the page in. So, try them
  52. # all, starting with the one that was made default.
  53. my @page_locs=$page;
  54. foreach my $field ($form->field) {
  55. if ($field eq 'page') {
  56. @page_locs=$field->def_value;
  57. push @page_locs, $field->options;
  58. }
  59. }
  60. foreach my $p (@page_locs) {
  61. foreach my $registering_page (keys %pagestate) {
  62. if (exists $pagestate{$registering_page}{edittemplate}) {
  63. foreach my $pagespec (sort keys %{$pagestate{$registering_page}{edittemplate}}) {
  64. if (pagespec_match($p, $pagespec, location => $registering_page)) {
  65. $form->field(name => "editcontent",
  66. value => filltemplate($pagestate{$registering_page}{edittemplate}{$pagespec}, $page));
  67. return;
  68. }
  69. }
  70. }
  71. }
  72. }
  73. } #}}}
  74. sub filltemplate ($$) { #{{{
  75. my $template_page=shift;
  76. my $page=shift;
  77. my $template_file=$pagesources{$template_page};
  78. if (! defined $template_file) {
  79. return;
  80. }
  81. my $template;
  82. eval {
  83. $template=HTML::Template->new(
  84. filter => sub {
  85. my $text_ref = shift;
  86. $$text_ref=&Encode::decode_utf8($$text_ref);
  87. chomp $$text_ref;
  88. },
  89. filename => srcfile($template_file),
  90. die_on_bad_params => 0,
  91. no_includes => 1,
  92. );
  93. };
  94. if ($@) {
  95. # Indicate that the earlier preprocessor directive set
  96. # up a template that doesn't work.
  97. return "[[!pagetemplate ".gettext("failed to process")." $@]]";
  98. }
  99. $template->param(name => $page);
  100. return $template->output;
  101. } #}}}
  102. 1