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