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};
43 if (IkiWiki::Plugin::htmlscrubber->can("sanitize")) {
44 return IkiWiki::Plugin::htmlscrubber::sanitize(
45 content => shift, destpage => shift);
54 if (exists $IkiWiki::Plugin::htmlscrubber::{safe_url_regexp} &&
55 defined $IkiWiki::Plugin::htmlscrubber::safe_url_regexp) {
56 return $url=~/$IkiWiki::Plugin::htmlscrubber::safe_url_regexp/;
67 return IkiWiki::htmlize($page, $destpage, pagetype($pagesources{$page}),
68 IkiWiki::linkify($page, $destpage,
69 IkiWiki::preprocess($page, $destpage, shift)));
76 my $value=$params{$key};
78 my $page=$params{page};
80 my $destpage=$params{destpage};
81 delete $params{destpage};
82 delete $params{preview};
84 eval q{use HTML::Entities};
85 # Always decode, even if encoding later, since it might not be
87 $value=decode_entities($value);
89 # Metadata collection that needs to happen during the scan pass.
90 if ($key eq 'title') {
91 $pagestate{$page}{meta}{title}=HTML::Entities::encode_numeric($value);
92 if (exists $params{sortas}) {
93 $pagestate{$page}{meta}{titlesort}=$params{sortas};
96 $pagestate{$page}{meta}{titlesort}=$value;
100 elsif ($key eq 'description') {
101 $pagestate{$page}{meta}{description}=HTML::Entities::encode_numeric($value);
104 elsif ($key eq 'guid') {
105 $pagestate{$page}{meta}{guid}=HTML::Entities::encode_numeric($value);
108 elsif ($key eq 'license') {
109 push @{$metaheaders{$page}}, '<link rel="license" href="#page_license" />';
110 $pagestate{$page}{meta}{license}=$value;
113 elsif ($key eq 'copyright') {
114 push @{$metaheaders{$page}}, '<link rel="copyright" href="#page_copyright" />';
115 $pagestate{$page}{meta}{copyright}=$value;
118 elsif ($key eq 'link' && ! %params) {
120 add_link($page, $value);
123 elsif ($key eq 'author') {
124 $pagestate{$page}{meta}{author}=$value;
125 if (exists $params{sortas}) {
126 $pagestate{$page}{meta}{authorsort}=$params{sortas};
129 $pagestate{$page}{meta}{authorsort}=$value;
133 elsif ($key eq 'authorurl') {
134 $pagestate{$page}{meta}{authorurl}=$value if safeurl($value);
137 elsif ($key eq 'permalink') {
138 $pagestate{$page}{meta}{permalink}=$value if safeurl($value);
141 elsif ($key eq 'date') {
142 eval q{use Date::Parse};
144 my $time = str2time($value);
145 $IkiWiki::pagectime{$page}=$time if defined $time;
148 elsif ($key eq 'updated') {
149 eval q{use Date::Parse};
151 my $time = str2time($value);
152 $pagestate{$page}{meta}{updated}=$time if defined $time;
156 if (! defined wantarray) {
157 # avoid collecting duplicate data during scan pass
161 # Metadata handling that happens only during preprocessing pass.
162 if ($key eq 'permalink') {
163 if (safeurl($value)) {
164 push @{$metaheaders{$page}}, scrub('<link rel="bookmark" href="'.encode_entities($value).'" />', $destpage);
167 elsif ($key eq 'stylesheet') {
168 my $rel=exists $params{rel} ? $params{rel} : "alternate stylesheet";
169 my $title=exists $params{title} ? $params{title} : $value;
170 # adding .css to the value prevents using any old web
171 # editable page as a stylesheet
172 my $stylesheet=bestlink($page, $value.".css");
173 if (! length $stylesheet) {
174 error gettext("stylesheet not found")
176 push @{$metaheaders{$page}}, '<link href="'.urlto($stylesheet, $page).
177 '" rel="'.encode_entities($rel).
178 '" title="'.encode_entities($title).
179 "\" type=\"text/css\" />";
181 elsif ($key eq 'openid') {
182 my $delegate=0; # both by default
183 if (exists $params{delegate}) {
184 $delegate = 1 if lc $params{delegate} eq 'openid';
185 $delegate = 2 if lc $params{delegate} eq 'openid2';
187 if (exists $params{server} && safeurl($params{server})) {
188 push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
189 '" rel="openid.server" />' if $delegate ne 2;
190 push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
191 '" rel="openid2.provider" />' if $delegate ne 1;
193 if (safeurl($value)) {
194 push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
195 '" rel="openid.delegate" />' if $delegate ne 2;
196 push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
197 '" rel="openid2.local_id" />' if $delegate ne 1;
199 if (exists $params{"xrds-location"} && safeurl($params{"xrds-location"})) {
200 push @{$metaheaders{$page}}, '<meta http-equiv="X-XRDS-Location"'.
201 'content="'.encode_entities($params{"xrds-location"}).'" />';
204 elsif ($key eq 'redir') {
205 return "" if $page ne $destpage;
207 if ($value !~ /^\w+:\/\//) {
208 my ($redir_page, $redir_anchor) = split /\#/, $value;
210 my $link=bestlink($page, $redir_page);
211 if (! length $link) {
212 error gettext("redir page not found")
214 add_depends($page, $link, deptype("presence"));
216 $value=urlto($link, $page);
217 $value.='#'.$redir_anchor if defined $redir_anchor;
220 # redir cycle detection
221 $pagestate{$page}{meta}{redir}=$link;
224 while (exists $pagestate{$at}{meta}{redir}) {
226 error gettext("redir cycle is not allowed")
229 $at=$pagestate{$at}{meta}{redir};
233 $value=encode_entities($value);
235 my $delay=int(exists $params{delay} ? $params{delay} : 0);
236 my $redir="<meta http-equiv=\"refresh\" content=\"$delay; URL=$value\" />";
238 $redir=scrub($redir, $destpage);
240 push @{$metaheaders{$page}}, $redir;
242 elsif ($key eq 'link') {
244 push @{$metaheaders{$page}}, scrub("<link href=\"".encode_entities($value)."\" ".
246 encode_entities($_)."=\"".encode_entities(decode_entities($params{$_}))."\""
251 elsif ($key eq 'robots') {
252 push @{$metaheaders{$page}}, '<meta name="robots"'.
253 ' content="'.encode_entities($value).'" />';
255 elsif ($key eq 'description') {
256 push @{$metaheaders{$page}}, '<meta name="'.encode_entities($key).
257 '" content="'.encode_entities($value).'" />';
260 push @{$metaheaders{$page}}, scrub('<meta name="'.encode_entities($key).
261 '" content="'.encode_entities($value).'" />', $destpage);
267 sub pagetemplate (@) {
269 my $page=$params{page};
270 my $destpage=$params{destpage};
271 my $template=$params{template};
273 if (exists $metaheaders{$page} && $template->query(name => "meta")) {
274 # avoid duplicate meta lines
276 $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$metaheaders{$page}}));
278 if (exists $pagestate{$page}{meta}{title} && $template->query(name => "title")) {
279 $template->param(title => $pagestate{$page}{meta}{title});
280 $template->param(title_overridden => 1);
283 foreach my $field (qw{author authorurl description permalink}) {
284 $template->param($field => $pagestate{$page}{meta}{$field})
285 if exists $pagestate{$page}{meta}{$field} && $template->query(name => $field);
288 foreach my $field (qw{license copyright}) {
289 if (exists $pagestate{$page}{meta}{$field} && $template->query(name => $field) &&
290 ($page eq $destpage || ! exists $pagestate{$destpage}{meta}{$field} ||
291 $pagestate{$page}{meta}{$field} ne $pagestate{$destpage}{meta}{$field})) {
292 $template->param($field => htmlize($page, $destpage, $pagestate{$page}{meta}{$field}));
301 # e.g. titlesort (also makes sense for author)
302 my $key = $pagestate{$page}{meta}{$meta . "sort"};
303 return $key if defined $key;
306 $key = $pagestate{$page}{meta}{$meta};
307 return $key if defined $key;
309 # fall back to closer-to-core things
310 if ($meta eq 'title') {
311 return pagetitle(IkiWiki::basename($page));
313 elsif ($meta eq 'date') {
314 return $IkiWiki::pagectime{$page};
316 elsif ($meta eq 'updated') {
317 return $IkiWiki::pagemtime{$page};
328 # turn glob into a safe regexp
329 my $re=IkiWiki::glob2re(shift);
332 if (exists $pagestate{$page}{meta}{$field}) {
333 $val=$pagestate{$page}{meta}{$field};
335 elsif ($field eq 'title') {
336 $val = pagetitle($page);
340 if ($val=~/^$re$/i) {
341 return IkiWiki::SuccessReason->new("$re matches $field of $page", $page => $IkiWiki::DEPEND_CONTENT, "" => 1);
344 return IkiWiki::FailReason->new("$re does not match $field of $page", "" => 1);
348 return IkiWiki::FailReason->new("$page does not have a $field", "" => 1);
352 package IkiWiki::PageSpec;
354 sub match_title ($$;@) {
355 IkiWiki::Plugin::meta::match("title", @_);
358 sub match_author ($$;@) {
359 IkiWiki::Plugin::meta::match("author", @_);
362 sub match_authorurl ($$;@) {
363 IkiWiki::Plugin::meta::match("authorurl", @_);
366 sub match_license ($$;@) {
367 IkiWiki::Plugin::meta::match("license", @_);
370 sub match_copyright ($$;@) {
371 IkiWiki::Plugin::meta::match("copyright", @_);
374 package IkiWiki::SortSpec;
380 error(gettext("sort=meta requires a parameter")) unless defined $meta;
382 if ($meta eq 'updated' || $meta eq 'date') {
383 return IkiWiki::Plugin::meta::get_sort_key($left, $meta)
385 IkiWiki::Plugin::meta::get_sort_key($right, $meta);
388 return IkiWiki::Plugin::meta::get_sort_key($left, $meta)
390 IkiWiki::Plugin::meta::get_sort_key($right, $meta);
393 # A prototype of how sort=title could behave in 4.0 or something