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