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