2 # Ikiwiki metadata plugin.
3 package IkiWiki::Plugin::meta;
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);
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};
44 if (IkiWiki::Plugin::htmlscrubber->can("sanitize")) {
45 return IkiWiki::Plugin::htmlscrubber::sanitize(
46 content => shift, page => shift, destpage => 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/;
68 return IkiWiki::htmlize($page, $destpage, pagetype($pagesources{$page}),
69 IkiWiki::linkify($page, $destpage,
70 IkiWiki::preprocess($page, $destpage, shift)));
77 my $value=$params{$key};
79 my $page=$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
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};
97 delete $pagestate{$page}{meta}{titlesort};
101 elsif ($key eq 'description') {
102 $pagestate{$page}{meta}{description}=$value;
105 elsif ($key eq 'guid') {
106 $pagestate{$page}{meta}{guid}=$value;
109 elsif ($key eq 'license') {
110 push @{$metaheaders{$page}}, '<link rel="license" href="#page_license" />';
111 $pagestate{$page}{meta}{license}=$value;
114 elsif ($key eq 'copyright') {
115 push @{$metaheaders{$page}}, '<link rel="copyright" href="#page_copyright" />';
116 $pagestate{$page}{meta}{copyright}=$value;
119 elsif ($key eq 'link' && ! %params) {
121 add_link($page, $value);
124 elsif ($key eq 'enclosure') {
125 my $link=bestlink($page, $value);
126 if (! length $link) {
127 error gettext("enclosure not found")
129 add_depends($page, $link, deptype("presence"));
131 $value=urlto($link, $page);
132 $pagestate{$page}{meta}{enclosure}=$value;
134 elsif ($key eq 'author') {
135 $pagestate{$page}{meta}{author}=$value;
136 if (exists $params{sortas}) {
137 $pagestate{$page}{meta}{authorsort}=$params{sortas};
140 delete $pagestate{$page}{meta}{authorsort};
144 elsif ($key eq 'authorurl') {
145 $pagestate{$page}{meta}{authorurl}=$value if safeurl($value);
148 elsif ($key eq 'permalink') {
149 $pagestate{$page}{meta}{permalink}=$value if safeurl($value);
152 elsif ($key eq 'date') {
153 eval q{use Date::Parse};
155 my $time = str2time($value);
156 $IkiWiki::pagectime{$page}=$time if defined $time;
159 elsif ($key eq 'updated') {
160 eval q{use Date::Parse};
162 my $time = str2time($value);
163 $pagestate{$page}{meta}{updated}=$time if defined $time;
167 if (! defined wantarray) {
168 # avoid collecting duplicate data during scan pass
172 # Metadata handling that happens only during preprocessing pass.
173 if ($key eq 'permalink') {
174 if (safeurl($value)) {
175 push @{$metaheaders{$page}}, scrub('<link rel="bookmark" href="'.encode_entities($value).'" />', $page, $destpage);
178 elsif ($key eq 'stylesheet') {
179 my $rel=exists $params{rel} ? $params{rel} : "alternate stylesheet";
180 my $title=exists $params{title} ? $params{title} : $value;
181 # adding .css to the value prevents using any old web
182 # editable page as a stylesheet
183 my $stylesheet=bestlink($page, $value.".css");
184 if (! length $stylesheet) {
185 error gettext("stylesheet not found")
187 push @{$metaheaders{$page}}, scrub('<link href="'.urlto($stylesheet, $page).
188 '" rel="'.encode_entities($rel).
189 '" title="'.encode_entities($title).
190 "\" type=\"text/css\" />", $page, $destpage);
192 elsif ($key eq 'script') {
193 my $defer=exists $params{defer} ? ' defer="defer"' : '';
194 my $async=exists $params{async} ? ' async="async"' : '';
195 my $js=bestlink($page, $value.".js");
197 error gettext("script not found");
199 push @{$metaheaders{$page}}, scrub('<script src="'.urlto($js, $page).
200 '"' . $defer . $async . ' type="text/javascript"></script>',
203 elsif ($key eq 'openid') {
204 my $delegate=0; # both by default
205 if (exists $params{delegate}) {
206 $delegate = 1 if lc $params{delegate} eq 'openid';
207 $delegate = 2 if lc $params{delegate} eq 'openid2';
209 if (exists $params{server} && safeurl($params{server})) {
210 push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
211 '" rel="openid.server" />' if $delegate ne 2;
212 push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
213 '" rel="openid2.provider" />' if $delegate ne 1;
215 if (safeurl($value)) {
216 push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
217 '" rel="openid.delegate" />' if $delegate ne 2;
218 push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
219 '" rel="openid2.local_id" />' if $delegate ne 1;
221 if (exists $params{"xrds-location"} && safeurl($params{"xrds-location"})) {
225 my $url=URI->new_abs($params{"xrds-location"}, $config{url});
226 push @{$metaheaders{$page}}, '<meta http-equiv="X-XRDS-Location" '.
227 'content="'.encode_entities($url).'" />';
230 elsif ($key eq 'foaf') {
231 if (safeurl($value)) {
232 push @{$metaheaders{$page}}, '<link rel="meta" '.
233 'type="application/rdf+xml" title="FOAF" '.
234 'href="'.encode_entities($value).'" />';
237 elsif ($key eq 'redir') {
238 return "" if $page ne $destpage;
240 if ($value !~ /^\w+:\/\//) {
241 my ($redir_page, $redir_anchor) = split /\#/, $value;
243 my $link=bestlink($page, $redir_page);
244 if (! length $link) {
245 error gettext("redir page not found")
247 add_depends($page, $link, deptype("presence"));
249 $value=urlto($link, $page);
250 $value.='#'.$redir_anchor if defined $redir_anchor;
253 # redir cycle detection
254 $pagestate{$page}{meta}{redir}=$link;
257 while (exists $pagestate{$at}{meta}{redir}) {
259 error gettext("redir cycle is not allowed")
262 $at=$pagestate{$at}{meta}{redir};
266 $value=encode_entities($value);
268 my $delay=int(exists $params{delay} ? $params{delay} : 0);
269 my $redir="<meta http-equiv=\"refresh\" content=\"$delay; URL=$value\" />";
271 $redir=scrub($redir, $page, $destpage);
273 push @{$metaheaders{$page}}, $redir;
275 elsif ($key eq 'link') {
277 push @{$metaheaders{$page}}, scrub("<link href=\"".encode_entities($value)."\" ".
279 encode_entities($_)."=\"".encode_entities(decode_entities($params{$_}))."\""
281 " />\n", $page, $destpage);
284 elsif ($key eq 'robots') {
285 push @{$metaheaders{$page}}, '<meta name="robots"'.
286 ' content="'.encode_entities($value).'" />';
288 elsif ($key eq 'description' || $key eq 'author') {
289 push @{$metaheaders{$page}}, '<meta name="'.$key.
290 '" content="'.encode_entities($value).'" />';
292 elsif ($key eq 'name') {
293 push @{$metaheaders{$page}}, scrub('<meta name="'.
294 encode_entities($value).
295 join(' ', map { "$_=\"$params{$_}\"" } keys %params).
296 ' />', $page, $destpage);
298 elsif ($key eq 'keywords') {
299 # Make sure the keyword string is safe: only allow alphanumeric
300 # characters, space and comma and strip the rest.
301 $value =~ s/[^[:alnum:], ]+//g;
302 push @{$metaheaders{$page}}, '<meta name="keywords"'.
303 ' content="'.encode_entities($value).'" />';
306 push @{$metaheaders{$page}}, scrub('<meta name="'.
307 encode_entities($key).'" content="'.
308 encode_entities($value).'" />', $page, $destpage);
314 sub pagetemplate (@) {
316 my $page=$params{page};
317 my $destpage=$params{destpage};
318 my $template=$params{template};
320 if (exists $metaheaders{$page} && $template->query(name => "meta")) {
321 # avoid duplicate meta lines
323 $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$metaheaders{$page}}));
325 if (exists $pagestate{$page}{meta}{title} && $template->query(name => "title")) {
326 eval q{use HTML::Entities};
327 $template->param(title => HTML::Entities::encode_numeric($pagestate{$page}{meta}{title}));
328 $template->param(title_overridden => 1);
331 if (exists $pagestate{$page}{meta}{enclosure}) {
332 $template->param(enclosure => $pagestate{$page}{meta}{enclosure});
335 foreach my $field (qw{authorurl}) {
336 eval q{use HTML::Entities};
337 $template->param($field => HTML::Entities::encode_entities($pagestate{$page}{meta}{$field}))
338 if exists $pagestate{$page}{meta}{$field} && $template->query(name => $field);
341 foreach my $field (qw{permalink}) {
342 if (exists $pagestate{$page}{meta}{$field} && $template->query(name => $field)) {
343 eval q{use HTML::Entities};
344 $template->param($field => HTML::Entities::encode_entities(IkiWiki::urlabs($pagestate{$page}{meta}{$field}, $config{url})));
348 foreach my $field (qw{description author}) {
349 eval q{use HTML::Entities};
350 $template->param($field => HTML::Entities::encode_numeric($pagestate{$page}{meta}{$field}))
351 if exists $pagestate{$page}{meta}{$field} && $template->query(name => $field);
354 foreach my $field (qw{license copyright}) {
355 if (exists $pagestate{$page}{meta}{$field} && $template->query(name => $field) &&
356 ($page eq $destpage || ! exists $pagestate{$destpage}{meta}{$field} ||
357 $pagestate{$page}{meta}{$field} ne $pagestate{$destpage}{meta}{$field})) {
358 $template->param($field => htmlize($page, $destpage, $pagestate{$page}{meta}{$field}));
367 # e.g. titlesort (also makes sense for author)
368 my $key = $pagestate{$page}{meta}{$meta . "sort"};
369 return $key if defined $key;
372 $key = $pagestate{$page}{meta}{$meta};
373 return $key if defined $key;
375 # fall back to closer-to-core things
376 if ($meta eq 'title') {
377 return pagetitle(IkiWiki::basename($page));
379 elsif ($meta eq 'date') {
380 return $IkiWiki::pagectime{$page};
382 elsif ($meta eq 'updated') {
383 return $IkiWiki::pagemtime{$page};
394 # turn glob into a safe regexp
395 my $re=IkiWiki::glob2re(shift);
398 if (exists $pagestate{$page}{meta}{$field}) {
399 $val=$pagestate{$page}{meta}{$field};
401 elsif ($field eq 'title') {
402 $val = pagetitle($page);
407 return IkiWiki::SuccessReason->new("$re matches $field of $page", $page => $IkiWiki::DEPEND_CONTENT, "" => 1);
410 return IkiWiki::FailReason->new("$re does not match $field of $page", $page => $IkiWiki::DEPEND_CONTENT, "" => 1);
414 return IkiWiki::FailReason->new("$page does not have a $field", $page => $IkiWiki::DEPEND_CONTENT);
418 package IkiWiki::PageSpec;
420 sub match_title ($$;@) {
421 IkiWiki::Plugin::meta::match("title", @_);
424 sub match_author ($$;@) {
425 IkiWiki::Plugin::meta::match("author", @_);
428 sub match_authorurl ($$;@) {
429 IkiWiki::Plugin::meta::match("authorurl", @_);
432 sub match_license ($$;@) {
433 IkiWiki::Plugin::meta::match("license", @_);
436 sub match_copyright ($$;@) {
437 IkiWiki::Plugin::meta::match("copyright", @_);
440 sub match_guid ($$;@) {
441 IkiWiki::Plugin::meta::match("guid", @_);
444 package IkiWiki::SortSpec;
448 error(gettext("sort=meta requires a parameter")) unless defined $meta;
450 if ($meta eq 'updated' || $meta eq 'date') {
451 return IkiWiki::Plugin::meta::get_sort_key($a, $meta)
453 IkiWiki::Plugin::meta::get_sort_key($b, $meta);
456 return IkiWiki::Plugin::meta::get_sort_key($a, $meta)
458 IkiWiki::Plugin::meta::get_sort_key($b, $meta);
461 # A prototype of how sort=title could behave in 4.0 or something