summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/template.pm
blob: a2859b2a99864ddb0e9d33cea8106b46ffbc8775 (plain)
  1. #!/usr/bin/perl
  2. # Structured template plugin.
  3. package IkiWiki::Plugin::template;
  4. use warnings;
  5. use strict;
  6. use IkiWiki 2.00;
  7. use HTML::Template;
  8. use Encode;
  9. sub import { #{{{
  10. hook(type => "getsetup", id => "template", call => \&getsetup);
  11. hook(type => "preprocess", id => "template", call => \&preprocess);
  12. } # }}}
  13. sub getsetup () { #{{{
  14. return
  15. plugin => {
  16. safe => 1,
  17. rebuild => undef,
  18. },
  19. } #}}}
  20. sub preprocess (@) { #{{{
  21. my %params=@_;
  22. if (! exists $params{id}) {
  23. error gettext("missing id parameter")
  24. }
  25. my $template_page="templates/$params{id}";
  26. add_depends($params{page}, $template_page);
  27. my $template_file=$pagesources{$template_page};
  28. return sprintf(gettext("template %s not found"),
  29. htmllink($params{page}, $params{destpage}, $template_page))
  30. unless defined $template_file;
  31. my $template;
  32. eval {
  33. $template=HTML::Template->new(
  34. filter => sub {
  35. my $text_ref = shift;
  36. $$text_ref=&Encode::decode_utf8($$text_ref);
  37. chomp $$text_ref;
  38. },
  39. filename => srcfile($template_file),
  40. die_on_bad_params => 0,
  41. no_includes => 1,
  42. blind_cache => 1,
  43. );
  44. };
  45. if ($@) {
  46. error gettext("failed to process:")." $@"
  47. }
  48. $params{basename}=IkiWiki::basename($params{page});
  49. foreach my $param (keys %params) {
  50. if ($template->query(name => $param)) {
  51. $template->param($param =>
  52. IkiWiki::htmlize($params{page}, $params{destpage},
  53. pagetype($pagesources{$params{page}}),
  54. $params{$param}));
  55. }
  56. if ($template->query(name => "raw_$param")) {
  57. $template->param("raw_$param" => $params{$param});
  58. }
  59. }
  60. return IkiWiki::preprocess($params{page}, $params{destpage},
  61. IkiWiki::filter($params{page}, $params{destpage},
  62. $template->output));
  63. } # }}}
  64. 1