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="#pagelicense" />';
111 $pagestate{$page}{meta}{license}=$value;
114 elsif ($key eq 'copyright') {
115 push @{$metaheaders{$page}}, '<link rel="copyright" href="#pagecopyright" />';
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, 1);
132 $pagestate{$page}{meta}{enclosure}=$value;
133 $pagestate{$page}{meta}{enclosurefile}=$link;
136 elsif ($key eq 'author') {
137 $pagestate{$page}{meta}{author}=$value;
138 if (exists $params{sortas}) {
139 $pagestate{$page}{meta}{authorsort}=$params{sortas};
142 delete $pagestate{$page}{meta}{authorsort};
146 elsif ($key eq 'authorurl') {
147 $pagestate{$page}{meta}{authorurl}=$value if safeurl($value);
150 elsif ($key eq 'permalink') {
151 $pagestate{$page}{meta}{permalink}=$value if safeurl($value);
154 elsif ($key eq 'date') {
155 eval q{use Date::Parse};
157 my $time = str2time($value);
158 $IkiWiki::pagectime{$page}=$time if defined $time;
161 elsif ($key eq 'updated') {
162 eval q{use Date::Parse};
164 my $time = str2time($value);
165 $pagestate{$page}{meta}{updated}=$time if defined $time;
169 if (! defined wantarray) {
170 # avoid collecting duplicate data during scan pass
174 # Metadata handling that happens only during preprocessing pass.
175 if ($key eq 'permalink') {
176 if (safeurl($value)) {
177 push @{$metaheaders{$page}}, scrub('<link rel="bookmark" href="'.encode_entities($value).'" />', $page, $destpage);
180 elsif ($key eq 'stylesheet') {
181 my $rel=exists $params{rel} ? $params{rel} : "alternate stylesheet";
182 my $title=exists $params{title} ? $params{title} : $value;
183 # adding .css to the value prevents using any old web
184 # editable page as a stylesheet
185 my $stylesheet=bestlink($page, $value.".css");
186 if (! length $stylesheet) {
187 error gettext("stylesheet not found")
189 push @{$metaheaders{$page}}, scrub('<link href="'.urlto($stylesheet, $page).
190 '" rel="'.encode_entities($rel).
191 '" title="'.encode_entities($title).
192 "\" type=\"text/css\" />", $page, $destpage);
194 elsif ($key eq 'script') {
195 my $defer=exists $params{defer} ? ' defer="defer"' : '';
196 my $async=exists $params{async} ? ' async="async"' : '';
197 my $js=bestlink($page, $value.".js");
199 error gettext("script not found");
201 push @{$metaheaders{$page}}, scrub('<script src="'.urlto($js, $page).
202 '"' . $defer . $async . ' type="text/javascript"></script>',
205 elsif ($key eq 'openid') {
206 my $delegate=0; # both by default
207 if (exists $params{delegate}) {
208 $delegate = 1 if lc $params{delegate} eq 'openid';
209 $delegate = 2 if lc $params{delegate} eq 'openid2';
211 if (exists $params{server} && safeurl($params{server})) {
212 push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
213 '" rel="openid.server" />' if $delegate ne 2;
214 push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
215 '" rel="openid2.provider" />' if $delegate ne 1;
217 if (safeurl($value)) {
218 push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
219 '" rel="openid.delegate" />' if $delegate ne 2;
220 push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
221 '" rel="openid2.local_id" />' if $delegate ne 1;
223 if (exists $params{"xrds-location"} && safeurl($params{"xrds-location"})) {
227 my $url=URI->new_abs($params{"xrds-location"}, $config{url});
228 push @{$metaheaders{$page}}, '<meta http-equiv="X-XRDS-Location" '.
229 'content="'.encode_entities($url).'" />';
232 elsif ($key eq 'foaf') {
233 if (safeurl($value)) {
234 push @{$metaheaders{$page}}, '<link rel="meta" '.
235 'type="application/rdf+xml" title="FOAF" '.
236 'href="'.encode_entities($value).'" />';
239 elsif ($key eq 'redir') {
240 return "" if $page ne $destpage;
242 if ($value !~ /^\w+:\/\//) {
243 my ($redir_page, $redir_anchor) = split /\#/, $value;
245 my $link=bestlink($page, $redir_page);
246 if (! length $link) {
247 error gettext("redir page not found")
249 add_depends($page, $link, deptype("presence"));
251 $value=urlto($link, $page);
252 $value.='#'.$redir_anchor if defined $redir_anchor;
255 # redir cycle detection
256 $pagestate{$page}{meta}{redir}=$link;
259 while (exists $pagestate{$at}{meta}{redir}) {
261 error gettext("redir cycle is not allowed")
264 $at=$pagestate{$at}{meta}{redir};
268 $value=encode_entities($value);
270 my $delay=int(exists $params{delay} ? $params{delay} : 0);
271 my $redir="<meta http-equiv=\"refresh\" content=\"$delay; URL=$value\" />";
273 $redir=scrub($redir, $page, $destpage);
275 push @{$metaheaders{$page}}, $redir;
277 elsif ($key eq 'link') {
279 push @{$metaheaders{$page}}, scrub("<link href=\"".encode_entities($value)."\" ".
281 encode_entities($_)."=\"".encode_entities(decode_entities($params{$_}))."\""
283 " />\n", $page, $destpage);
286 elsif ($key eq 'robots') {
287 push @{$metaheaders{$page}}, '<meta name="robots"'.
288 ' content="'.encode_entities($value).'" />';
290 elsif ($key eq 'description' || $key eq 'author') {
291 push @{$metaheaders{$page}}, '<meta name="'.$key.
292 '" content="'.encode_entities($value).'" />';
294 elsif ($key eq 'name') {
295 push @{$metaheaders{$page}}, scrub('<meta name="'.
296 encode_entities($value).
297 join(' ', map { "$_=\"$params{$_}\"" } keys %params).
298 ' />', $page, $destpage);
300 elsif ($key eq 'keywords') {
301 # Make sure the keyword string is safe: only allow alphanumeric
302 # characters, space and comma and strip the rest.
303 $value =~ s/[^[:alnum:], ]+//g;
304 push @{$metaheaders{$page}}, '<meta name="keywords"'.
305 ' content="'.encode_entities($value).'" />';
308 push @{$metaheaders{$page}}, scrub('<meta name="'.
309 encode_entities($key).'" content="'.
310 encode_entities($value).'" />', $page, $destpage);
316 sub pagetemplate (@) {
318 my $page=$params{page};
319 my $destpage=$params{destpage};
320 my $template=$params{template};
322 if (exists $metaheaders{$page} && $template->query(name => "meta")) {
323 # avoid duplicate meta lines
325 $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$metaheaders{$page}}));
327 if (exists $pagestate{$page}{meta}{title} && $template->query(name => "title")) {
328 eval q{use HTML::Entities};
329 $template->param(title => HTML::Entities::encode_numeric($pagestate{$page}{meta}{title}));
330 $template->param(title_overridden => 1);
333 if (exists $pagestate{$page}{meta}{enclosure}) {
334 $template->param(enclosure => HTML::Entities::encode_entities(IkiWiki::urlabs($pagestate{$page}{meta}{enclosure}, $config{url})));
337 foreach my $field (qw{authorurl}) {
338 eval q{use HTML::Entities};
339 $template->param($field => HTML::Entities::encode_entities($pagestate{$page}{meta}{$field}))
340 if exists $pagestate{$page}{meta}{$field} && $template->query(name => $field);
343 foreach my $field (qw{permalink}) {
344 if (exists $pagestate{$page}{meta}{$field} && $template->query(name => $field)) {
345 eval q{use HTML::Entities};
346 $template->param($field => HTML::Entities::encode_entities(IkiWiki::urlabs($pagestate{$page}{meta}{$field}, $config{url})));
350 foreach my $field (qw{description author}) {
351 eval q{use HTML::Entities};
352 $template->param($field => HTML::Entities::encode_numeric($pagestate{$page}{meta}{$field}))
353 if exists $pagestate{$page}{meta}{$field} && $template->query(name => $field);
356 foreach my $field (qw{license copyright}) {
357 if (exists $pagestate{$page}{meta}{$field} && $template->query(name => $field) &&
358 ($page eq $destpage || ! exists $pagestate{$destpage}{meta}{$field} ||
359 $pagestate{$page}{meta}{$field} ne $pagestate{$destpage}{meta}{$field})) {
360 $template->param($field => htmlize($page, $destpage, $pagestate{$page}{meta}{$field}));
369 # e.g. titlesort (also makes sense for author)
370 my $key = $pagestate{$page}{meta}{$meta . "sort"};
371 return $key if defined $key;
374 $key = $pagestate{$page}{meta}{$meta};
375 return $key if defined $key;
377 # fall back to closer-to-core things
378 if ($meta eq 'title') {
379 return pagetitle(IkiWiki::basename($page));
381 elsif ($meta eq 'date') {
382 return $IkiWiki::pagectime{$page};
384 elsif ($meta eq 'updated') {
385 return $IkiWiki::pagemtime{$page};
396 # turn glob into a safe regexp
397 my $re=IkiWiki::glob2re(shift);
400 if (exists $pagestate{$page}{meta}{$field}) {
401 $val=$pagestate{$page}{meta}{$field};
403 elsif ($field eq 'title') {
404 $val = pagetitle($page);
409 return IkiWiki::SuccessReason->new("$re matches $field of $page", $page => $IkiWiki::DEPEND_CONTENT, "" => 1);
412 return IkiWiki::FailReason->new("$re does not match $field of $page", $page => $IkiWiki::DEPEND_CONTENT, "" => 1);
416 return IkiWiki::FailReason->new("$page does not have a $field", $page => $IkiWiki::DEPEND_CONTENT);
420 package IkiWiki::PageSpec;
422 sub match_title ($$;@) {
423 IkiWiki::Plugin::meta::match("title", @_);
426 sub match_author ($$;@) {
427 IkiWiki::Plugin::meta::match("author", @_);
430 sub match_authorurl ($$;@) {
431 IkiWiki::Plugin::meta::match("authorurl", @_);
434 sub match_license ($$;@) {
435 IkiWiki::Plugin::meta::match("license", @_);
438 sub match_copyright ($$;@) {
439 IkiWiki::Plugin::meta::match("copyright", @_);
442 sub match_guid ($$;@) {
443 IkiWiki::Plugin::meta::match("guid", @_);
446 package IkiWiki::SortSpec;
450 error(gettext("sort=meta requires a parameter")) unless defined $meta;
452 if ($meta eq 'updated' || $meta eq 'date') {
453 return IkiWiki::Plugin::meta::get_sort_key($a, $meta)
455 IkiWiki::Plugin::meta::get_sort_key($b, $meta);
458 return IkiWiki::Plugin::meta::get_sort_key($a, $meta)
460 IkiWiki::Plugin::meta::get_sort_key($b, $meta);
463 # A prototype of how sort=title could behave in 4.0 or something