]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/meta.pm
Merge commit 'upstream/master' into prv/po
[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                 },
24 }
26 sub needsbuild (@) {
27         my $needsbuild=shift;
28         foreach my $page (keys %pagestate) {
29                 if (exists $pagestate{$page}{meta}) {
30                         if (exists $pagesources{$page} &&
31                             grep { $_ eq $pagesources{$page} } @$needsbuild) {
32                                 # remove state, it will be re-added
33                                 # if the preprocessor directive is still
34                                 # there during the rebuild
35                                 delete $pagestate{$page}{meta};
36                         }
37                 }
38         }
39 }
41 sub scrub ($$) {
42         if (IkiWiki::Plugin::htmlscrubber->can("sanitize")) {
43                 return IkiWiki::Plugin::htmlscrubber::sanitize(
44                         content => shift, destpage => shift);
45         }
46         else {
47                 return shift;
48         }
49 }
51 sub safeurl ($) {
52         my $url=shift;
53         if (exists $IkiWiki::Plugin::htmlscrubber::{safe_url_regexp} &&
54             defined $IkiWiki::Plugin::htmlscrubber::safe_url_regexp) {
55                 return $url=~/$IkiWiki::Plugin::htmlscrubber::safe_url_regexp/;
56         }
57         else {
58                 return 1;
59         }
60 }
62 sub htmlize ($$$) {
63         my $page = shift;
64         my $destpage = shift;
66         return IkiWiki::htmlize($page, $destpage, pagetype($pagesources{$page}),
67                 IkiWiki::linkify($page, $destpage,
68                 IkiWiki::preprocess($page, $destpage, shift)));
69 }
71 sub preprocess (@) {
72         return "" unless @_;
73         my %params=@_;
74         my $key=shift;
75         my $value=$params{$key};
76         delete $params{$key};
77         my $page=$params{page};
78         delete $params{page};
79         my $destpage=$params{destpage};
80         delete $params{destpage};
81         delete $params{preview};
83         eval q{use HTML::Entities};
84         # Always decode, even if encoding later, since it might not be
85         # fully encoded.
86         $value=decode_entities($value);
88         # Metadata collection that needs to happen during the scan pass.
89         if ($key eq 'title') {
90                 $pagestate{$page}{meta}{title}=HTML::Entities::encode_numeric($value);
91                 # fallthrough
92         }
93         elsif ($key eq 'description') {
94                 $pagestate{$page}{meta}{description}=HTML::Entities::encode_numeric($value);
95                 # fallthrough
96         }
97         elsif ($key eq 'guid') {
98                 $pagestate{$page}{meta}{guid}=HTML::Entities::encode_numeric($value);
99                 # fallthrough
100         }
101         elsif ($key eq 'license') {
102                 push @{$metaheaders{$page}}, '<link rel="license" href="#page_license" />';
103                 $pagestate{$page}{meta}{license}=$value;
104                 return "";
105         }
106         elsif ($key eq 'copyright') {
107                 push @{$metaheaders{$page}}, '<link rel="copyright" href="#page_copyright" />';
108                 $pagestate{$page}{meta}{copyright}=$value;
109                 return "";
110         }
111         elsif ($key eq 'link' && ! %params) {
112                 # hidden WikiLink
113                 push @{$links{$page}}, $value;
114                 return "";
115         }
116         elsif ($key eq 'author') {
117                 $pagestate{$page}{meta}{author}=$value;
118                 # fallthorough
119         }
120         elsif ($key eq 'authorurl') {
121                 $pagestate{$page}{meta}{authorurl}=$value if safeurl($value);
122                 # fallthrough
123         }
124         elsif ($key eq 'date') {
125                 eval q{use Date::Parse};
126                 if (! $@) {
127                         my $time = str2time($value);
128                         $IkiWiki::pagectime{$page}=$time if defined $time;
129                 }
130         }
132         if (! defined wantarray) {
133                 # avoid collecting duplicate data during scan pass
134                 return;
135         }
137         # Metadata collection that happens only during preprocessing pass.
138         if ($key eq 'permalink') {
139                 if (safeurl($value)) {
140                         $pagestate{$page}{meta}{permalink}=$value;
141                         push @{$metaheaders{$page}}, scrub('<link rel="bookmark" href="'.encode_entities($value).'" />', $destpage);
142                 }
143         }
144         elsif ($key eq 'stylesheet') {
145                 my $rel=exists $params{rel} ? $params{rel} : "alternate stylesheet";
146                 my $title=exists $params{title} ? $params{title} : $value;
147                 # adding .css to the value prevents using any old web
148                 # editable page as a stylesheet
149                 my $stylesheet=bestlink($page, $value.".css");
150                 if (! length $stylesheet) {
151                         error gettext("stylesheet not found")
152                 }
153                 push @{$metaheaders{$page}}, '<link href="'.urlto($stylesheet, $page).
154                         '" rel="'.encode_entities($rel).
155                         '" title="'.encode_entities($title).
156                         "\" type=\"text/css\" />";
157         }
158         elsif ($key eq 'openid') {
159                 if (exists $params{server} && safeurl($params{server})) {
160                         push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
161                                 '" rel="openid.server" />';
162                         push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
163                                 '" rel="openid2.provider" />';
164                 }
165                 if (safeurl($value)) {
166                         push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
167                                 '" rel="openid.delegate" />';
168                         push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
169                                 '" rel="openid2.local_id" />';
170                 }
171                 if (exists $params{"xrds-location"} && safeurl($params{"xrds-location"})) {
172                         push @{$metaheaders{$page}}, '<meta http-equiv="X-XRDS-Location"'.
173                                 'content="'.encode_entities($params{"xrds-location"}).'" />';
174                 }
175         }
176         elsif ($key eq 'redir') {
177                 return "" if $page ne $destpage;
178                 my $safe=0;
179                 if ($value !~ /^\w+:\/\//) {
180                         my ($redir_page, $redir_anchor) = split /\#/, $value;
182                         add_depends($page, $redir_page);
183                         my $link=bestlink($page, $redir_page);
184                         if (! length $link) {
185                                 error gettext("redir page not found")
186                         }
188                         $value=urlto($link, $page);
189                         $value.='#'.$redir_anchor if defined $redir_anchor;
190                         $safe=1;
192                         # redir cycle detection
193                         $pagestate{$page}{meta}{redir}=$link;
194                         my $at=$page;
195                         my %seen;
196                         while (exists $pagestate{$at}{meta}{redir}) {
197                                 if ($seen{$at}) {
198                                         error gettext("redir cycle is not allowed")
199                                 }
200                                 $seen{$at}=1;
201                                 $at=$pagestate{$at}{meta}{redir};
202                         }
203                 }
204                 else {
205                         $value=encode_entities($value);
206                 }
207                 my $delay=int(exists $params{delay} ? $params{delay} : 0);
208                 my $redir="<meta http-equiv=\"refresh\" content=\"$delay; URL=$value\" />";
209                 if (! $safe) {
210                         $redir=scrub($redir, $destpage);
211                 }
212                 push @{$metaheaders{$page}}, $redir;
213         }
214         elsif ($key eq 'link') {
215                 if (%params) {
216                         push @{$metaheaders{$page}}, scrub("<link href=\"".encode_entities($value)."\" ".
217                                 join(" ", map {
218                                         encode_entities($_)."=\"".encode_entities(decode_entities($params{$_}))."\""
219                                 } keys %params).
220                                 " />\n", $destpage);
221                 }
222         }
223         elsif ($key eq 'robots') {
224                 push @{$metaheaders{$page}}, '<meta name="robots"'.
225                         ' content="'.encode_entities($value).'" />';
226         }
227         else {
228                 push @{$metaheaders{$page}}, scrub('<meta name="'.encode_entities($key).
229                         '" content="'.encode_entities($value).'" />', $destpage);
230         }
232         return "";
235 sub pagetemplate (@) {
236         my %params=@_;
237         my $page=$params{page};
238         my $destpage=$params{destpage};
239         my $template=$params{template};
241         if (exists $metaheaders{$page} && $template->query(name => "meta")) {
242                 # avoid duplicate meta lines
243                 my %seen;
244                 $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$metaheaders{$page}}));
245         }
246         if (exists $pagestate{$page}{meta}{title} && $template->query(name => "title")) {
247                 $template->param(title => $pagestate{$page}{meta}{title});
248                 $template->param(title_overridden => 1);
249         }
251         foreach my $field (qw{author authorurl permalink}) {
252                 $template->param($field => $pagestate{$page}{meta}{$field})
253                         if exists $pagestate{$page}{meta}{$field} && $template->query(name => $field);
254         }
256         foreach my $field (qw{license copyright}) {
257                 if (exists $pagestate{$page}{meta}{$field} && $template->query(name => $field) &&
258                     ($page eq $destpage || ! exists $pagestate{$destpage}{meta}{$field} ||
259                      $pagestate{$page}{meta}{$field} ne $pagestate{$destpage}{meta}{$field})) {
260                         $template->param($field => htmlize($page, $destpage, $pagestate{$page}{meta}{$field}));
261                 }
262         }
265 sub match {
266         my $field=shift;
267         my $page=shift;
268         
269         # turn glob into a safe regexp
270         my $re=IkiWiki::glob2re(shift);
272         my $val;
273         if (exists $pagestate{$page}{meta}{$field}) {
274                 $val=$pagestate{$page}{meta}{$field};
275         }
276         elsif ($field eq 'title') {
277                 $val = pagetitle($page);
278         }
280         if (defined $val) {
281                 if ($val=~/^$re$/i) {
282                         return IkiWiki::SuccessReason->new("$re matches $field of $page");
283                 }
284                 else {
285                         return IkiWiki::FailReason->new("$re does not match $field of $page");
286                 }
287         }
288         else {
289                 return IkiWiki::FailReason->new("$page does not have a $field");
290         }
293 package IkiWiki::PageSpec;
295 sub match_title ($$;@) {
296         IkiWiki::Plugin::meta::match("title", @_);      
299 sub match_author ($$;@) {
300         IkiWiki::Plugin::meta::match("author", @_);
303 sub match_authorurl ($$;@) {
304         IkiWiki::Plugin::meta::match("authorurl", @_);
307 sub match_license ($$;@) {
308         IkiWiki::Plugin::meta::match("license", @_);
311 sub match_copyright ($$;@) {
312         IkiWiki::Plugin::meta::match("copyright", @_);