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