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