summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/template.pm
blob: c33dbbb83b8d1623ffd1348667247ec3be04f082 (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 => "preprocess", id => "template", call => \&preprocess);
  11. } # }}}
  12. sub preprocess (@) { #{{{
  13. my %params=@_;
  14. if (! exists $params{id}) {
  15. error gettext("missing id parameter")
  16. }
  17. my $template_page="templates/$params{id}";
  18. add_depends($params{page}, $template_page);
  19. my $template_file=$pagesources{$template_page};
  20. return sprintf(gettext("template %s not found"),
  21. htmllink($params{page}, $params{destpage}, $template_page))
  22. unless defined $template_file;
  23. my $template;
  24. eval {
  25. $template=HTML::Template->new(
  26. filter => sub {
  27. my $text_ref = shift;
  28. $$text_ref=&Encode::decode_utf8($$text_ref);
  29. chomp $$text_ref;
  30. },
  31. filename => srcfile($template_file),
  32. die_on_bad_params => 0,
  33. no_includes => 1,
  34. blind_cache => 1,
  35. );
  36. };
  37. if ($@) {
  38. error gettext("failed to process:")." $@"
  39. }
  40. $params{basename}=IkiWiki::basename($params{page});
  41. foreach my $param (keys %params) {
  42. if ($template->query(name => $param)) {
  43. $template->param($param =>
  44. IkiWiki::htmlize($params{page}, $params{destpage},
  45. pagetype($pagesources{$params{page}}),
  46. $params{$param}));
  47. }
  48. if ($template->query(name => "raw_$param")) {
  49. $template->param("raw_$param" => $params{$param});
  50. }
  51. }
  52. return IkiWiki::preprocess($params{page}, $params{destpage},
  53. IkiWiki::filter($params{page}, $params{destpage},
  54. $template->output));
  55. } # }}}
  56. 1