summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/meta.pm
blob: 4a22fed3057015156c27d9f8159e9f4b47e3a633 (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. push @{$links{$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. if (exists $params{server} && safeurl($params{server})) {
  153. push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
  154. '" rel="openid.server" />';
  155. push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
  156. '" rel="openid2.provider" />';
  157. }
  158. if (safeurl($value)) {
  159. push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
  160. '" rel="openid.delegate" />';
  161. push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
  162. '" rel="openid2.local_id" />';
  163. }
  164. if (exists $params{"xrds-location"} && safeurl($params{"xrds-location"})) {
  165. push @{$metaheaders{$page}}, '<meta http-equiv="X-XRDS-Location"'.
  166. 'content="'.encode_entities($params{"xrds-location"}).'" />';
  167. }
  168. }
  169. elsif ($key eq 'redir') {
  170. return "" if $page ne $destpage;
  171. my $safe=0;
  172. if ($value !~ /^\w+:\/\//) {
  173. my ($redir_page, $redir_anchor) = split /\#/, $value;
  174. add_depends($page, $redir_page);
  175. my $link=bestlink($page, $redir_page);
  176. if (! length $link) {
  177. error gettext("redir page not found")
  178. }
  179. $value=urlto($link, $page);
  180. $value.='#'.$redir_anchor if defined $redir_anchor;
  181. $safe=1;
  182. # redir cycle detection
  183. $pagestate{$page}{meta}{redir}=$link;
  184. my $at=$page;
  185. my %seen;
  186. while (exists $pagestate{$at}{meta}{redir}) {
  187. if ($seen{$at}) {
  188. error gettext("redir cycle is not allowed")
  189. }
  190. $seen{$at}=1;
  191. $at=$pagestate{$at}{meta}{redir};
  192. }
  193. }
  194. else {
  195. $value=encode_entities($value);
  196. }
  197. my $delay=int(exists $params{delay} ? $params{delay} : 0);
  198. my $redir="<meta http-equiv=\"refresh\" content=\"$delay; URL=$value\" />";
  199. if (! $safe) {
  200. $redir=scrub($redir, $destpage);
  201. }
  202. push @{$metaheaders{$page}}, $redir;
  203. }
  204. elsif ($key eq 'link') {
  205. if (%params) {
  206. push @{$metaheaders{$page}}, scrub("<link href=\"".encode_entities($value)."\" ".
  207. join(" ", map {
  208. encode_entities($_)."=\"".encode_entities(decode_entities($params{$_}))."\""
  209. } keys %params).
  210. " />\n", $destpage);
  211. }
  212. }
  213. elsif ($key eq 'robots') {
  214. push @{$metaheaders{$page}}, '<meta name="robots"'.
  215. ' content="'.encode_entities($value).'" />';
  216. }
  217. else {
  218. push @{$metaheaders{$page}}, scrub('<meta name="'.encode_entities($key).
  219. '" content="'.encode_entities($value).'" />', $destpage);
  220. }
  221. return "";
  222. }
  223. sub pagetemplate (@) {
  224. my %params=@_;
  225. my $page=$params{page};
  226. my $destpage=$params{destpage};
  227. my $template=$params{template};
  228. if (exists $metaheaders{$page} && $template->query(name => "meta")) {
  229. # avoid duplicate meta lines
  230. my %seen;
  231. $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$metaheaders{$page}}));
  232. }
  233. if (exists $pagestate{$page}{meta}{title} && $template->query(name => "title")) {
  234. $template->param(title => $pagestate{$page}{meta}{title});
  235. $template->param(title_overridden => 1);
  236. }
  237. foreach my $field (qw{author authorurl permalink}) {
  238. $template->param($field => $pagestate{$page}{meta}{$field})
  239. if exists $pagestate{$page}{meta}{$field} && $template->query(name => $field);
  240. }
  241. foreach my $field (qw{license copyright}) {
  242. if (exists $pagestate{$page}{meta}{$field} && $template->query(name => $field) &&
  243. ($page eq $destpage || ! exists $pagestate{$destpage}{meta}{$field} ||
  244. $pagestate{$page}{meta}{$field} ne $pagestate{$destpage}{meta}{$field})) {
  245. $template->param($field => htmlize($page, $destpage, $pagestate{$page}{meta}{$field}));
  246. }
  247. }
  248. }
  249. sub match {
  250. my $field=shift;
  251. my $page=shift;
  252. # turn glob into a safe regexp
  253. my $re=IkiWiki::glob2re(shift);
  254. my $val;
  255. if (exists $pagestate{$page}{meta}{$field}) {
  256. $val=$pagestate{$page}{meta}{$field};
  257. }
  258. elsif ($field eq 'title') {
  259. $val = pagetitle($page);
  260. }
  261. if (defined $val) {
  262. if ($val=~/^$re$/i) {
  263. return IkiWiki::SuccessReason->new("$re matches $field of $page");
  264. }
  265. else {
  266. return IkiWiki::FailReason->new("$re does not match $field of $page");
  267. }
  268. }
  269. else {
  270. return IkiWiki::FailReason->new("$page does not have a $field");
  271. }
  272. }
  273. package IkiWiki::PageSpec;
  274. sub match_title ($$;@) {
  275. IkiWiki::Plugin::meta::match("title", @_);
  276. }
  277. sub match_author ($$;@) {
  278. IkiWiki::Plugin::meta::match("author", @_);
  279. }
  280. sub match_authorurl ($$;@) {
  281. IkiWiki::Plugin::meta::match("authorurl", @_);
  282. }
  283. sub match_license ($$;@) {
  284. IkiWiki::Plugin::meta::match("license", @_);
  285. }
  286. sub match_copyright ($$;@) {
  287. IkiWiki::Plugin::meta::match("copyright", @_);
  288. }
  289. 1