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