summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/meta.pm
blob: d2c6e7f8bfa404739498940050bda916b54d3209 (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 => "needsbuild", id => "meta", call => \&needsbuild);
  16. hook(type => "preprocess", id => "meta", call => \&preprocess, scan => 1);
  17. hook(type => "pagetemplate", id => "meta", call => \&pagetemplate);
  18. } # }}}
  19. sub needsbuild (@) { #{{{
  20. my $needsbuild=shift;
  21. foreach my $page (keys %pagestate) {
  22. if (exists $pagestate{$page}{meta}) {
  23. if (grep { $_ eq $pagesources{$page} } @$needsbuild) {
  24. # remove state, it will be re-added
  25. # if the preprocessor directive is still
  26. # there during the rebuild
  27. delete $pagestate{$page}{meta};
  28. }
  29. }
  30. }
  31. }
  32. sub scrub ($) { #{{{
  33. if (IkiWiki::Plugin::htmlscrubber->can("sanitize")) {
  34. return IkiWiki::Plugin::htmlscrubber::sanitize(content => shift);
  35. }
  36. else {
  37. return shift;
  38. }
  39. } #}}}
  40. sub htmlize ($$$) { #{{{
  41. my $page = shift;
  42. my $destpage = shift;
  43. return IkiWiki::htmlize($page, pagetype($pagesources{$page}),
  44. IkiWiki::linkify($page, $destpage,
  45. IkiWiki::preprocess($page, $destpage, shift)));
  46. }
  47. sub preprocess (@) { #{{{
  48. return "" unless @_;
  49. my %params=@_;
  50. my $key=shift;
  51. my $value=$params{$key};
  52. delete $params{$key};
  53. my $page=$params{page};
  54. delete $params{page};
  55. my $destpage=$params{destpage};
  56. delete $params{destpage};
  57. delete $params{preview};
  58. eval q{use HTML::Entities};
  59. # Always decode, even if encoding later, since it might not be
  60. # fully encoded.
  61. $value=decode_entities($value);
  62. # Metadata collection that needs to happen during the scan pass.
  63. if ($key eq 'title') {
  64. $title{$page}=HTML::Entities::encode_numeric($value);
  65. }
  66. elsif ($key eq 'license') {
  67. push @{$meta{$page}}, '<link rel="license" href="#page_license" />';
  68. $license{$page}=$value;
  69. return "";
  70. }
  71. elsif ($key eq 'copyright') {
  72. push @{$meta{$page}}, '<link rel="copyright" href="#page_copyright" />';
  73. $copyright{$page}=$value;
  74. return "";
  75. }
  76. elsif ($key eq 'link' && ! %params) {
  77. # hidden WikiLink
  78. push @{$links{$page}}, $value;
  79. return "";
  80. }
  81. elsif ($key eq 'author') {
  82. $author{$page}=$value;
  83. # fallthorough
  84. }
  85. elsif ($key eq 'authorurl') {
  86. $authorurl{$page}=$value;
  87. # fallthrough
  88. }
  89. if (! defined wantarray) {
  90. # avoid collecting duplicate data during scan pass
  91. return;
  92. }
  93. # Metadata collection that happens only during preprocessing pass.
  94. if ($key eq 'date') {
  95. eval q{use Date::Parse};
  96. if (! $@) {
  97. my $time = str2time($value);
  98. $IkiWiki::pagectime{$page}=$time if defined $time;
  99. }
  100. }
  101. elsif ($key eq 'permalink') {
  102. $permalink{$page}=$value;
  103. push @{$meta{$page}}, scrub('<link rel="bookmark" href="'.encode_entities($value).'" />');
  104. }
  105. elsif ($key eq 'stylesheet') {
  106. my $rel=exists $params{rel} ? $params{rel} : "alternate stylesheet";
  107. my $title=exists $params{title} ? $params{title} : $value;
  108. # adding .css to the value prevents using any old web
  109. # editable page as a stylesheet
  110. my $stylesheet=bestlink($page, $value.".css");
  111. if (! length $stylesheet) {
  112. return "[[meta ".gettext("stylesheet not found")."]]";
  113. }
  114. push @{$meta{$page}}, '<link href="'.urlto($stylesheet, $page).
  115. '" rel="'.encode_entities($rel).
  116. '" title="'.encode_entities($title).
  117. "\" type=\"text/css\" />";
  118. }
  119. elsif ($key eq 'openid') {
  120. if (exists $params{server}) {
  121. push @{$meta{$page}}, '<link href="'.encode_entities($params{server}).
  122. '" rel="openid.server" />';
  123. }
  124. push @{$meta{$page}}, '<link href="'.encode_entities($value).
  125. '" rel="openid.delegate" />';
  126. }
  127. elsif ($key eq 'redir') {
  128. return "" if $page ne $destpage;
  129. my $safe=0;
  130. if ($value !~ /^\w+:\/\//) {
  131. my ($redir_page, $redir_anchor) = split /\#/, $value;
  132. add_depends($page, $redir_page);
  133. my $link=bestlink($page, $redir_page);
  134. if (! length $link) {
  135. return "[[meta ".gettext("redir page not found")."]]";
  136. }
  137. $value=urlto($link, $page);
  138. $value.='#'.$redir_anchor if defined $redir_anchor;
  139. $safe=1;
  140. # redir cycle detection
  141. $pagestate{$page}{meta}{redir}=$link;
  142. my $at=$page;
  143. my %seen;
  144. while (exists $pagestate{$at}{meta}{redir}) {
  145. if ($seen{$at}) {
  146. return "[[meta ".gettext("redir cycle is not allowed")."]]";
  147. }
  148. $seen{$at}=1;
  149. $at=$pagestate{$at}{meta}{redir};
  150. }
  151. }
  152. else {
  153. $value=encode_entities($value);
  154. }
  155. my $delay=int(exists $params{delay} ? $params{delay} : 0);
  156. my $redir="<meta http-equiv=\"refresh\" content=\"$delay; URL=$value\">";
  157. if (! $safe) {
  158. $redir=scrub($redir);
  159. }
  160. push @{$meta{$page}}, $redir;
  161. }
  162. elsif ($key eq 'link') {
  163. if (%params) {
  164. push @{$meta{$page}}, scrub("<link href=\"".encode_entities($value)."\" ".
  165. join(" ", map {
  166. encode_entities($_)."=\"".encode_entities(decode_entities($params{$_}))."\""
  167. } keys %params).
  168. " />\n");
  169. }
  170. }
  171. else {
  172. push @{$meta{$page}}, scrub('<meta name="'.encode_entities($key).
  173. '" content="'.encode_entities($value).'" />');
  174. }
  175. return "";
  176. } # }}}
  177. sub pagetemplate (@) { #{{{
  178. my %params=@_;
  179. my $page=$params{page};
  180. my $destpage=$params{destpage};
  181. my $template=$params{template};
  182. if (exists $meta{$page} && $template->query(name => "meta")) {
  183. # avoid duplicate meta lines
  184. my %seen;
  185. $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$meta{$page}}));
  186. }
  187. if (exists $title{$page} && $template->query(name => "title")) {
  188. $template->param(title => $title{$page});
  189. $template->param(title_overridden => 1);
  190. }
  191. $template->param(permalink => $permalink{$page})
  192. if exists $permalink{$page} && $template->query(name => "permalink");
  193. $template->param(author => $author{$page})
  194. if exists $author{$page} && $template->query(name => "author");
  195. $template->param(authorurl => $authorurl{$page})
  196. if exists $authorurl{$page} && $template->query(name => "authorurl");
  197. if (exists $license{$page} && $template->query(name => "license") &&
  198. ($page eq $destpage || ! exists $license{$destpage} ||
  199. $license{$page} ne $license{$destpage})) {
  200. $template->param(license => htmlize($page, $destpage, $license{$page}));
  201. }
  202. if (exists $copyright{$page} && $template->query(name => "copyright") &&
  203. ($page eq $destpage || ! exists $copyright{$destpage} ||
  204. $copyright{$page} ne $copyright{$destpage})) {
  205. $template->param(copyright => htmlize($page, $destpage, $copyright{$page}));
  206. }
  207. } # }}}
  208. 1