summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/edittemplate.pm
blob: d6507201080da9915cf318d2bc63f811b4fac79a (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;
  100. eval {
  101. # force page name absolute so it doesn't look in templates/
  102. $template=template("/".$template_page);
  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 template:")." $@]]";
  108. }
  109. if (! defined $template) {
  110. return;
  111. }
  112. $template->param(name => $page);
  113. return $template->output;
  114. }
  115. 1