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