summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/edittemplate.pm
blob: c381940e5edd4056b5622c0daeb10cab1d6423a8 (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 => "getsetup", id => "edittemplate",
  10. call => \&getsetup);
  11. hook(type => "needsbuild", id => "edittemplate",
  12. call => \&needsbuild);
  13. hook(type => "preprocess", id => "edittemplate",
  14. call => \&preprocess);
  15. hook(type => "formbuilder", id => "edittemplate",
  16. call => \&formbuilder);
  17. } #}}}
  18. sub getsetup () { #{{{
  19. return
  20. plugin => {
  21. safe => 1,
  22. rebuild => undef,
  23. },
  24. } #}}}
  25. sub needsbuild (@) { #{{{
  26. my $needsbuild=shift;
  27. foreach my $page (keys %pagestate) {
  28. if (exists $pagestate{$page}{edittemplate}) {
  29. if (exists $pagesources{$page} &&
  30. grep { $_ eq $pagesources{$page} } @$needsbuild) {
  31. # remove state, it will be re-added
  32. # if the preprocessor directive is still
  33. # there during the rebuild
  34. delete $pagestate{$page}{edittemplate};
  35. }
  36. }
  37. }
  38. } #}}}
  39. sub preprocess (@) { #{{{
  40. my %params=@_;
  41. return "" if $params{page} ne $params{destpage};
  42. if (! exists $params{template} || ! length($params{template})) {
  43. error gettext("template not specified")
  44. }
  45. if (! exists $params{match} || ! length($params{match})) {
  46. error gettext("match not specified")
  47. }
  48. $pagestate{$params{page}}{edittemplate}{$params{match}}=$params{template};
  49. return "" if ($params{silent} && IkiWiki::yesno($params{silent}));
  50. my $link=IkiWiki::linkpage($params{template});
  51. add_depends($params{page}, $link);
  52. my $linkHTML = htmllink($params{page}, $params{destpage}, $link);
  53. return sprintf(gettext("edittemplate: %s registered for %s"),
  54. $linkHTML, $params{match});
  55. } # }}}
  56. sub formbuilder (@) { #{{{
  57. my %params=@_;
  58. my $form=$params{form};
  59. return if $form->field("do") ne "create" ||
  60. length $form->field("editcontent");
  61. my $page=$form->field("page");
  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, try them
  66. # all, starting with the one that was made default.
  67. my @page_locs=$page;
  68. foreach my $field ($form->field) {
  69. if ($field eq 'page') {
  70. @page_locs=$field->def_value;
  71. push @page_locs, $field->options;
  72. }
  73. }
  74. foreach my $p (@page_locs) {
  75. foreach my $registering_page (keys %pagestate) {
  76. if (exists $pagestate{$registering_page}{edittemplate}) {
  77. foreach my $pagespec (sort keys %{$pagestate{$registering_page}{edittemplate}}) {
  78. if (pagespec_match($p, $pagespec, location => $registering_page)) {
  79. $form->field(name => "editcontent",
  80. value => filltemplate($pagestate{$registering_page}{edittemplate}{$pagespec}, $page));
  81. $form->field(name => "type",
  82. value => pagetype($pagesources{$pagestate{$registering_page}{edittemplate}{$pagespec}}))
  83. if $pagesources{$pagestate{$registering_page}{edittemplate}{$pagespec}};
  84. return;
  85. }
  86. }
  87. }
  88. }
  89. }
  90. } #}}}
  91. sub filltemplate ($$) { #{{{
  92. my $template_page=shift;
  93. my $page=shift;
  94. my $template_file=$pagesources{$template_page};
  95. if (! defined $template_file) {
  96. return;
  97. }
  98. my $template;
  99. eval {
  100. $template=HTML::Template->new(
  101. filter => sub {
  102. my $text_ref = shift;
  103. $$text_ref=&Encode::decode_utf8($$text_ref);
  104. chomp $$text_ref;
  105. },
  106. filename => srcfile($template_file),
  107. die_on_bad_params => 0,
  108. no_includes => 1,
  109. );
  110. };
  111. if ($@) {
  112. # Indicate that the earlier preprocessor directive set
  113. # up a template that doesn't work.
  114. return "[[!pagetemplate ".gettext("failed to process")." $@]]";
  115. }
  116. $template->param(name => $page);
  117. return $template->output;
  118. } #}}}
  119. 1