summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/meta.pm
blob: 8803747fb9bd3cb932b222f9dd8f81aa600874b8 (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. push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
  131. '" rel="openid2.provider" />';
  132. }
  133. if (safeurl($value)) {
  134. push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
  135. '" rel="openid.delegate" />';
  136. push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
  137. '" rel="openid2.local_id" />';
  138. }
  139. if (exists $params{xrds-location} && safeurl($params{xrds-location})) {
  140. push @{$metaheaders{$page}}, '<meta http-equiv="X-XRDS-Location"'.
  141. 'content="'.encode_entities($params{xrds-location}).'" />';
  142. }
  143. }
  144. elsif ($key eq 'redir') {
  145. return "" if $page ne $destpage;
  146. my $safe=0;
  147. if ($value !~ /^\w+:\/\//) {
  148. my ($redir_page, $redir_anchor) = split /\#/, $value;
  149. add_depends($page, $redir_page);
  150. my $link=bestlink($page, $redir_page);
  151. if (! length $link) {
  152. return "[[meta ".gettext("redir page not found")."]]";
  153. }
  154. $value=urlto($link, $page);
  155. $value.='#'.$redir_anchor if defined $redir_anchor;
  156. $safe=1;
  157. # redir cycle detection
  158. $pagestate{$page}{meta}{redir}=$link;
  159. my $at=$page;
  160. my %seen;
  161. while (exists $pagestate{$at}{meta}{redir}) {
  162. if ($seen{$at}) {
  163. return "[[meta ".gettext("redir cycle is not allowed")."]]";
  164. }
  165. $seen{$at}=1;
  166. $at=$pagestate{$at}{meta}{redir};
  167. }
  168. }
  169. else {
  170. $value=encode_entities($value);
  171. }
  172. my $delay=int(exists $params{delay} ? $params{delay} : 0);
  173. my $redir="<meta http-equiv=\"refresh\" content=\"$delay; URL=$value\">";
  174. if (! $safe) {
  175. $redir=scrub($redir);
  176. }
  177. push @{$metaheaders{$page}}, $redir;
  178. }
  179. elsif ($key eq 'link') {
  180. if (%params) {
  181. push @{$metaheaders{$page}}, scrub("<link href=\"".encode_entities($value)."\" ".
  182. join(" ", map {
  183. encode_entities($_)."=\"".encode_entities(decode_entities($params{$_}))."\""
  184. } keys %params).
  185. " />\n");
  186. }
  187. }
  188. elsif ($key eq 'robots') {
  189. push @{$metaheaders{$page}}, '<meta name="robots"'.
  190. ' content="'.encode_entities($value).'" />';
  191. }
  192. else {
  193. push @{$metaheaders{$page}}, scrub('<meta name="'.encode_entities($key).
  194. '" content="'.encode_entities($value).'" />');
  195. }
  196. return "";
  197. } # }}}
  198. sub pagetemplate (@) { #{{{
  199. my %params=@_;
  200. my $page=$params{page};
  201. my $destpage=$params{destpage};
  202. my $template=$params{template};
  203. if (exists $metaheaders{$page} && $template->query(name => "meta")) {
  204. # avoid duplicate meta lines
  205. my %seen;
  206. $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$metaheaders{$page}}));
  207. }
  208. if (exists $pagestate{$page}{meta}{title} && $template->query(name => "title")) {
  209. $template->param(title => $pagestate{$page}{meta}{title});
  210. $template->param(title_overridden => 1);
  211. }
  212. foreach my $field (qw{author authorurl permalink}) {
  213. $template->param($field => $pagestate{$page}{meta}{$field})
  214. if exists $pagestate{$page}{meta}{$field} && $template->query(name => $field);
  215. }
  216. foreach my $field (qw{license copyright}) {
  217. if (exists $pagestate{$page}{meta}{$field} && $template->query(name => $field) &&
  218. ($page eq $destpage || ! exists $pagestate{$destpage}{meta}{$field} ||
  219. $pagestate{$page}{meta}{$field} ne $pagestate{$destpage}{meta}{$field})) {
  220. $template->param($field => htmlize($page, $destpage, $pagestate{$page}{meta}{$field}));
  221. }
  222. }
  223. } # }}}
  224. sub match { #{{{
  225. my $field=shift;
  226. my $page=shift;
  227. # turn glob into a safe regexp
  228. my $re=quotemeta(shift);
  229. $re=~s/\\\*/.*/g;
  230. $re=~s/\\\?/./g;
  231. my $val;
  232. if (exists $pagestate{$page}{meta}{$field}) {
  233. $val=$pagestate{$page}{meta}{$field};
  234. }
  235. elsif ($field eq 'title') {
  236. $val=pagetitle($page);
  237. }
  238. if (defined $val) {
  239. if ($val=~/^$re$/i) {
  240. return IkiWiki::SuccessReason->new("$re matches $field of $page");
  241. }
  242. else {
  243. return IkiWiki::FailReason->new("$re does not match $field of $page");
  244. }
  245. }
  246. else {
  247. return IkiWiki::FailReason->new("$page does not have a $field");
  248. }
  249. } #}}}
  250. package IkiWiki::PageSpec;
  251. sub match_title ($$;@) { #{{{
  252. IkiWiki::Plugin::meta::match("title", @_);
  253. } #}}}
  254. sub match_author ($$;@) { #{{{
  255. IkiWiki::Plugin::meta::match("author", @_);
  256. } #}}}
  257. sub match_authorurl ($$;@) { #{{{
  258. IkiWiki::Plugin::meta::match("authorurl", @_);
  259. } #}}}
  260. sub match_license ($$;@) { #{{{
  261. IkiWiki::Plugin::meta::match("license", @_);
  262. } #}}}
  263. sub match_copyright ($$;@) { #{{{
  264. IkiWiki::Plugin::meta::match("copyright", @_);
  265. } #}}}
  266. 1