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