summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/template.pm
blob: 3df06e652eeee285d51690b9d2178d6e0fb2b8ca (plain)
  1. #!/usr/bin/perl
  2. # Structured template plugin.
  3. package IkiWiki::Plugin::template;
  4. use warnings;
  5. use strict;
  6. use IkiWiki 3.00;
  7. use Encode;
  8. sub import {
  9. hook(type => "getsetup", id => "template", call => \&getsetup);
  10. hook(type => "preprocess", id => "template", call => \&preprocess,
  11. scan => 1);
  12. }
  13. sub getsetup () {
  14. return
  15. plugin => {
  16. safe => 1,
  17. rebuild => undef,
  18. section => "widget",
  19. },
  20. }
  21. sub preprocess (@) {
  22. my %params=@_;
  23. # This needs to run even in scan mode, in order to process
  24. # links and other metadata included via the template.
  25. my $scan=! defined wantarray;
  26. if (! exists $params{id}) {
  27. error gettext("missing id parameter")
  28. }
  29. # The bare id is used, so a page templates/$id can be used as
  30. # the template.
  31. my $template;
  32. eval {
  33. $template=template_depends($params{id}, $params{page},
  34. blind_cache => 1);
  35. };
  36. if ($@) {
  37. error sprintf(gettext("failed to process template %s"),
  38. htmllink($params{page}, $params{destpage},
  39. "/templates/$params{id}"))." $@";
  40. }
  41. $params{basename}=IkiWiki::basename($params{page});
  42. foreach my $param (keys %params) {
  43. my $value=IkiWiki::preprocess($params{page}, $params{destpage},
  44. $params{$param}, $scan);
  45. if ($template->query(name => $param)) {
  46. my $htmlvalue=IkiWiki::htmlize($params{page}, $params{destpage},
  47. pagetype($pagesources{$params{page}}),
  48. $value);
  49. chomp $htmlvalue;
  50. $template->param($param => $htmlvalue);
  51. }
  52. if ($template->query(name => "raw_$param")) {
  53. chomp $value;
  54. $template->param("raw_$param" => $value);
  55. }
  56. }
  57. return IkiWiki::preprocess($params{page}, $params{destpage},
  58. $template->output, $scan);
  59. }
  60. 1