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