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