summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/meta.pm
blob: fd21432fad09d790c6478b34b6004e0b1b96ff15 (plain)
  1. #!/usr/bin/perl
  2. # Ikiwiki metadata plugin.
  3. package IkiWiki::Plugin::meta;
  4. use warnings;
  5. use strict;
  6. use IkiWiki 2.00;
  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 scrub ($) { #{{{
  23. if (IkiWiki::Plugin::htmlscrubber->can("sanitize")) {
  24. return IkiWiki::Plugin::htmlscrubber::sanitize(content => shift);
  25. }
  26. else {
  27. return shift;
  28. }
  29. } #}}}
  30. sub preprocess (@) { #{{{
  31. if (! @_) {
  32. return "";
  33. }
  34. my %params=@_;
  35. my $key=shift;
  36. my $value=$params{$key};
  37. delete $params{$key};
  38. my $page=$params{page};
  39. delete $params{page};
  40. delete $params{destpage};
  41. delete $params{preview};
  42. eval q{use HTML::Entities};
  43. # Always dencode, even if encoding later, since it might not be
  44. # fully encoded.
  45. $value=decode_entities($value);
  46. if ($key eq 'link') {
  47. if (%params) {
  48. $meta{$page}.=scrub("<link href=\"".encode_entities($value)."\" ".
  49. join(" ", map { encode_entities($_)."=\"".encode_entities(decode_entities($params{$_}))."\"" } keys %params).
  50. " />\n");
  51. }
  52. else {
  53. # hidden WikiLink
  54. push @{$links{$page}}, $value;
  55. }
  56. }
  57. elsif ($key eq 'title') {
  58. $title{$page}=HTML::Entities::encode_numeric($value);
  59. }
  60. elsif ($key eq 'permalink') {
  61. $permalink{$page}=$value;
  62. $meta{$page}.=scrub("<link rel=\"bookmark\" href=\"".encode_entities($value)."\" />\n");
  63. }
  64. elsif ($key eq 'date') {
  65. eval q{use Date::Parse};
  66. if (! $@) {
  67. my $time = str2time($value);
  68. $IkiWiki::pagectime{$page}=$time if defined $time;
  69. }
  70. }
  71. elsif ($key eq 'stylesheet') {
  72. my $rel=exists $params{rel} ? $params{rel} : "alternate stylesheet";
  73. my $title=exists $params{title} ? $params{title} : $value;
  74. # adding .css to the value prevents using any old web
  75. # editable page as a stylesheet
  76. my $stylesheet=bestlink($page, $value.".css");
  77. if (! length $stylesheet) {
  78. return "[[meta ".gettext("stylesheet not found")."]]";
  79. }
  80. $meta{$page}.='<link href="'.urlto($stylesheet, $page).
  81. '" rel="'.encode_entities($rel).
  82. '" title="'.encode_entities($title).
  83. "\" type=\"text/css\" />\n";
  84. }
  85. elsif ($key eq 'openid') {
  86. if (exists $params{server}) {
  87. $meta{$page}.='<link href="'.encode_entities($params{server}).
  88. "\" rel=\"openid.server\" />\n";
  89. }
  90. $meta{$page}.='<link href="'.encode_entities($value).
  91. "\" rel=\"openid.delegate\" />\n";
  92. }
  93. else {
  94. $meta{$page}.=scrub("<meta name=\"".encode_entities($key).
  95. "\" content=\"".encode_entities($value)."\" />\n");
  96. if ($key eq 'author') {
  97. $author{$page}=$value;
  98. }
  99. elsif ($key eq 'authorurl') {
  100. $authorurl{$page}=$value;
  101. }
  102. }
  103. return "";
  104. } # }}}
  105. sub pagetemplate (@) { #{{{
  106. my %params=@_;
  107. my $page=$params{page};
  108. my $template=$params{template};
  109. $template->param(meta => $meta{$page})
  110. if exists $meta{$page} && $template->query(name => "meta");
  111. if (exists $title{$page} && $template->query(name => "title")) {
  112. $template->param(title => $title{$page});
  113. $template->param(title_overridden => 1);
  114. }
  115. $template->param(permalink => $permalink{$page})
  116. if exists $permalink{$page} && $template->query(name => "permalink");
  117. $template->param(author => $author{$page})
  118. if exists $author{$page} && $template->query(name => "author");
  119. $template->param(authorurl => $authorurl{$page})
  120. if exists $authorurl{$page} && $template->query(name => "authorurl");
  121. } # }}}
  122. 1