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