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