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