summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/meta.pm
blob: 9b026fffa3c3c57480643bb424834abb6e50efbc (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. $meta{$page}.="<link rel=\"bookmark\" href=\"".encode_entities($value)."\" />\n";
  57. }
  58. else {
  59. $meta{$page}.="<meta name=\"".encode_entities($key).
  60. "\" content=\"".encode_entities($value)."\" />\n";
  61. if ($key eq 'author') {
  62. $author{$page}=$value;
  63. }
  64. elsif ($key eq 'authorurl') {
  65. $authorurl{$page}=$value;
  66. }
  67. }
  68. return "";
  69. } # }}}
  70. sub pagetemplate (@) { #{{{
  71. my %params=@_;
  72. my $page=$params{page};
  73. my $template=$params{template};
  74. $template->param(meta => $meta{$page})
  75. if exists $meta{$page} && $template->query(name => "meta");
  76. if (exists $title{$page} && $template->query(name => "title")) {
  77. $template->param(title => $title{$page});
  78. $template->param(title_overridden => 1);
  79. }
  80. $template->param(permalink => $permalink{$page})
  81. if exists $permalink{$page} && $template->query(name => "permalink");
  82. $template->param(author => $author{$page})
  83. if exists $author{$page} && $template->query(name => "author");
  84. $template->param(authorurl => $authorurl{$page})
  85. if exists $authorurl{$page} && $template->query(name => "authorurl");
  86. } # }}}
  87. 1