summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/meta.pm
blob: 5867329afd74ed0c2b94df12caad61d6d19a50ab (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, scan => 1);
  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. elsif ($key eq 'date') {
  56. eval q{use Date::Parse};
  57. if (! $@) {
  58. my $time = str2time($value);
  59. $IkiWiki::pagectime{$page}=$time if defined $time;
  60. }
  61. }
  62. else {
  63. $meta{$page}.="<meta name=\"".encode_entities($key).
  64. "\" content=\"".encode_entities($value)."\" />\n";
  65. if ($key eq 'author') {
  66. $author{$page}=$value;
  67. }
  68. elsif ($key eq 'authorurl') {
  69. $authorurl{$page}=$value;
  70. }
  71. }
  72. return "";
  73. } # }}}
  74. sub pagetemplate (@) { #{{{
  75. my %params=@_;
  76. my $page=$params{page};
  77. my $template=$params{template};
  78. $template->param(meta => $meta{$page})
  79. if exists $meta{$page} && $template->query(name => "meta");
  80. if (exists $title{$page} && $template->query(name => "title")) {
  81. $template->param(title => $title{$page});
  82. $template->param(title_overridden => 1);
  83. }
  84. $template->param(permalink => $permalink{$page})
  85. if exists $permalink{$page} && $template->query(name => "permalink");
  86. $template->param(author => $author{$page})
  87. if exists $author{$page} && $template->query(name => "author");
  88. $template->param(authorurl => $authorurl{$page})
  89. if exists $authorurl{$page} && $template->query(name => "authorurl");
  90. } # }}}
  91. 1