summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/meta.pm
blob: b2295923e31d94ed8d393a09d1ebeb2f05604878 (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 '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 'updated') {
  120. eval q{use Date::Parse};
  121. if (! $@) {
  122. my $time = str2time($value);
  123. $pagestate{$page}{meta}{updated}=$time if defined $time;
  124. }
  125. }
  126. if (! defined wantarray) {
  127. # avoid collecting duplicate data during scan pass
  128. return;
  129. }
  130. # Metadata collection that happens only during preprocessing pass.
  131. if ($key eq 'permalink') {
  132. if (safeurl($value)) {
  133. $pagestate{$page}{meta}{permalink}=$value;
  134. push @{$metaheaders{$page}}, scrub('<link rel="bookmark" href="'.encode_entities($value).'" />', $destpage);
  135. }
  136. }
  137. elsif ($key eq 'stylesheet') {
  138. my $rel=exists $params{rel} ? $params{rel} : "alternate stylesheet";
  139. my $title=exists $params{title} ? $params{title} : $value;
  140. # adding .css to the value prevents using any old web
  141. # editable page as a stylesheet
  142. my $stylesheet=bestlink($page, $value.".css");
  143. if (! length $stylesheet) {
  144. error gettext("stylesheet not found")
  145. }
  146. push @{$metaheaders{$page}}, '<link href="'.urlto($stylesheet, $page).
  147. '" rel="'.encode_entities($rel).
  148. '" title="'.encode_entities($title).
  149. "\" type=\"text/css\" />";
  150. }
  151. elsif ($key eq 'openid') {
  152. my $delegate=0; # both by default
  153. if (exists $params{delegate}) {
  154. $delegate = 1 if lc $params{delegate} eq 'openid';
  155. $delegate = 2 if lc $params{delegate} eq 'openid2';
  156. }
  157. if (exists $params{server} && safeurl($params{server})) {
  158. push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
  159. '" rel="openid.server" />' if $delegate ne 2;
  160. push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
  161. '" rel="openid2.provider" />' if $delegate ne 1;
  162. }
  163. if (safeurl($value)) {
  164. push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
  165. '" rel="openid.delegate" />' if $delegate ne 2;
  166. push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
  167. '" rel="openid2.local_id" />' if $delegate ne 1;
  168. }
  169. if (exists $params{"xrds-location"} && safeurl($params{"xrds-location"})) {
  170. push @{$metaheaders{$page}}, '<meta http-equiv="X-XRDS-Location"'.
  171. 'content="'.encode_entities($params{"xrds-location"}).'" />';
  172. }
  173. }
  174. elsif ($key eq 'redir') {
  175. return "" if $page ne $destpage;
  176. my $safe=0;
  177. if ($value !~ /^\w+:\/\//) {
  178. my ($redir_page, $redir_anchor) = split /\#/, $value;
  179. add_depends($page, $redir_page);
  180. my $link=bestlink($page, $redir_page);
  181. if (! length $link) {
  182. error gettext("redir page not found")
  183. }
  184. $value=urlto($link, $page);
  185. $value.='#'.$redir_anchor if defined $redir_anchor;
  186. $safe=1;
  187. # redir cycle detection
  188. $pagestate{$page}{meta}{redir}=$link;
  189. my $at=$page;
  190. my %seen;
  191. while (exists $pagestate{$at}{meta}{redir}) {
  192. if ($seen{$at}) {
  193. error gettext("redir cycle is not allowed")
  194. }
  195. $seen{$at}=1;
  196. $at=$pagestate{$at}{meta}{redir};
  197. }
  198. }
  199. else {
  200. $value=encode_entities($value);
  201. }
  202. my $delay=int(exists $params{delay} ? $params{delay} : 0);
  203. my $redir="<meta http-equiv=\"refresh\" content=\"$delay; URL=$value\" />";
  204. if (! $safe) {
  205. $redir=scrub($redir, $destpage);
  206. }
  207. push @{$metaheaders{$page}}, $redir;
  208. }
  209. elsif ($key eq 'link') {
  210. if (%params) {
  211. push @{$metaheaders{$page}}, scrub("<link href=\"".encode_entities($value)."\" ".
  212. join(" ", map {
  213. encode_entities($_)."=\"".encode_entities(decode_entities($params{$_}))."\""
  214. } keys %params).
  215. " />\n", $destpage);
  216. }
  217. }
  218. elsif ($key eq 'robots') {
  219. push @{$metaheaders{$page}}, '<meta name="robots"'.
  220. ' content="'.encode_entities($value).'" />';
  221. }
  222. else {
  223. push @{$metaheaders{$page}}, scrub('<meta name="'.encode_entities($key).
  224. '" content="'.encode_entities($value).'" />', $destpage);
  225. }
  226. return "";
  227. }
  228. sub pagetemplate (@) {
  229. my %params=@_;
  230. my $page=$params{page};
  231. my $destpage=$params{destpage};
  232. my $template=$params{template};
  233. if (exists $metaheaders{$page} && $template->query(name => "meta")) {
  234. # avoid duplicate meta lines
  235. my %seen;
  236. $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$metaheaders{$page}}));
  237. }
  238. if (exists $pagestate{$page}{meta}{title} && $template->query(name => "title")) {
  239. $template->param(title => $pagestate{$page}{meta}{title});
  240. $template->param(title_overridden => 1);
  241. }
  242. foreach my $field (qw{author authorurl permalink}) {
  243. $template->param($field => $pagestate{$page}{meta}{$field})
  244. if exists $pagestate{$page}{meta}{$field} && $template->query(name => $field);
  245. }
  246. foreach my $field (qw{license copyright}) {
  247. if (exists $pagestate{$page}{meta}{$field} && $template->query(name => $field) &&
  248. ($page eq $destpage || ! exists $pagestate{$destpage}{meta}{$field} ||
  249. $pagestate{$page}{meta}{$field} ne $pagestate{$destpage}{meta}{$field})) {
  250. $template->param($field => htmlize($page, $destpage, $pagestate{$page}{meta}{$field}));
  251. }
  252. }
  253. }
  254. sub match {
  255. my $field=shift;
  256. my $page=shift;
  257. # turn glob into a safe regexp
  258. my $re=IkiWiki::glob2re(shift);
  259. my $val;
  260. if (exists $pagestate{$page}{meta}{$field}) {
  261. $val=$pagestate{$page}{meta}{$field};
  262. }
  263. elsif ($field eq 'title') {
  264. $val = pagetitle($page);
  265. }
  266. if (defined $val) {
  267. if ($val=~/^$re$/i) {
  268. return IkiWiki::SuccessReason->new("$re matches $field of $page");
  269. }
  270. else {
  271. return IkiWiki::FailReason->new("$re does not match $field of $page");
  272. }
  273. }
  274. else {
  275. return IkiWiki::FailReason->new("$page does not have a $field");
  276. }
  277. }
  278. package IkiWiki::PageSpec;
  279. sub match_title ($$;@) {
  280. IkiWiki::Plugin::meta::match("title", @_);
  281. }
  282. sub match_author ($$;@) {
  283. IkiWiki::Plugin::meta::match("author", @_);
  284. }
  285. sub match_authorurl ($$;@) {
  286. IkiWiki::Plugin::meta::match("authorurl", @_);
  287. }
  288. sub match_license ($$;@) {
  289. IkiWiki::Plugin::meta::match("license", @_);
  290. }
  291. sub match_copyright ($$;@) {
  292. IkiWiki::Plugin::meta::match("copyright", @_);
  293. }
  294. 1