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