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