summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/edittemplate.pm
blob: 20c948eb1af75d30b6913efafce70198b3b604b5 (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_setup", id => "edittemplate",
  14. call => \&formbuilder_setup);
  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_setup (@) { #{{{
  43. my %params=@_;
  44. my $form=$params{form};
  45. return if $form->title ne "editpage"
  46. || $form->field("do") ne "create";
  47. my $page=$form->field("page");
  48. my $from=$form->field("from");
  49. # The tricky bit here is that $page is probably just the base
  50. # page name, without any subdir, but the pagespec for a template
  51. # probably does include the subdir (ie, "bugs/*"). We don't know
  52. # what subdir the user will pick to put the page in. So, generate
  53. # an ordered list and the first template to match will be used.
  54. #
  55. # This code corresponds to the code in editpage() that generates
  56. # the list of possible page names, unfortunatly, that code runs
  57. # later, so that list can't be simply reused.
  58. my @page_locs=$page;
  59. if (defined $from) {
  60. push @page_locs, "$from/$page";
  61. my $dir=$from.="/";
  62. while (length $dir) {
  63. $dir=~s![^/]+/+$!!;
  64. push @page_locs, $dir.$page;
  65. }
  66. }
  67. foreach my $p (@page_locs) {
  68. foreach my $registering_page (keys %pagestate) {
  69. if (exists $pagestate{$registering_page}{edittemplate}) {
  70. foreach my $pagespec (sort keys %{$pagestate{$registering_page}{edittemplate}}) {
  71. if (pagespec_match($p, $pagespec, location => $registering_page)) {
  72. $form->field(name => "editcontent",
  73. value => filltemplate($pagestate{$registering_page}{edittemplate}{$pagespec}, $page));
  74. return;
  75. }
  76. }
  77. }
  78. }
  79. }
  80. } #}}}
  81. sub filltemplate ($$) { #{{{
  82. my $template_page=shift;
  83. my $page=shift;
  84. my $template_file=$pagesources{$template_page};
  85. if (! defined $template_file) {
  86. return;
  87. }
  88. my $template;
  89. eval {
  90. $template=HTML::Template->new(
  91. filter => sub {
  92. my $text_ref = shift;
  93. $$text_ref=&Encode::decode_utf8($$text_ref);
  94. chomp $$text_ref;
  95. },
  96. filename => srcfile($template_file),
  97. die_on_bad_params => 0,
  98. no_includes => 1,
  99. );
  100. };
  101. if ($@) {
  102. return "[[pagetemplate ".gettext("failed to process")." $@]]";
  103. }
  104. $template->param(name => $page);
  105. return $template->output;
  106. } #}}}
  107. 1