summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/meta.pm
blob: bac16346932d2a00e1bc034d500a1fe8a08abebb (plain)
  1. #!/usr/bin/perl
  2. # Ikiwiki metadata plugin.
  3. package IkiWiki::Plugin::meta;
  4. use warnings;
  5. use strict;
  6. use IkiWiki;
  7. my %meta;
  8. my %title;
  9. sub import { #{{{
  10. IkiWiki::hook(type => "preprocess", id => "meta",
  11. call => \&preprocess);
  12. IkiWiki::hook(type => "filter", id => "meta",
  13. call => \&filter);
  14. IkiWiki::hook(type => "pagetemplate", id => "meta",
  15. call => \&pagetemplate);
  16. } # }}}
  17. sub filter (@) { #{{{
  18. my %params=@_;
  19. $meta{$params{page}}='';
  20. return $params{content};
  21. } # }}}
  22. sub preprocess (@) { #{{{
  23. if (! @_) {
  24. return "";
  25. }
  26. my %params=@_;
  27. my $key=shift;
  28. my $value=$params{$key};
  29. delete $params{$key};
  30. my $page=$params{page};
  31. delete $params{page};
  32. delete $params{destpage};
  33. eval q{use HTML::Entities};
  34. # Always dencode, even if encoding later, since it might not be
  35. # fully encoded.
  36. $value=decode_entities($value);
  37. if ($key eq 'link') {
  38. if (%params) {
  39. $meta{$page}.="<link href=\"".encode_entities($value)."\" ".
  40. join(" ", map { encode_entities($_)."=\"".encode_entities(decode_entities($params{$_}))."\"" } keys %params).
  41. " />\n";
  42. }
  43. else {
  44. # hidden WikiLink
  45. push @{$IkiWiki::links{$page}}, $value;
  46. }
  47. }
  48. elsif ($key eq 'title') {
  49. $title{$page}=$value;
  50. }
  51. else {
  52. $meta{$page}.="<meta name=\"".encode_entities($key).
  53. "\" content=\"".encode_entities($value)."\" />\n";
  54. }
  55. return "";
  56. } # }}}
  57. sub pagetemplate (@) { #{{{
  58. my %params=@_;
  59. my $page=$params{page};
  60. my $template=$params{template};
  61. $template->param(meta => $meta{$page})
  62. if exists $meta{$page} && $template->query(name => "meta");
  63. $template->param(title => $title{$page})
  64. if exists $title{$page} && $template->query(name => "title");
  65. } # }}}
  66. 1