summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/meta.pm
blob: 938a28e59f83851f53e8d68c2776c2ee37e115f9 (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. my %license;
  13. my %copyright;
  14. sub import { #{{{
  15. hook(type => "preprocess", id => "meta", call => \&preprocess, scan => 1);
  16. hook(type => "filter", id => "meta", call => \&filter);
  17. hook(type => "pagetemplate", id => "meta", call => \&pagetemplate);
  18. } # }}}
  19. sub filter (@) { #{{{
  20. my %params=@_;
  21. $meta{$params{page}}='';
  22. return $params{content};
  23. } # }}}
  24. sub scrub ($) { #{{{
  25. if (IkiWiki::Plugin::htmlscrubber->can("sanitize")) {
  26. return IkiWiki::Plugin::htmlscrubber::sanitize(content => shift);
  27. }
  28. else {
  29. return shift;
  30. }
  31. } #}}}
  32. sub preprocess (@) { #{{{
  33. if (! @_) {
  34. return "";
  35. }
  36. my %params=@_;
  37. my $key=shift;
  38. my $value=$params{$key};
  39. delete $params{$key};
  40. my $page=$params{page};
  41. delete $params{page};
  42. delete $params{destpage};
  43. delete $params{preview};
  44. eval q{use HTML::Entities};
  45. # Always dencode, even if encoding later, since it might not be
  46. # fully encoded.
  47. $value=decode_entities($value);
  48. if ($key eq 'link') {
  49. if (%params) {
  50. $meta{$page}.=scrub("<link href=\"".encode_entities($value)."\" ".
  51. join(" ", map { encode_entities($_)."=\"".encode_entities(decode_entities($params{$_}))."\"" } keys %params).
  52. " />\n");
  53. }
  54. else {
  55. # hidden WikiLink
  56. push @{$links{$page}}, $value;
  57. }
  58. }
  59. elsif ($key eq 'title') {
  60. $title{$page}=HTML::Entities::encode_numeric($value);
  61. }
  62. elsif ($key eq 'permalink') {
  63. $permalink{$page}=$value;
  64. $meta{$page}.=scrub("<link rel=\"bookmark\" href=\"".encode_entities($value)."\" />\n");
  65. }
  66. elsif ($key eq 'date') {
  67. eval q{use Date::Parse};
  68. if (! $@) {
  69. my $time = str2time($value);
  70. $IkiWiki::pagectime{$page}=$time if defined $time;
  71. }
  72. }
  73. elsif ($key eq 'stylesheet') {
  74. my $rel=exists $params{rel} ? $params{rel} : "alternate stylesheet";
  75. my $title=exists $params{title} ? $params{title} : $value;
  76. # adding .css to the value prevents using any old web
  77. # editable page as a stylesheet
  78. my $stylesheet=bestlink($page, $value.".css");
  79. if (! length $stylesheet) {
  80. return "[[meta ".gettext("stylesheet not found")."]]";
  81. }
  82. $meta{$page}.='<link href="'.urlto($stylesheet, $page).
  83. '" rel="'.encode_entities($rel).
  84. '" title="'.encode_entities($title).
  85. "\" type=\"text/css\" />\n";
  86. }
  87. elsif ($key eq 'openid') {
  88. if (exists $params{server}) {
  89. $meta{$page}.='<link href="'.encode_entities($params{server}).
  90. "\" rel=\"openid.server\" />\n";
  91. }
  92. $meta{$page}.='<link href="'.encode_entities($value).
  93. "\" rel=\"openid.delegate\" />\n";
  94. }
  95. elsif ($key eq 'license') {
  96. $meta{$page}.="<link rel=\"license\" href=\"#page_license\" />\n";
  97. $license{$page}=$value;
  98. }
  99. elsif ($key eq 'copyright') {
  100. $meta{$page}.="<link rel=\"copyright\" href=\"#page_copyright\" />\n";
  101. $copyright{$page}=$value;
  102. }
  103. else {
  104. $meta{$page}.=scrub("<meta name=\"".encode_entities($key).
  105. "\" content=\"".encode_entities($value)."\" />\n");
  106. if ($key eq 'author') {
  107. $author{$page}=$value;
  108. }
  109. elsif ($key eq 'authorurl') {
  110. $authorurl{$page}=$value;
  111. }
  112. }
  113. return "";
  114. } # }}}
  115. sub pagetemplate (@) { #{{{
  116. my %params=@_;
  117. my $page=$params{page};
  118. my $destpage=$params{destpage};
  119. my $template=$params{template};
  120. $template->param(meta => $meta{$page})
  121. if exists $meta{$page} && $template->query(name => "meta");
  122. if (exists $title{$page} && $template->query(name => "title")) {
  123. $template->param(title => $title{$page});
  124. $template->param(title_overridden => 1);
  125. }
  126. $template->param(permalink => $permalink{$page})
  127. if exists $permalink{$page} && $template->query(name => "permalink");
  128. $template->param(author => $author{$page})
  129. if exists $author{$page} && $template->query(name => "author");
  130. $template->param(authorurl => $authorurl{$page})
  131. if exists $authorurl{$page} && $template->query(name => "authorurl");
  132. if ($page ne $destpage &&
  133. ((exists $license{$page} && ! exists $license{$destpage}) ||
  134. (exists $copyright{$page} && ! exists $copyright{$destpage}))) {
  135. # Force a scan of the destpage to get its copyright/license
  136. # info. If the info is declared after an inline, it will
  137. # otherwise not be available at this point.
  138. IkiWiki::scan($pagesources{$destpage});
  139. }
  140. if (exists $license{$page} && $template->query(name => "license") &&
  141. ($page eq $destpage || ! exists $license{$destpage} ||
  142. $license{$page} ne $license{$destpage})) {
  143. $template->param(license => IkiWiki::linkify($page, $destpage, $license{$page}));
  144. }
  145. if (exists $copyright{$page} && $template->query(name => "copyright") &&
  146. ($page eq $destpage || ! exists $copyright{$destpage} ||
  147. $copyright{$page} ne $copyright{$destpage})) {
  148. $template->param(copyright => IkiWiki::linkify($page, $destpage, $copyright{$page}));
  149. }
  150. } # }}}
  151. 1