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