summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/meta.pm
blob: 74b630afc27c5c355973ba5aa6bb398122508816 (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. }
  136. elsif ($key eq 'redir') {
  137. return "" if $page ne $destpage;
  138. my $safe=0;
  139. if ($value !~ /^\w+:\/\//) {
  140. my ($redir_page, $redir_anchor) = split /\#/, $value;
  141. add_depends($page, $redir_page);
  142. my $link=bestlink($page, $redir_page);
  143. if (! length $link) {
  144. return "[[meta ".gettext("redir page not found")."]]";
  145. }
  146. $value=urlto($link, $page);
  147. $value.='#'.$redir_anchor if defined $redir_anchor;
  148. $safe=1;
  149. # redir cycle detection
  150. $pagestate{$page}{meta}{redir}=$link;
  151. my $at=$page;
  152. my %seen;
  153. while (exists $pagestate{$at}{meta}{redir}) {
  154. if ($seen{$at}) {
  155. return "[[meta ".gettext("redir cycle is not allowed")."]]";
  156. }
  157. $seen{$at}=1;
  158. $at=$pagestate{$at}{meta}{redir};
  159. }
  160. }
  161. else {
  162. $value=encode_entities($value);
  163. }
  164. my $delay=int(exists $params{delay} ? $params{delay} : 0);
  165. my $redir="<meta http-equiv=\"refresh\" content=\"$delay; URL=$value\">";
  166. if (! $safe) {
  167. $redir=scrub($redir);
  168. }
  169. push @{$metaheaders{$page}}, $redir;
  170. }
  171. elsif ($key eq 'link') {
  172. if (%params) {
  173. push @{$metaheaders{$page}}, scrub("<link href=\"".encode_entities($value)."\" ".
  174. join(" ", map {
  175. encode_entities($_)."=\"".encode_entities(decode_entities($params{$_}))."\""
  176. } keys %params).
  177. " />\n");
  178. }
  179. }
  180. else {
  181. push @{$metaheaders{$page}}, scrub('<meta name="'.encode_entities($key).
  182. '" content="'.encode_entities($value).'" />');
  183. }
  184. return "";
  185. } # }}}
  186. sub pagetemplate (@) { #{{{
  187. my %params=@_;
  188. my $page=$params{page};
  189. my $destpage=$params{destpage};
  190. my $template=$params{template};
  191. if (exists $metaheaders{$page} && $template->query(name => "meta")) {
  192. # avoid duplicate meta lines
  193. my %seen;
  194. $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$metaheaders{$page}}));
  195. }
  196. if (exists $pagestate{$page}{meta}{title} && $template->query(name => "title")) {
  197. $template->param(title => $pagestate{$page}{meta}{title});
  198. $template->param(title_overridden => 1);
  199. }
  200. foreach my $field (qw{author authorurl permalink}) {
  201. $template->param($field => $pagestate{$page}{meta}{$field})
  202. if exists $pagestate{$page}{meta}{$field} && $template->query(name => $field);
  203. }
  204. foreach my $field (qw{license copyright}) {
  205. if (exists $pagestate{$page}{meta}{$field} && $template->query(name => $field) &&
  206. ($page eq $destpage || ! exists $pagestate{$destpage}{meta}{$field} ||
  207. $pagestate{$page}{meta}{$field} ne $pagestate{$destpage}{meta}{$field})) {
  208. $template->param($field => htmlize($page, $destpage, $pagestate{$page}{meta}{$field}));
  209. }
  210. }
  211. } # }}}
  212. sub match { #{{{
  213. my $field=shift;
  214. my $page=shift;
  215. # turn glob into a safe regexp
  216. my $re=quotemeta(shift);
  217. $re=~s/\\\*/.*/g;
  218. $re=~s/\\\?/./g;
  219. my $val;
  220. if (exists $pagestate{$page}{meta}{$field}) {
  221. $val=$pagestate{$page}{meta}{$field};
  222. }
  223. elsif ($field eq 'title') {
  224. $val=pagetitle($page);
  225. }
  226. if (defined $val) {
  227. if ($val=~/^$re$/i) {
  228. return IkiWiki::SuccessReason->new("$re matches $field of $page");
  229. }
  230. else {
  231. return IkiWiki::FailReason->new("$re does not match $field of $page");
  232. }
  233. }
  234. else {
  235. return IkiWiki::FailReason->new("$page does not have a $field");
  236. }
  237. } #}}}
  238. package IkiWiki::PageSpec;
  239. sub match_title ($$;@) { #{{{
  240. IkiWiki::Plugin::meta::match("title", @_);
  241. } #}}}
  242. sub match_author ($$;@) { #{{{
  243. IkiWiki::Plugin::meta::match("author", @_);
  244. } #}}}
  245. sub match_authorurl ($$;@) { #{{{
  246. IkiWiki::Plugin::meta::match("authorurl", @_);
  247. } #}}}
  248. sub match_license ($$;@) { #{{{
  249. IkiWiki::Plugin::meta::match("license", @_);
  250. } #}}}
  251. sub match_copyright ($$;@) { #{{{
  252. IkiWiki::Plugin::meta::match("copyright", @_);
  253. } #}}}
  254. 1