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