summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/edittemplate.pm
blob: 0bafc95d06d854b860e566a8c14134119d51db17 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::edittemplate;
  3. use warnings;
  4. use strict;
  5. use IkiWiki 3.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. my $link=linkpage($params{template});
  49. $pagestate{$params{page}}{edittemplate}{$params{match}}=$link;
  50. return "" if ($params{silent} && IkiWiki::yesno($params{silent}));
  51. add_depends($params{page}, $link);
  52. return sprintf(gettext("edittemplate %s registered for %s"),
  53. htmllink($params{page}, $params{destpage}, $link),
  54. $params{match});
  55. }
  56. sub formbuilder (@) {
  57. my %params=@_;
  58. my $form=$params{form};
  59. return if $form->field("do") ne "create" ||
  60. (defined $form->field("editcontent") && 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. my $template=$pagestate{$registering_page}{edittemplate}{$pagespec};
  80. $form->field(name => "editcontent",
  81. value => filltemplate($template, $page));
  82. $form->field(name => "type",
  83. value => pagetype($pagesources{$template}))
  84. if $pagesources{$template};
  85. return;
  86. }
  87. }
  88. }
  89. }
  90. }
  91. }
  92. sub filltemplate ($$) {
  93. my $template_page=shift;
  94. my $page=shift;
  95. my $template_file=$pagesources{$template_page};
  96. if (! defined $template_file) {
  97. return;
  98. }
  99. my $template;
  100. eval {
  101. $template=HTML::Template->new(
  102. filter => sub {
  103. my $text_ref = shift;
  104. $$text_ref=&Encode::decode_utf8($$text_ref);
  105. chomp $$text_ref;
  106. },
  107. filename => srcfile($template_file),
  108. die_on_bad_params => 0,
  109. no_includes => 1,
  110. );
  111. };
  112. if ($@) {
  113. # Indicate that the earlier preprocessor directive set
  114. # up a template that doesn't work.
  115. return "[[!pagetemplate ".gettext("failed to process")." $@]]";
  116. }
  117. $template->param(name => $page);
  118. return $template->output;
  119. }
  120. 1