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