summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/template.pm
blob: b8c2f05b29ab4492453a91b16dff76f8d1521228 (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. IkiWiki::filter($params{page}, $params{destpage},
  48. $params{$param}), $scan);
  49. if ($template->query(name => $param)) {
  50. my $htmlvalue=IkiWiki::htmlize($params{page}, $params{destpage},
  51. pagetype($pagesources{$params{page}}),
  52. $value);
  53. chomp $htmlvalue;
  54. $template->param($param => $htmlvalue);
  55. }
  56. if ($template->query(name => "raw_$param")) {
  57. chomp $value;
  58. $template->param("raw_$param" => $value);
  59. }
  60. }
  61. return IkiWiki::preprocess($params{page}, $params{destpage},
  62. IkiWiki::filter($params{page}, $params{destpage},
  63. $template->output), $scan);
  64. }
  65. 1