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