summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/meta.pm
blob: 1590086141a7306f570799387a12017fcbaeb716 (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. section => "core",
  20. },
  21. }
  22. sub needsbuild (@) {
  23. my $needsbuild=shift;
  24. foreach my $page (keys %pagestate) {
  25. if (exists $pagestate{$page}{meta}) {
  26. if (exists $pagesources{$page} &&
  27. grep { $_ eq $pagesources{$page} } @$needsbuild) {
  28. # remove state, it will be re-added
  29. # if the preprocessor directive is still
  30. # there during the rebuild
  31. delete $pagestate{$page}{meta};
  32. }
  33. }
  34. }
  35. }
  36. sub scrub ($$) {
  37. if (IkiWiki::Plugin::htmlscrubber->can("sanitize")) {
  38. return IkiWiki::Plugin::htmlscrubber::sanitize(
  39. content => shift, destpage => shift);
  40. }
  41. else {
  42. return shift;
  43. }
  44. }
  45. sub safeurl ($) {
  46. my $url=shift;
  47. if (exists $IkiWiki::Plugin::htmlscrubber::{safe_url_regexp} &&
  48. defined $IkiWiki::Plugin::htmlscrubber::safe_url_regexp) {
  49. return $url=~/$IkiWiki::Plugin::htmlscrubber::safe_url_regexp/;
  50. }
  51. else {
  52. return 1;
  53. }
  54. }
  55. sub htmlize ($$$) {
  56. my $page = shift;
  57. my $destpage = shift;
  58. return IkiWiki::htmlize($page, $destpage, pagetype($pagesources{$page}),
  59. IkiWiki::linkify($page, $destpage,
  60. IkiWiki::preprocess($page, $destpage, shift)));
  61. }
  62. sub preprocess (@) {
  63. return "" unless @_;
  64. my %params=@_;
  65. my $key=shift;
  66. my $value=$params{$key};
  67. delete $params{$key};
  68. my $page=$params{page};
  69. delete $params{page};
  70. my $destpage=$params{destpage};
  71. delete $params{destpage};
  72. delete $params{preview};
  73. eval q{use HTML::Entities};
  74. # Always decode, even if encoding later, since it might not be
  75. # fully encoded.
  76. $value=decode_entities($value);
  77. # Metadata collection that needs to happen during the scan pass.
  78. if ($key eq 'title') {
  79. $pagestate{$page}{meta}{title}=$value;
  80. if (exists $params{sortas}) {
  81. $pagestate{$page}{meta}{titlesort}=$params{sortas};
  82. }
  83. else {
  84. delete $pagestate{$page}{meta}{titlesort};
  85. }
  86. return "";
  87. }
  88. elsif ($key eq 'description') {
  89. $pagestate{$page}{meta}{description}=$value;
  90. # fallthrough
  91. }
  92. elsif ($key eq 'guid') {
  93. $pagestate{$page}{meta}{guid}=$value;
  94. # fallthrough
  95. }
  96. elsif ($key eq 'license') {
  97. push @{$metaheaders{$page}}, '<link rel="license" href="#page_license" />';
  98. $pagestate{$page}{meta}{license}=$value;
  99. return "";
  100. }
  101. elsif ($key eq 'copyright') {
  102. push @{$metaheaders{$page}}, '<link rel="copyright" href="#page_copyright" />';
  103. $pagestate{$page}{meta}{copyright}=$value;
  104. return "";
  105. }
  106. elsif ($key eq 'link' && ! %params) {
  107. # hidden WikiLink
  108. add_link($page, $value);
  109. return "";
  110. }
  111. elsif ($key eq 'author') {
  112. $pagestate{$page}{meta}{author}=$value;
  113. if (exists $params{sortas}) {
  114. $pagestate{$page}{meta}{authorsort}=$params{sortas};
  115. }
  116. else {
  117. delete $pagestate{$page}{meta}{authorsort};
  118. }
  119. # fallthorough
  120. }
  121. elsif ($key eq 'authorurl') {
  122. $pagestate{$page}{meta}{authorurl}=$value if safeurl($value);
  123. # fallthrough
  124. }
  125. elsif ($key eq 'permalink') {
  126. $pagestate{$page}{meta}{permalink}=$value if safeurl($value);
  127. # fallthrough
  128. }
  129. elsif ($key eq 'date') {
  130. eval q{use Date::Parse};
  131. if (! $@) {
  132. my $time = str2time($value);
  133. $IkiWiki::pagectime{$page}=$time if defined $time;
  134. }
  135. }
  136. elsif ($key eq 'updated') {
  137. eval q{use Date::Parse};
  138. if (! $@) {
  139. my $time = str2time($value);
  140. $pagestate{$page}{meta}{updated}=$time if defined $time;
  141. }
  142. }
  143. if (! defined wantarray) {
  144. # avoid collecting duplicate data during scan pass
  145. return;
  146. }
  147. # Metadata handling that happens only during preprocessing pass.
  148. if ($key eq 'permalink') {
  149. if (safeurl($value)) {
  150. push @{$metaheaders{$page}}, scrub('<link rel="bookmark" href="'.encode_entities($value).'" />', $destpage);
  151. }
  152. }
  153. elsif ($key eq 'stylesheet') {
  154. my $rel=exists $params{rel} ? $params{rel} : "alternate stylesheet";
  155. my $title=exists $params{title} ? $params{title} : $value;
  156. # adding .css to the value prevents using any old web
  157. # editable page as a stylesheet
  158. my $stylesheet=bestlink($page, $value.".css");
  159. if (! length $stylesheet) {
  160. error gettext("stylesheet not found")
  161. }
  162. push @{$metaheaders{$page}}, '<link href="'.urlto($stylesheet, $page).
  163. '" rel="'.encode_entities($rel).
  164. '" title="'.encode_entities($title).
  165. "\" type=\"text/css\" />";
  166. }
  167. elsif ($key eq 'openid') {
  168. my $delegate=0; # both by default
  169. if (exists $params{delegate}) {
  170. $delegate = 1 if lc $params{delegate} eq 'openid';
  171. $delegate = 2 if lc $params{delegate} eq 'openid2';
  172. }
  173. if (exists $params{server} && safeurl($params{server})) {
  174. push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
  175. '" rel="openid.server" />' if $delegate ne 2;
  176. push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
  177. '" rel="openid2.provider" />' if $delegate ne 1;
  178. }
  179. if (safeurl($value)) {
  180. push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
  181. '" rel="openid.delegate" />' if $delegate ne 2;
  182. push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
  183. '" rel="openid2.local_id" />' if $delegate ne 1;
  184. }
  185. if (exists $params{"xrds-location"} && safeurl($params{"xrds-location"})) {
  186. push @{$metaheaders{$page}}, '<meta http-equiv="X-XRDS-Location"'.
  187. 'content="'.encode_entities($params{"xrds-location"}).'" />';
  188. }
  189. }
  190. elsif ($key eq 'redir') {
  191. return "" if $page ne $destpage;
  192. my $safe=0;
  193. if ($value !~ /^\w+:\/\//) {
  194. my ($redir_page, $redir_anchor) = split /\#/, $value;
  195. my $link=bestlink($page, $redir_page);
  196. if (! length $link) {
  197. error gettext("redir page not found")
  198. }
  199. add_depends($page, $link, deptype("presence"));
  200. $value=urlto($link, $page);
  201. $value.='#'.$redir_anchor if defined $redir_anchor;
  202. $safe=1;
  203. # redir cycle detection
  204. $pagestate{$page}{meta}{redir}=$link;
  205. my $at=$page;
  206. my %seen;
  207. while (exists $pagestate{$at}{meta}{redir}) {
  208. if ($seen{$at}) {
  209. error gettext("redir cycle is not allowed")
  210. }
  211. $seen{$at}=1;
  212. $at=$pagestate{$at}{meta}{redir};
  213. }
  214. }
  215. else {
  216. $value=encode_entities($value);
  217. }
  218. my $delay=int(exists $params{delay} ? $params{delay} : 0);
  219. my $redir="<meta http-equiv=\"refresh\" content=\"$delay; URL=$value\" />";
  220. if (! $safe) {
  221. $redir=scrub($redir, $destpage);
  222. }
  223. push @{$metaheaders{$page}}, $redir;
  224. }
  225. elsif ($key eq 'link') {
  226. if (%params) {
  227. push @{$metaheaders{$page}}, scrub("<link href=\"".encode_entities($value)."\" ".
  228. join(" ", map {
  229. encode_entities($_)."=\"".encode_entities(decode_entities($params{$_}))."\""
  230. } keys %params).
  231. " />\n", $destpage);
  232. }
  233. }
  234. elsif ($key eq 'robots') {
  235. push @{$metaheaders{$page}}, '<meta name="robots"'.
  236. ' content="'.encode_entities($value).'" />';
  237. }
  238. elsif ($key eq 'description') {
  239. push @{$metaheaders{$page}}, '<meta name="'.encode_entities($key).
  240. '" content="'.encode_entities($value).'" />';
  241. }
  242. else {
  243. push @{$metaheaders{$page}}, scrub('<meta name="'.encode_entities($key).
  244. '" content="'.encode_entities($value).'" />', $destpage);
  245. }
  246. return "";
  247. }
  248. sub pagetemplate (@) {
  249. my %params=@_;
  250. my $page=$params{page};
  251. my $destpage=$params{destpage};
  252. my $template=$params{template};
  253. if (exists $metaheaders{$page} && $template->query(name => "meta")) {
  254. # avoid duplicate meta lines
  255. my %seen;
  256. $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$metaheaders{$page}}));
  257. }
  258. if (exists $pagestate{$page}{meta}{title} && $template->query(name => "title")) {
  259. $template->param(title => HTML::Entities::encode_numeric($pagestate{$page}{meta}{title}));
  260. $template->param(title_overridden => 1);
  261. }
  262. foreach my $field (qw{author authorurl permalink}) {
  263. $template->param($field => $pagestate{$page}{meta}{$field})
  264. if exists $pagestate{$page}{meta}{$field} && $template->query(name => $field);
  265. }
  266. foreach my $field (qw{description}) {
  267. $template->param($field => HTML::Entities::encode_numeric($pagestate{$page}{meta}{$field}))
  268. if exists $pagestate{$page}{meta}{$field} && $template->query(name => $field);
  269. }
  270. foreach my $field (qw{license copyright}) {
  271. if (exists $pagestate{$page}{meta}{$field} && $template->query(name => $field) &&
  272. ($page eq $destpage || ! exists $pagestate{$destpage}{meta}{$field} ||
  273. $pagestate{$page}{meta}{$field} ne $pagestate{$destpage}{meta}{$field})) {
  274. $template->param($field => htmlize($page, $destpage, $pagestate{$page}{meta}{$field}));
  275. }
  276. }
  277. }
  278. sub get_sort_key {
  279. my $page = shift;
  280. my $meta = shift;
  281. # e.g. titlesort (also makes sense for author)
  282. my $key = $pagestate{$page}{meta}{$meta . "sort"};
  283. return $key if defined $key;
  284. # e.g. title
  285. $key = $pagestate{$page}{meta}{$meta};
  286. return $key if defined $key;
  287. # fall back to closer-to-core things
  288. if ($meta eq 'title') {
  289. return pagetitle(IkiWiki::basename($page));
  290. }
  291. elsif ($meta eq 'date') {
  292. return $IkiWiki::pagectime{$page};
  293. }
  294. elsif ($meta eq 'updated') {
  295. return $IkiWiki::pagemtime{$page};
  296. }
  297. else {
  298. return '';
  299. }
  300. }
  301. sub match {
  302. my $field=shift;
  303. my $page=shift;
  304. # turn glob into a safe regexp
  305. my $re=IkiWiki::glob2re(shift);
  306. my $val;
  307. if (exists $pagestate{$page}{meta}{$field}) {
  308. $val=$pagestate{$page}{meta}{$field};
  309. }
  310. elsif ($field eq 'title') {
  311. $val = pagetitle($page);
  312. }
  313. if (defined $val) {
  314. if ($val=~/^$re$/i) {
  315. return IkiWiki::SuccessReason->new("$re matches $field of $page", $page => $IkiWiki::DEPEND_CONTENT, "" => 1);
  316. }
  317. else {
  318. return IkiWiki::FailReason->new("$re does not match $field of $page", $page => $IkiWiki::DEPEND_CONTENT, "" => 1);
  319. }
  320. }
  321. else {
  322. return IkiWiki::FailReason->new("$page does not have a $field", $page => $IkiWiki::DEPEND_CONTENT);
  323. }
  324. }
  325. package IkiWiki::PageSpec;
  326. sub match_title ($$;@) {
  327. IkiWiki::Plugin::meta::match("title", @_);
  328. }
  329. sub match_author ($$;@) {
  330. IkiWiki::Plugin::meta::match("author", @_);
  331. }
  332. sub match_authorurl ($$;@) {
  333. IkiWiki::Plugin::meta::match("authorurl", @_);
  334. }
  335. sub match_license ($$;@) {
  336. IkiWiki::Plugin::meta::match("license", @_);
  337. }
  338. sub match_copyright ($$;@) {
  339. IkiWiki::Plugin::meta::match("copyright", @_);
  340. }
  341. package IkiWiki::SortSpec;
  342. sub cmp_meta {
  343. my $meta = shift;
  344. error(gettext("sort=meta requires a parameter")) unless defined $meta;
  345. if ($meta eq 'updated' || $meta eq 'date') {
  346. return IkiWiki::Plugin::meta::get_sort_key($a, $meta)
  347. <=>
  348. IkiWiki::Plugin::meta::get_sort_key($b, $meta);
  349. }
  350. return IkiWiki::Plugin::meta::get_sort_key($a, $meta)
  351. cmp
  352. IkiWiki::Plugin::meta::get_sort_key($b, $meta);
  353. }
  354. # A prototype of how sort=title could behave in 4.0 or something
  355. sub cmp_meta_title {
  356. $_[0] = 'title';
  357. return cmp_meta(@_);
  358. }
  359. 1