summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/template.pm
blob: db26bfe315a254ddc8bdafef07abf83c4a819deb (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 gettext("failed to process template:")." $@";
  38. }
  39. if (! $template) {
  40. error sprintf(gettext("%s not found"),
  41. htmllink($params{page}, $params{destpage},
  42. "/templates/$params{id}"))
  43. }
  44. $params{basename}=IkiWiki::basename($params{page});
  45. foreach my $param (keys %params) {
  46. my $value=IkiWiki::preprocess($params{page}, $params{destpage},
  47. $params{$param}, $scan);
  48. if ($template->query(name => $param)) {
  49. my $htmlvalue=IkiWiki::htmlize($params{page}, $params{destpage},
  50. pagetype($pagesources{$params{page}}),
  51. $value);
  52. chomp $htmlvalue;
  53. $template->param($param => $htmlvalue);
  54. }
  55. if ($template->query(name => "raw_$param")) {
  56. chomp $value;
  57. $template->param("raw_$param" => $value);
  58. }
  59. }
  60. return IkiWiki::preprocess($params{page}, $params{destpage},
  61. $template->output, $scan);
  62. }
  63. 1