summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/meta.pm
blob: 5bcd658378f37c630fe4b389f7bf25dfc95faeb7 (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. hook(type => "preprocess", id => "meta", call => \&preprocess);
  14. hook(type => "filter", id => "meta", call => \&filter);
  15. hook(type => "pagetemplate", id => "meta", 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 @{$links{$page}}, $value;
  46. }
  47. }
  48. elsif ($key eq 'title') {
  49. $title{$page}=$value;
  50. }
  51. elsif ($key eq 'permalink') {
  52. $permalink{$page}=$value;
  53. $meta{$page}.="<link rel=\"bookmark\" href=\"".encode_entities($value)."\" />\n";
  54. }
  55. else {
  56. $meta{$page}.="<meta name=\"".encode_entities($key).
  57. "\" content=\"".encode_entities($value)."\" />\n";
  58. if ($key eq 'author') {
  59. $author{$page}=$value;
  60. }
  61. elsif ($key eq 'authorurl') {
  62. $authorurl{$page}=$value;
  63. }
  64. }
  65. return "";
  66. } # }}}
  67. sub pagetemplate (@) { #{{{
  68. my %params=@_;
  69. my $page=$params{page};
  70. my $template=$params{template};
  71. $template->param(meta => $meta{$page})
  72. if exists $meta{$page} && $template->query(name => "meta");
  73. if (exists $title{$page} && $template->query(name => "title")) {
  74. $template->param(title => $title{$page});
  75. $template->param(title_overridden => 1);
  76. }
  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