summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/meta.pm
blob: d624757baead4e1f63c3ee7226d502bca4aeeec3 (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. delete $params{preview};
  34. eval q{use HTML::Entities};
  35. # Always dencode, even if encoding later, since it might not be
  36. # fully encoded.
  37. $value=decode_entities($value);
  38. if ($key eq 'link') {
  39. if (%params) {
  40. $meta{$page}.="<link href=\"".encode_entities($value)."\" ".
  41. join(" ", map { encode_entities($_)."=\"".encode_entities(decode_entities($params{$_}))."\"" } keys %params).
  42. " />\n";
  43. }
  44. else {
  45. # hidden WikiLink
  46. push @{$links{$page}}, $value;
  47. }
  48. }
  49. elsif ($key eq 'title') {
  50. $title{$page}=$value;
  51. }
  52. elsif ($key eq 'permalink') {
  53. $permalink{$page}=$value;
  54. $meta{$page}.="<link rel=\"bookmark\" href=\"".encode_entities($value)."\" />\n";
  55. }
  56. elsif ($key eq 'date') {
  57. eval q{use Date::Parse};
  58. if (! $@) {
  59. my $time = str2time($value);
  60. $IkiWiki::pagectime{$page}=$time if defined $time;
  61. }
  62. }
  63. else {
  64. $meta{$page}.="<meta name=\"".encode_entities($key).
  65. "\" content=\"".encode_entities($value)."\" />\n";
  66. if ($key eq 'author') {
  67. $author{$page}=$value;
  68. }
  69. elsif ($key eq 'authorurl') {
  70. $authorurl{$page}=$value;
  71. }
  72. }
  73. return "";
  74. } # }}}
  75. sub pagetemplate (@) { #{{{
  76. my %params=@_;
  77. my $page=$params{page};
  78. my $template=$params{template};
  79. $template->param(meta => $meta{$page})
  80. if exists $meta{$page} && $template->query(name => "meta");
  81. if (exists $title{$page} && $template->query(name => "title")) {
  82. $template->param(title => $title{$page});
  83. $template->param(title_overridden => 1);
  84. }
  85. $template->param(permalink => $permalink{$page})
  86. if exists $permalink{$page} && $template->query(name => "permalink");
  87. $template->param(author => $author{$page})
  88. if exists $author{$page} && $template->query(name => "author");
  89. $template->param(authorurl => $authorurl{$page})
  90. if exists $authorurl{$page} && $template->query(name => "authorurl");
  91. } # }}}
  92. 1