summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/edittemplate.pm
blob: cdcdd074f5ff63f64ed7190c6b84b221f5a697ca (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 sprintf(gettext("edittemplate %s registered for %s"),
  50. $params{template}, $params{match});
  51. } # }}}
  52. sub formbuilder (@) { #{{{
  53. my %params=@_;
  54. my $form=$params{form};
  55. return if $form->field("do") ne "create";
  56. my $page=$form->field("page");
  57. # The tricky bit here is that $page is probably just the base
  58. # page name, without any subdir, but the pagespec for a template
  59. # probably does include the subdir (ie, "bugs/*"). We don't know
  60. # what subdir the user will pick to put the page in. So, try them
  61. # all, starting with the one that was made default.
  62. my @page_locs=$page;
  63. foreach my $field ($form->field) {
  64. if ($field eq 'page') {
  65. @page_locs=$field->def_value;
  66. push @page_locs, $field->options;
  67. }
  68. }
  69. foreach my $p (@page_locs) {
  70. foreach my $registering_page (keys %pagestate) {
  71. if (exists $pagestate{$registering_page}{edittemplate}) {
  72. foreach my $pagespec (sort keys %{$pagestate{$registering_page}{edittemplate}}) {
  73. if (pagespec_match($p, $pagespec, location => $registering_page)) {
  74. $form->field(name => "editcontent",
  75. value => filltemplate($pagestate{$registering_page}{edittemplate}{$pagespec}, $page));
  76. return;
  77. }
  78. }
  79. }
  80. }
  81. }
  82. } #}}}
  83. sub filltemplate ($$) { #{{{
  84. my $template_page=shift;
  85. my $page=shift;
  86. my $template_file=$pagesources{$template_page};
  87. if (! defined $template_file) {
  88. return;
  89. }
  90. my $template;
  91. eval {
  92. $template=HTML::Template->new(
  93. filter => sub {
  94. my $text_ref = shift;
  95. $$text_ref=&Encode::decode_utf8($$text_ref);
  96. chomp $$text_ref;
  97. },
  98. filename => srcfile($template_file),
  99. die_on_bad_params => 0,
  100. no_includes => 1,
  101. );
  102. };
  103. if ($@) {
  104. # Indicate that the earlier preprocessor directive set
  105. # up a template that doesn't work.
  106. return "[[!pagetemplate ".gettext("failed to process")." $@]]";
  107. }
  108. $template->param(name => $page);
  109. return $template->output;
  110. } #}}}
  111. 1