summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/meta.pm
blob: d7f56a8bd112712f38dfa9d8e73801427d772f4c (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, $destpage, 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. # fallthrough
  71. }
  72. elsif ($key eq 'description') {
  73. $pagestate{$page}{meta}{description}=HTML::Entities::encode_numeric($value);
  74. # fallthrough
  75. }
  76. elsif ($key eq 'guid') {
  77. my $abs = $value;
  78. if ($abs =~ m/^[[:xdigit:]]{8}-[[:xdigit:]]{4}-[[:xdigit]]{4}-[[:xdigit:]]{4}-[[:xdigit:]]{12}$/) {
  79. $abs = lc "urn:uuid:$abs";
  80. }
  81. $pagestate{$page}{meta}{guid}=HTML::Entities::encode_numeric($abs);
  82. # fallthrough
  83. }
  84. elsif ($key eq 'license') {
  85. push @{$metaheaders{$page}}, '<link rel="license" href="#page_license" />';
  86. $pagestate{$page}{meta}{license}=$value;
  87. return "";
  88. }
  89. elsif ($key eq 'copyright') {
  90. push @{$metaheaders{$page}}, '<link rel="copyright" href="#page_copyright" />';
  91. $pagestate{$page}{meta}{copyright}=$value;
  92. return "";
  93. }
  94. elsif ($key eq 'link' && ! %params) {
  95. # hidden WikiLink
  96. push @{$links{$page}}, $value;
  97. return "";
  98. }
  99. elsif ($key eq 'author') {
  100. $pagestate{$page}{meta}{author}=$value;
  101. # fallthorough
  102. }
  103. elsif ($key eq 'authorurl') {
  104. $pagestate{$page}{meta}{authorurl}=$value if safeurl($value);
  105. # fallthrough
  106. }
  107. if (! defined wantarray) {
  108. # avoid collecting duplicate data during scan pass
  109. return;
  110. }
  111. # Metadata collection that happens only during preprocessing pass.
  112. if ($key eq 'date') {
  113. eval q{use Date::Parse};
  114. if (! $@) {
  115. my $time = str2time($value);
  116. $IkiWiki::pagectime{$page}=$time if defined $time;
  117. }
  118. }
  119. elsif ($key eq 'permalink') {
  120. if (safeurl($value)) {
  121. $pagestate{$page}{meta}{permalink}=$value;
  122. push @{$metaheaders{$page}}, scrub('<link rel="bookmark" href="'.encode_entities($value).'" />');
  123. }
  124. }
  125. elsif ($key eq 'stylesheet') {
  126. my $rel=exists $params{rel} ? $params{rel} : "alternate stylesheet";
  127. my $title=exists $params{title} ? $params{title} : $value;
  128. # adding .css to the value prevents using any old web
  129. # editable page as a stylesheet
  130. my $stylesheet=bestlink($page, $value.".css");
  131. if (! length $stylesheet) {
  132. return "[[meta ".gettext("stylesheet not found")."]]";
  133. }
  134. push @{$metaheaders{$page}}, '<link href="'.urlto($stylesheet, $page).
  135. '" rel="'.encode_entities($rel).
  136. '" title="'.encode_entities($title).
  137. "\" type=\"text/css\" />";
  138. }
  139. elsif ($key eq 'openid') {
  140. if (exists $params{server} && safeurl($params{server})) {
  141. push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
  142. '" rel="openid.server" />';
  143. push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
  144. '" rel="openid2.provider" />';
  145. }
  146. if (safeurl($value)) {
  147. push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
  148. '" rel="openid.delegate" />';
  149. push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
  150. '" rel="openid2.local_id" />';
  151. }
  152. if (exists $params{"xrds-location"} && safeurl($params{"xrds-location"})) {
  153. push @{$metaheaders{$page}}, '<meta http-equiv="X-XRDS-Location"'.
  154. 'content="'.encode_entities($params{"xrds-location"}).'" />';
  155. }
  156. }
  157. elsif ($key eq 'redir') {
  158. return "" if $page ne $destpage;
  159. my $safe=0;
  160. if ($value !~ /^\w+:\/\//) {
  161. my ($redir_page, $redir_anchor) = split /\#/, $value;
  162. add_depends($page, $redir_page);
  163. my $link=bestlink($page, $redir_page);
  164. if (! length $link) {
  165. return "[[meta ".gettext("redir page not found")."]]";
  166. }
  167. $value=urlto($link, $page);
  168. $value.='#'.$redir_anchor if defined $redir_anchor;
  169. $safe=1;
  170. # redir cycle detection
  171. $pagestate{$page}{meta}{redir}=$link;
  172. my $at=$page;
  173. my %seen;
  174. while (exists $pagestate{$at}{meta}{redir}) {
  175. if ($seen{$at}) {
  176. return "[[meta ".gettext("redir cycle is not allowed")."]]";
  177. }
  178. $seen{$at}=1;
  179. $at=$pagestate{$at}{meta}{redir};
  180. }
  181. }
  182. else {
  183. $value=encode_entities($value);
  184. }
  185. my $delay=int(exists $params{delay} ? $params{delay} : 0);
  186. my $redir="<meta http-equiv=\"refresh\" content=\"$delay; URL=$value\" />";
  187. if (! $safe) {
  188. $redir=scrub($redir);
  189. }
  190. push @{$metaheaders{$page}}, $redir;
  191. }
  192. elsif ($key eq 'link') {
  193. if (%params) {
  194. push @{$metaheaders{$page}}, scrub("<link href=\"".encode_entities($value)."\" ".
  195. join(" ", map {
  196. encode_entities($_)."=\"".encode_entities(decode_entities($params{$_}))."\""
  197. } keys %params).
  198. " />\n");
  199. }
  200. }
  201. elsif ($key eq 'robots') {
  202. push @{$metaheaders{$page}}, '<meta name="robots"'.
  203. ' content="'.encode_entities($value).'" />';
  204. }
  205. else {
  206. push @{$metaheaders{$page}}, scrub('<meta name="'.encode_entities($key).
  207. '" content="'.encode_entities($value).'" />');
  208. }
  209. return "";
  210. } # }}}
  211. sub pagetemplate (@) { #{{{
  212. my %params=@_;
  213. my $page=$params{page};
  214. my $destpage=$params{destpage};
  215. my $template=$params{template};
  216. if (exists $metaheaders{$page} && $template->query(name => "meta")) {
  217. # avoid duplicate meta lines
  218. my %seen;
  219. $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$metaheaders{$page}}));
  220. }
  221. if (exists $pagestate{$page}{meta}{title} && $template->query(name => "title")) {
  222. $template->param(title => $pagestate{$page}{meta}{title});
  223. $template->param(title_overridden => 1);
  224. }
  225. foreach my $field (qw{author authorurl permalink}) {
  226. $template->param($field => $pagestate{$page}{meta}{$field})
  227. if exists $pagestate{$page}{meta}{$field} && $template->query(name => $field);
  228. }
  229. foreach my $field (qw{license copyright}) {
  230. if (exists $pagestate{$page}{meta}{$field} && $template->query(name => $field) &&
  231. ($page eq $destpage || ! exists $pagestate{$destpage}{meta}{$field} ||
  232. $pagestate{$page}{meta}{$field} ne $pagestate{$destpage}{meta}{$field})) {
  233. $template->param($field => htmlize($page, $destpage, $pagestate{$page}{meta}{$field}));
  234. }
  235. }
  236. } # }}}
  237. sub match { #{{{
  238. my $field=shift;
  239. my $page=shift;
  240. # turn glob into a safe regexp
  241. my $re=IkiWiki::glob2re(shift);
  242. my $val;
  243. if (exists $pagestate{$page}{meta}{$field}) {
  244. $val=$pagestate{$page}{meta}{$field};
  245. }
  246. elsif ($field eq 'title') {
  247. $val=pagetitle($page);
  248. }
  249. if (defined $val) {
  250. if ($val=~/^$re$/i) {
  251. return IkiWiki::SuccessReason->new("$re matches $field of $page");
  252. }
  253. else {
  254. return IkiWiki::FailReason->new("$re does not match $field of $page");
  255. }
  256. }
  257. else {
  258. return IkiWiki::FailReason->new("$page does not have a $field");
  259. }
  260. } #}}}
  261. package IkiWiki::PageSpec;
  262. sub match_title ($$;@) { #{{{
  263. IkiWiki::Plugin::meta::match("title", @_);
  264. } #}}}
  265. sub match_author ($$;@) { #{{{
  266. IkiWiki::Plugin::meta::match("author", @_);
  267. } #}}}
  268. sub match_authorurl ($$;@) { #{{{
  269. IkiWiki::Plugin::meta::match("authorurl", @_);
  270. } #}}}
  271. sub match_license ($$;@) { #{{{
  272. IkiWiki::Plugin::meta::match("license", @_);
  273. } #}}}
  274. sub match_copyright ($$;@) { #{{{
  275. IkiWiki::Plugin::meta::match("copyright", @_);
  276. } #}}}
  277. 1