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