]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/meta.pm
minor style etc changes
[git.ikiwiki.info.git] / IkiWiki / Plugin / meta.pm
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);
16 }
18 sub getsetup () {
19         return
20                 plugin => {
21                         safe => 1,
22                         rebuild => undef,
23                         section => "core",
24                 },
25 }
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};
37                         }
38                 }
39         }
40 }
42 sub scrub ($$) {
43         if (IkiWiki::Plugin::htmlscrubber->can("sanitize")) {
44                 return IkiWiki::Plugin::htmlscrubber::sanitize(
45                         content => shift, destpage => shift);
46         }
47         else {
48                 return shift;
49         }
50 }
52 sub safeurl ($) {
53         my $url=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/;
57         }
58         else {
59                 return 1;
60         }
61 }
63 sub htmlize ($$$) {
64         my $page = shift;
65         my $destpage = shift;
67         return IkiWiki::htmlize($page, $destpage, pagetype($pagesources{$page}),
68                 IkiWiki::linkify($page, $destpage,
69                 IkiWiki::preprocess($page, $destpage, shift)));
70 }
72 sub preprocess (@) {
73         return "" unless @_;
74         my %params=@_;
75         my $key=shift;
76         my $value=$params{$key};
77         delete $params{$key};
78         my $page=$params{page};
79         delete $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
86         # fully encoded.
87         $value=decode_entities($value);
89         # Metadata collection that needs to happen during the scan pass.
90         if ($key eq 'title') {
91                 my $encoded = HTML::Entities::encode_numeric($value);
92                 $pagestate{$page}{meta}{title} = $encoded;
94                 if (exists $params{sortas}) {
95                         $pagestate{$page}{meta}{titlesort}=$params{sortas};
96                 }
97                 elsif ($encoded ne $value) {
98                         $pagestate{$page}{meta}{titlesort}=$value;
99                 }
100                 else {
101                         delete $pagestate{$page}{meta}{titlesort};
102                 }
103                 return "";
104         }
105         elsif ($key eq 'description') {
106                 $pagestate{$page}{meta}{description}=HTML::Entities::encode_numeric($value);
107                 # fallthrough
108         }
109         elsif ($key eq 'guid') {
110                 $pagestate{$page}{meta}{guid}=HTML::Entities::encode_numeric($value);
111                 # fallthrough
112         }
113         elsif ($key eq 'license') {
114                 push @{$metaheaders{$page}}, '<link rel="license" href="#page_license" />';
115                 $pagestate{$page}{meta}{license}=$value;
116                 return "";
117         }
118         elsif ($key eq 'copyright') {
119                 push @{$metaheaders{$page}}, '<link rel="copyright" href="#page_copyright" />';
120                 $pagestate{$page}{meta}{copyright}=$value;
121                 return "";
122         }
123         elsif ($key eq 'link' && ! %params) {
124                 # hidden WikiLink
125                 add_link($page, $value);
126                 return "";
127         }
128         elsif ($key eq 'author') {
129                 $pagestate{$page}{meta}{author}=$value;
130                 if (exists $params{sortas}) {
131                         $pagestate{$page}{meta}{authorsort}=$params{sortas};
132                 }
133                 else {
134                         delete $pagestate{$page}{meta}{authorsort};
135                 }
136                 # fallthorough
137         }
138         elsif ($key eq 'authorurl') {
139                 $pagestate{$page}{meta}{authorurl}=$value if safeurl($value);
140                 # fallthrough
141         }
142         elsif ($key eq 'permalink') {
143                 $pagestate{$page}{meta}{permalink}=$value if safeurl($value);
144                 # fallthrough
145         }
146         elsif ($key eq 'date') {
147                 eval q{use Date::Parse};
148                 if (! $@) {
149                         my $time = str2time($value);
150                         $IkiWiki::pagectime{$page}=$time if defined $time;
151                 }
152         }
153         elsif ($key eq 'updated') {
154                 eval q{use Date::Parse};
155                 if (! $@) {
156                         my $time = str2time($value);
157                         $pagestate{$page}{meta}{updated}=$time if defined $time;
158                 }
159         }
161         if (! defined wantarray) {
162                 # avoid collecting duplicate data during scan pass
163                 return;
164         }
166         # Metadata handling that happens only during preprocessing pass.
167         if ($key eq 'permalink') {
168                 if (safeurl($value)) {
169                         push @{$metaheaders{$page}}, scrub('<link rel="bookmark" href="'.encode_entities($value).'" />', $destpage);
170                 }
171         }
172         elsif ($key eq 'stylesheet') {
173                 my $rel=exists $params{rel} ? $params{rel} : "alternate stylesheet";
174                 my $title=exists $params{title} ? $params{title} : $value;
175                 # adding .css to the value prevents using any old web
176                 # editable page as a stylesheet
177                 my $stylesheet=bestlink($page, $value.".css");
178                 if (! length $stylesheet) {
179                         error gettext("stylesheet not found")
180                 }
181                 push @{$metaheaders{$page}}, '<link href="'.urlto($stylesheet, $page).
182                         '" rel="'.encode_entities($rel).
183                         '" title="'.encode_entities($title).
184                         "\" type=\"text/css\" />";
185         }
186         elsif ($key eq 'openid') {
187                 my $delegate=0; # both by default
188                 if (exists $params{delegate}) {
189                         $delegate = 1 if lc $params{delegate} eq 'openid';
190                         $delegate = 2 if lc $params{delegate} eq 'openid2';
191                 }
192                 if (exists $params{server} && safeurl($params{server})) {
193                         push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
194                                 '" rel="openid.server" />' if $delegate ne 2;
195                         push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
196                                 '" rel="openid2.provider" />' if $delegate ne 1;
197                 }
198                 if (safeurl($value)) {
199                         push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
200                                 '" rel="openid.delegate" />' if $delegate ne 2;
201                         push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
202                                 '" rel="openid2.local_id" />' if $delegate ne 1;
203                 }
204                 if (exists $params{"xrds-location"} && safeurl($params{"xrds-location"})) {
205                         push @{$metaheaders{$page}}, '<meta http-equiv="X-XRDS-Location"'.
206                                 'content="'.encode_entities($params{"xrds-location"}).'" />';
207                 }
208         }
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")
218                         }
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")
232                                 }
233                                 $seen{$at}=1;
234                                 $at=$pagestate{$at}{meta}{redir};
235                         }
236                 }
237                 else {
238                         $value=encode_entities($value);
239                 }
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, $destpage);
244                 }
245                 push @{$metaheaders{$page}}, $redir;
246         }
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", $destpage);
254                 }
255         }
256         elsif ($key eq 'robots') {
257                 push @{$metaheaders{$page}}, '<meta name="robots"'.
258                         ' content="'.encode_entities($value).'" />';
259         }
260         elsif ($key eq 'description') {
261                 push @{$metaheaders{$page}}, '<meta name="'.encode_entities($key).
262                         '" content="'.encode_entities($value).'" />';
263         }
264         else {
265                 push @{$metaheaders{$page}}, scrub('<meta name="'.encode_entities($key).
266                         '" content="'.encode_entities($value).'" />', $destpage);
267         }
269         return "";
272 sub pagetemplate (@) {
273         my %params=@_;
274         my $page=$params{page};
275         my $destpage=$params{destpage};
276         my $template=$params{template};
278         if (exists $metaheaders{$page} && $template->query(name => "meta")) {
279                 # avoid duplicate meta lines
280                 my %seen;
281                 $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$metaheaders{$page}}));
282         }
283         if (exists $pagestate{$page}{meta}{title} && $template->query(name => "title")) {
284                 $template->param(title => $pagestate{$page}{meta}{title});
285                 $template->param(title_overridden => 1);
286         }
288         foreach my $field (qw{author authorurl description permalink}) {
289                 $template->param($field => $pagestate{$page}{meta}{$field})
290                         if exists $pagestate{$page}{meta}{$field} && $template->query(name => $field);
291         }
293         foreach my $field (qw{license copyright}) {
294                 if (exists $pagestate{$page}{meta}{$field} && $template->query(name => $field) &&
295                     ($page eq $destpage || ! exists $pagestate{$destpage}{meta}{$field} ||
296                      $pagestate{$page}{meta}{$field} ne $pagestate{$destpage}{meta}{$field})) {
297                         $template->param($field => htmlize($page, $destpage, $pagestate{$page}{meta}{$field}));
298                 }
299         }
302 sub get_sort_key {
303         my $page = shift;
304         my $meta = shift;
306         # e.g. titlesort (also makes sense for author)
307         my $key = $pagestate{$page}{meta}{$meta . "sort"};
308         return $key if defined $key;
310         # e.g. title
311         $key = $pagestate{$page}{meta}{$meta};
312         return $key if defined $key;
314         # fall back to closer-to-core things
315         if ($meta eq 'title') {
316                 return pagetitle(IkiWiki::basename($page));
317         }
318         elsif ($meta eq 'date') {
319                 return $IkiWiki::pagectime{$page};
320         }
321         elsif ($meta eq 'updated') {
322                 return $IkiWiki::pagemtime{$page};
323         }
324         else {
325                 return '';
326         }
329 sub match {
330         my $field=shift;
331         my $page=shift;
332         
333         # turn glob into a safe regexp
334         my $re=IkiWiki::glob2re(shift);
336         my $val;
337         if (exists $pagestate{$page}{meta}{$field}) {
338                 $val=$pagestate{$page}{meta}{$field};
339         }
340         elsif ($field eq 'title') {
341                 $val = pagetitle($page);
342         }
344         if (defined $val) {
345                 if ($val=~/^$re$/i) {
346                         return IkiWiki::SuccessReason->new("$re matches $field of $page", $page => $IkiWiki::DEPEND_CONTENT, "" => 1);
347                 }
348                 else {
349                         return IkiWiki::FailReason->new("$re does not match $field of $page", "" => 1);
350                 }
351         }
352         else {
353                 return IkiWiki::FailReason->new("$page does not have a $field", "" => 1);
354         }
357 package IkiWiki::PageSpec;
359 sub match_title ($$;@) {
360         IkiWiki::Plugin::meta::match("title", @_);
363 sub match_author ($$;@) {
364         IkiWiki::Plugin::meta::match("author", @_);
367 sub match_authorurl ($$;@) {
368         IkiWiki::Plugin::meta::match("authorurl", @_);
371 sub match_license ($$;@) {
372         IkiWiki::Plugin::meta::match("license", @_);
375 sub match_copyright ($$;@) {
376         IkiWiki::Plugin::meta::match("copyright", @_);
379 package IkiWiki::SortSpec;
381 sub cmp_meta {
382         my $meta = shift;
383         error(gettext("sort=meta requires a parameter")) unless defined $meta;
385         if ($meta eq 'updated' || $meta eq 'date') {
386                 return IkiWiki::Plugin::meta::get_sort_key($a, $meta)
387                         <=>
388                         IkiWiki::Plugin::meta::get_sort_key($b, $meta);
389         }
391         return IkiWiki::Plugin::meta::get_sort_key($a, $meta)
392                 cmp
393                 IkiWiki::Plugin::meta::get_sort_key($b, $meta);
396 # A prototype of how sort=title could behave in 4.0 or something
397 sub cmp_meta_title {
398         $_[0] = 'title';
399         return cmp_meta(@_);