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, 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 'author') {
125 $pagestate{$page}{meta}{author}=$value;
126 if (exists $params{sortas}) {
127 $pagestate{$page}{meta}{authorsort}=$params{sortas};
130 delete $pagestate{$page}{meta}{authorsort};
134 elsif ($key eq 'authorurl') {
135 $pagestate{$page}{meta}{authorurl}=$value if safeurl($value);
138 elsif ($key eq 'permalink') {
139 $pagestate{$page}{meta}{permalink}=$value if safeurl($value);
142 elsif ($key eq 'date') {
143 eval q{use Date::Parse};
145 my $time = str2time($value);
146 $IkiWiki::pagectime{$page}=$time if defined $time;
149 elsif ($key eq 'updated') {
150 eval q{use Date::Parse};
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
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).'" />', $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 push @{$metaheaders{$page}}, '<meta http-equiv="X-XRDS-Location"'.
202 'content="'.encode_entities($params{"xrds-location"}).'" />';
205 elsif ($key eq 'redir') {
206 return "" if $page ne $destpage;
208 if ($value !~ /^\w+:\/\//) {
209 my ($redir_page, $redir_anchor) = split /\#/, $value;
211 my $link=bestlink($page, $redir_page);
212 if (! length $link) {
213 error gettext("redir page not found")
215 add_depends($page, $link, deptype("presence"));
217 $value=urlto($link, $page);
218 $value.='#'.$redir_anchor if defined $redir_anchor;
221 # redir cycle detection
222 $pagestate{$page}{meta}{redir}=$link;
225 while (exists $pagestate{$at}{meta}{redir}) {
227 error gettext("redir cycle is not allowed")
230 $at=$pagestate{$at}{meta}{redir};
234 $value=encode_entities($value);
236 my $delay=int(exists $params{delay} ? $params{delay} : 0);
237 my $redir="<meta http-equiv=\"refresh\" content=\"$delay; URL=$value\" />";
239 $redir=scrub($redir, $destpage);
241 push @{$metaheaders{$page}}, $redir;
243 elsif ($key eq 'link') {
245 push @{$metaheaders{$page}}, scrub("<link href=\"".encode_entities($value)."\" ".
247 encode_entities($_)."=\"".encode_entities(decode_entities($params{$_}))."\""
252 elsif ($key eq 'robots') {
253 push @{$metaheaders{$page}}, '<meta name="robots"'.
254 ' content="'.encode_entities($value).'" />';
256 elsif ($key eq 'description') {
257 push @{$metaheaders{$page}}, '<meta name="'.
258 encode_entities($key).
259 '" content="'.encode_entities($value).'" />';
261 elsif ($key eq 'name') {
262 push @{$metaheaders{$page}}, scrub('<meta '.$key.'="'.
263 encode_entities($value).
264 join(' ', map { "$_=\"$params{$_}\"" } keys %params).
268 push @{$metaheaders{$page}}, scrub('<meta name="'.
269 encode_entities($key).'" content="'.
270 encode_entities($value).'" />', $destpage);
276 sub pagetemplate (@) {
278 my $page=$params{page};
279 my $destpage=$params{destpage};
280 my $template=$params{template};
282 if (exists $metaheaders{$page} && $template->query(name => "meta")) {
283 # avoid duplicate meta lines
285 $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$metaheaders{$page}}));
287 if (exists $pagestate{$page}{meta}{title} && $template->query(name => "title")) {
288 $template->param(title => HTML::Entities::encode_numeric($pagestate{$page}{meta}{title}));
289 $template->param(title_overridden => 1);
292 foreach my $field (qw{author authorurl permalink}) {
293 $template->param($field => $pagestate{$page}{meta}{$field})
294 if exists $pagestate{$page}{meta}{$field} && $template->query(name => $field);
297 foreach my $field (qw{description}) {
298 $template->param($field => HTML::Entities::encode_numeric($pagestate{$page}{meta}{$field}))
299 if exists $pagestate{$page}{meta}{$field} && $template->query(name => $field);
302 foreach my $field (qw{license copyright}) {
303 if (exists $pagestate{$page}{meta}{$field} && $template->query(name => $field) &&
304 ($page eq $destpage || ! exists $pagestate{$destpage}{meta}{$field} ||
305 $pagestate{$page}{meta}{$field} ne $pagestate{$destpage}{meta}{$field})) {
306 $template->param($field => htmlize($page, $destpage, $pagestate{$page}{meta}{$field}));
315 # e.g. titlesort (also makes sense for author)
316 my $key = $pagestate{$page}{meta}{$meta . "sort"};
317 return $key if defined $key;
320 $key = $pagestate{$page}{meta}{$meta};
321 return $key if defined $key;
323 # fall back to closer-to-core things
324 if ($meta eq 'title') {
325 return pagetitle(IkiWiki::basename($page));
327 elsif ($meta eq 'date') {
328 return $IkiWiki::pagectime{$page};
330 elsif ($meta eq 'updated') {
331 return $IkiWiki::pagemtime{$page};
342 # turn glob into a safe regexp
343 my $re=IkiWiki::glob2re(shift);
346 if (exists $pagestate{$page}{meta}{$field}) {
347 $val=$pagestate{$page}{meta}{$field};
349 elsif ($field eq 'title') {
350 $val = pagetitle($page);
354 if ($val=~/^$re$/i) {
355 return IkiWiki::SuccessReason->new("$re matches $field of $page", $page => $IkiWiki::DEPEND_CONTENT, "" => 1);
358 return IkiWiki::FailReason->new("$re does not match $field of $page", $page => $IkiWiki::DEPEND_CONTENT, "" => 1);
362 return IkiWiki::FailReason->new("$page does not have a $field", $page => $IkiWiki::DEPEND_CONTENT);
366 package IkiWiki::PageSpec;
368 sub match_title ($$;@) {
369 IkiWiki::Plugin::meta::match("title", @_);
372 sub match_author ($$;@) {
373 IkiWiki::Plugin::meta::match("author", @_);
376 sub match_authorurl ($$;@) {
377 IkiWiki::Plugin::meta::match("authorurl", @_);
380 sub match_license ($$;@) {
381 IkiWiki::Plugin::meta::match("license", @_);
384 sub match_copyright ($$;@) {
385 IkiWiki::Plugin::meta::match("copyright", @_);
388 sub match_guid ($$;@) {
389 IkiWiki::Plugin::meta::match("guid", @_);
392 package IkiWiki::SortSpec;
396 error(gettext("sort=meta requires a parameter")) unless defined $meta;
398 if ($meta eq 'updated' || $meta eq 'date') {
399 return IkiWiki::Plugin::meta::get_sort_key($a, $meta)
401 IkiWiki::Plugin::meta::get_sort_key($b, $meta);
404 return IkiWiki::Plugin::meta::get_sort_key($a, $meta)
406 IkiWiki::Plugin::meta::get_sort_key($b, $meta);
409 # A prototype of how sort=title could behave in 4.0 or something