inline: Fix regression in feed titles. Closes: #610878 (Thanks, Paul Wise)
[ikiwiki.git] / IkiWiki / Plugin / meta.pm
blobad6d1a8e338895685be79486c5b4dac95f9c21b9
1 #!/usr/bin/perl
2 # Ikiwiki metadata plugin.
3 package IkiWiki::Plugin::meta;
5 use warnings;
6 use strict;
7 use IkiWiki 3.00;
9 my %metaheaders;
11 sub import {
12 hook(type => "getsetup", id => "meta", call => \&getsetup);
13 hook(type => "needsbuild", id => "meta", call => \&needsbuild);
14 hook(type => "preprocess", id => "meta", call => \&preprocess, scan => 1);
15 hook(type => "pagetemplate", id => "meta", call => \&pagetemplate);
18 sub getsetup () {
19 return
20 plugin => {
21 safe => 1,
22 rebuild => undef,
23 section => "core",
27 sub needsbuild (@) {
28 my $needsbuild=shift;
29 foreach my $page (keys %pagestate) {
30 if (exists $pagestate{$page}{meta}) {
31 if (exists $pagesources{$page} &&
32 grep { $_ eq $pagesources{$page} } @$needsbuild) {
33 # remove state, it will be re-added
34 # if the preprocessor directive is still
35 # there during the rebuild
36 delete $pagestate{$page}{meta};
40 return $needsbuild;
43 sub scrub ($$$) {
44 if (IkiWiki::Plugin::htmlscrubber->can("sanitize")) {
45 return IkiWiki::Plugin::htmlscrubber::sanitize(
46 content => shift, page => shift, destpage => shift);
48 else {
49 return shift;
53 sub safeurl ($) {
54 my $url=shift;
55 if (exists $IkiWiki::Plugin::htmlscrubber::{safe_url_regexp} &&
56 defined $IkiWiki::Plugin::htmlscrubber::safe_url_regexp) {
57 return $url=~/$IkiWiki::Plugin::htmlscrubber::safe_url_regexp/;
59 else {
60 return 1;
64 sub htmlize ($$$) {
65 my $page = shift;
66 my $destpage = shift;
68 return IkiWiki::htmlize($page, $destpage, pagetype($pagesources{$page}),
69 IkiWiki::linkify($page, $destpage,
70 IkiWiki::preprocess($page, $destpage, shift)));
73 sub preprocess (@) {
74 return "" unless @_;
75 my %params=@_;
76 my $key=shift;
77 my $value=$params{$key};
78 delete $params{$key};
79 my $page=$params{page};
80 delete $params{page};
81 my $destpage=$params{destpage};
82 delete $params{destpage};
83 delete $params{preview};
85 eval q{use HTML::Entities};
86 # Always decode, even if encoding later, since it might not be
87 # fully encoded.
88 $value=decode_entities($value);
90 # Metadata collection that needs to happen during the scan pass.
91 if ($key eq 'title') {
92 $pagestate{$page}{meta}{title}=$value;
93 if (exists $params{sortas}) {
94 $pagestate{$page}{meta}{titlesort}=$params{sortas};
96 else {
97 delete $pagestate{$page}{meta}{titlesort};
99 return "";
101 elsif ($key eq 'description') {
102 $pagestate{$page}{meta}{description}=$value;
103 # fallthrough
105 elsif ($key eq 'guid') {
106 $pagestate{$page}{meta}{guid}=$value;
107 # fallthrough
109 elsif ($key eq 'license') {
110 push @{$metaheaders{$page}}, '<link rel="license" href="#page_license" />';
111 $pagestate{$page}{meta}{license}=$value;
112 return "";
114 elsif ($key eq 'copyright') {
115 push @{$metaheaders{$page}}, '<link rel="copyright" href="#page_copyright" />';
116 $pagestate{$page}{meta}{copyright}=$value;
117 return "";
119 elsif ($key eq 'link' && ! %params) {
120 # hidden WikiLink
121 add_link($page, $value);
122 return "";
124 elsif ($key eq 'author') {
125 $pagestate{$page}{meta}{author}=$value;
126 if (exists $params{sortas}) {
127 $pagestate{$page}{meta}{authorsort}=$params{sortas};
129 else {
130 delete $pagestate{$page}{meta}{authorsort};
132 # fallthorough
134 elsif ($key eq 'authorurl') {
135 $pagestate{$page}{meta}{authorurl}=$value if safeurl($value);
136 # fallthrough
138 elsif ($key eq 'permalink') {
139 $pagestate{$page}{meta}{permalink}=$value if safeurl($value);
140 # fallthrough
142 elsif ($key eq 'date') {
143 eval q{use Date::Parse};
144 if (! $@) {
145 my $time = str2time($value);
146 $IkiWiki::pagectime{$page}=$time if defined $time;
149 elsif ($key eq 'updated') {
150 eval q{use Date::Parse};
151 if (! $@) {
152 my $time = str2time($value);
153 $pagestate{$page}{meta}{updated}=$time if defined $time;
157 if (! defined wantarray) {
158 # avoid collecting duplicate data during scan pass
159 return;
162 # Metadata handling that happens only during preprocessing pass.
163 if ($key eq 'permalink') {
164 if (safeurl($value)) {
165 push @{$metaheaders{$page}}, scrub('<link rel="bookmark" href="'.encode_entities($value).'" />', $page, $destpage);
168 elsif ($key eq 'stylesheet') {
169 my $rel=exists $params{rel} ? $params{rel} : "alternate stylesheet";
170 my $title=exists $params{title} ? $params{title} : $value;
171 # adding .css to the value prevents using any old web
172 # editable page as a stylesheet
173 my $stylesheet=bestlink($page, $value.".css");
174 if (! length $stylesheet) {
175 error gettext("stylesheet not found")
177 push @{$metaheaders{$page}}, '<link href="'.urlto($stylesheet, $page).
178 '" rel="'.encode_entities($rel).
179 '" title="'.encode_entities($title).
180 "\" type=\"text/css\" />";
182 elsif ($key eq 'openid') {
183 my $delegate=0; # both by default
184 if (exists $params{delegate}) {
185 $delegate = 1 if lc $params{delegate} eq 'openid';
186 $delegate = 2 if lc $params{delegate} eq 'openid2';
188 if (exists $params{server} && safeurl($params{server})) {
189 push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
190 '" rel="openid.server" />' if $delegate ne 2;
191 push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
192 '" rel="openid2.provider" />' if $delegate ne 1;
194 if (safeurl($value)) {
195 push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
196 '" rel="openid.delegate" />' if $delegate ne 2;
197 push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
198 '" rel="openid2.local_id" />' if $delegate ne 1;
200 if (exists $params{"xrds-location"} && safeurl($params{"xrds-location"})) {
201 # force url absolute
202 eval q{use URI};
203 error($@) if $@;
204 my $url=URI->new_abs($params{"xrds-location"}, $config{url});
205 push @{$metaheaders{$page}}, '<meta http-equiv="X-XRDS-Location" '.
206 'content="'.encode_entities($url).'" />';
209 elsif ($key eq 'redir') {
210 return "" if $page ne $destpage;
211 my $safe=0;
212 if ($value !~ /^\w+:\/\//) {
213 my ($redir_page, $redir_anchor) = split /\#/, $value;
215 my $link=bestlink($page, $redir_page);
216 if (! length $link) {
217 error gettext("redir page not found")
219 add_depends($page, $link, deptype("presence"));
221 $value=urlto($link, $page);
222 $value.='#'.$redir_anchor if defined $redir_anchor;
223 $safe=1;
225 # redir cycle detection
226 $pagestate{$page}{meta}{redir}=$link;
227 my $at=$page;
228 my %seen;
229 while (exists $pagestate{$at}{meta}{redir}) {
230 if ($seen{$at}) {
231 error gettext("redir cycle is not allowed")
233 $seen{$at}=1;
234 $at=$pagestate{$at}{meta}{redir};
237 else {
238 $value=encode_entities($value);
240 my $delay=int(exists $params{delay} ? $params{delay} : 0);
241 my $redir="<meta http-equiv=\"refresh\" content=\"$delay; URL=$value\" />";
242 if (! $safe) {
243 $redir=scrub($redir, $page, $destpage);
245 push @{$metaheaders{$page}}, $redir;
247 elsif ($key eq 'link') {
248 if (%params) {
249 push @{$metaheaders{$page}}, scrub("<link href=\"".encode_entities($value)."\" ".
250 join(" ", map {
251 encode_entities($_)."=\"".encode_entities(decode_entities($params{$_}))."\""
252 } keys %params).
253 " />\n", $page, $destpage);
256 elsif ($key eq 'robots') {
257 push @{$metaheaders{$page}}, '<meta name="robots"'.
258 ' content="'.encode_entities($value).'" />';
260 elsif ($key eq 'description') {
261 push @{$metaheaders{$page}}, '<meta name="'.
262 encode_entities($key).
263 '" content="'.encode_entities($value).'" />';
265 elsif ($key eq 'name') {
266 push @{$metaheaders{$page}}, scrub('<meta '.$key.'="'.
267 encode_entities($value).
268 join(' ', map { "$_=\"$params{$_}\"" } keys %params).
269 ' />', $page, $destpage);
271 else {
272 push @{$metaheaders{$page}}, scrub('<meta name="'.
273 encode_entities($key).'" content="'.
274 encode_entities($value).'" />', $page, $destpage);
277 return "";
280 sub pagetemplate (@) {
281 my %params=@_;
282 my $page=$params{page};
283 my $destpage=$params{destpage};
284 my $template=$params{template};
286 if (exists $metaheaders{$page} && $template->query(name => "meta")) {
287 # avoid duplicate meta lines
288 my %seen;
289 $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$metaheaders{$page}}));
291 if (exists $pagestate{$page}{meta}{title} && $template->query(name => "title")) {
292 $template->param(title => HTML::Entities::encode_numeric($pagestate{$page}{meta}{title}));
293 $template->param(title_overridden => 1);
296 foreach my $field (qw{author authorurl permalink}) {
297 $template->param($field => $pagestate{$page}{meta}{$field})
298 if exists $pagestate{$page}{meta}{$field} && $template->query(name => $field);
301 foreach my $field (qw{permalink}) {
302 $template->param($field => IkiWiki::urlabs($pagestate{$page}{meta}{$field}, $config{url}))
303 if exists $pagestate{$page}{meta}{$field} && $template->query(name => $field);
306 foreach my $field (qw{description}) {
307 $template->param($field => HTML::Entities::encode_numeric($pagestate{$page}{meta}{$field}))
308 if exists $pagestate{$page}{meta}{$field} && $template->query(name => $field);
311 foreach my $field (qw{license copyright}) {
312 if (exists $pagestate{$page}{meta}{$field} && $template->query(name => $field) &&
313 ($page eq $destpage || ! exists $pagestate{$destpage}{meta}{$field} ||
314 $pagestate{$page}{meta}{$field} ne $pagestate{$destpage}{meta}{$field})) {
315 $template->param($field => htmlize($page, $destpage, $pagestate{$page}{meta}{$field}));
320 sub get_sort_key {
321 my $page = shift;
322 my $meta = shift;
324 # e.g. titlesort (also makes sense for author)
325 my $key = $pagestate{$page}{meta}{$meta . "sort"};
326 return $key if defined $key;
328 # e.g. title
329 $key = $pagestate{$page}{meta}{$meta};
330 return $key if defined $key;
332 # fall back to closer-to-core things
333 if ($meta eq 'title') {
334 return pagetitle(IkiWiki::basename($page));
336 elsif ($meta eq 'date') {
337 return $IkiWiki::pagectime{$page};
339 elsif ($meta eq 'updated') {
340 return $IkiWiki::pagemtime{$page};
342 else {
343 return '';
347 sub match {
348 my $field=shift;
349 my $page=shift;
351 # turn glob into a safe regexp
352 my $re=IkiWiki::glob2re(shift);
354 my $val;
355 if (exists $pagestate{$page}{meta}{$field}) {
356 $val=$pagestate{$page}{meta}{$field};
358 elsif ($field eq 'title') {
359 $val = pagetitle($page);
362 if (defined $val) {
363 if ($val=~$re) {
364 return IkiWiki::SuccessReason->new("$re matches $field of $page", $page => $IkiWiki::DEPEND_CONTENT, "" => 1);
366 else {
367 return IkiWiki::FailReason->new("$re does not match $field of $page", $page => $IkiWiki::DEPEND_CONTENT, "" => 1);
370 else {
371 return IkiWiki::FailReason->new("$page does not have a $field", $page => $IkiWiki::DEPEND_CONTENT);
375 package IkiWiki::PageSpec;
377 sub match_title ($$;@) {
378 IkiWiki::Plugin::meta::match("title", @_);
381 sub match_author ($$;@) {
382 IkiWiki::Plugin::meta::match("author", @_);
385 sub match_authorurl ($$;@) {
386 IkiWiki::Plugin::meta::match("authorurl", @_);
389 sub match_license ($$;@) {
390 IkiWiki::Plugin::meta::match("license", @_);
393 sub match_copyright ($$;@) {
394 IkiWiki::Plugin::meta::match("copyright", @_);
397 sub match_guid ($$;@) {
398 IkiWiki::Plugin::meta::match("guid", @_);
401 package IkiWiki::SortSpec;
403 sub cmp_meta {
404 my $meta = shift;
405 error(gettext("sort=meta requires a parameter")) unless defined $meta;
407 if ($meta eq 'updated' || $meta eq 'date') {
408 return IkiWiki::Plugin::meta::get_sort_key($a, $meta)
410 IkiWiki::Plugin::meta::get_sort_key($b, $meta);
413 return IkiWiki::Plugin::meta::get_sort_key($a, $meta)
415 IkiWiki::Plugin::meta::get_sort_key($b, $meta);
418 # A prototype of how sort=title could behave in 4.0 or something
419 sub cmp_meta_title {
420 $_[0] = 'title';
421 return cmp_meta(@_);