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