summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/edittemplate.pm
blob: 189a066d83040a744be6a58efec6d626d140659b (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::edittemplate;
  3. use warnings;
  4. use strict;
  5. use IkiWiki 2.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. hook(type => "refresh", id => "edittemplate",
  18. call => \&refresh);
  19. } #}}}
  20. sub getsetup () { #{{{
  21. return
  22. plugin => {
  23. safe => 1,
  24. rebuild => undef,
  25. },
  26. } #}}}
  27. sub needsbuild (@) { #{{{
  28. my $needsbuild=shift;
  29. foreach my $page (keys %pagestate) {
  30. if (exists $pagestate{$page}{edittemplate}) {
  31. if (exists $pagesources{$page} &&
  32. grep { $_ eq $pagesources{$page} } @$needsbuild) {
  33. # remove state, it will be re-added
  34. # if the preprocessor directive is still
  35. # there during the rebuild
  36. delete $pagestate{$page}{edittemplate};
  37. }
  38. }
  39. }
  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. $pagestate{$params{page}}{edittemplate}{$params{match}}=$link;
  52. return "" if ($params{silent} && IkiWiki::yesno($params{silent}));
  53. add_depends($params{page}, $link);
  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. push @page_locs, $field->options;
  74. }
  75. }
  76. foreach my $p (@page_locs) {
  77. foreach my $registering_page (keys %pagestate) {
  78. if (exists $pagestate{$registering_page}{edittemplate}) {
  79. foreach my $pagespec (sort keys %{$pagestate{$registering_page}{edittemplate}}) {
  80. if (pagespec_match($p, $pagespec, location => $registering_page)) {
  81. my $template=$pagestate{$registering_page}{edittemplate}{$pagespec};
  82. $form->field(name => "editcontent",
  83. value => filltemplate($template, $page));
  84. $form->field(name => "type",
  85. value => pagetype($pagesources{$template}))
  86. if $pagesources{$template};
  87. return;
  88. }
  89. }
  90. }
  91. }
  92. }
  93. } #}}}
  94. sub filltemplate ($$) { #{{{
  95. my $template_page=shift;
  96. my $page=shift;
  97. my $template_file=$pagesources{$template_page};
  98. if (! defined $template_file) {
  99. return;
  100. }
  101. my $template;
  102. eval {
  103. $template=HTML::Template->new(
  104. filter => sub {
  105. my $text_ref = shift;
  106. $$text_ref=&Encode::decode_utf8($$text_ref);
  107. chomp $$text_ref;
  108. },
  109. filename => srcfile($template_file),
  110. die_on_bad_params => 0,
  111. no_includes => 1,
  112. );
  113. };
  114. if ($@) {
  115. # Indicate that the earlier preprocessor directive set
  116. # up a template that doesn't work.
  117. return "[[!pagetemplate ".gettext("failed to process")." $@]]";
  118. }
  119. $template->param(name => $page);
  120. return $template->output;
  121. } #}}}
  122. sub refresh () {
  123. }
  124. 1