]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/meta.pm
response
[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                 $pagestate{$page}{meta}{title}=HTML::Entities::encode_numeric($value);
92                 return "";
93         }
94         elsif ($key eq 'description') {
95                 $pagestate{$page}{meta}{description}=HTML::Entities::encode_numeric($value);
96                 # fallthrough
97         }
98         elsif ($key eq 'guid') {
99                 $pagestate{$page}{meta}{guid}=HTML::Entities::encode_numeric($value);
100                 # fallthrough
101         }
102         elsif ($key eq 'license') {
103                 push @{$metaheaders{$page}}, '<link rel="license" href="#page_license" />';
104                 $pagestate{$page}{meta}{license}=$value;
105                 return "";
106         }
107         elsif ($key eq 'copyright') {
108                 push @{$metaheaders{$page}}, '<link rel="copyright" href="#page_copyright" />';
109                 $pagestate{$page}{meta}{copyright}=$value;
110                 return "";
111         }
112         elsif ($key eq 'link' && ! %params) {
113                 # hidden WikiLink
114                 add_link($page, $value);
115                 return "";
116         }
117         elsif ($key eq 'author') {
118                 $pagestate{$page}{meta}{author}=$value;
119                 # fallthorough
120         }
121         elsif ($key eq 'authorurl') {
122                 $pagestate{$page}{meta}{authorurl}=$value if safeurl($value);
123                 # fallthrough
124         }
125         elsif ($key eq 'permalink') {
126                 $pagestate{$page}{meta}{permalink}=$value if safeurl($value);
127                 # fallthrough
128         }
129         elsif ($key eq 'date') {
130                 eval q{use Date::Parse};
131                 if (! $@) {
132                         my $time = str2time($value);
133                         $IkiWiki::pagectime{$page}=$time if defined $time;
134                 }
135         }
136         elsif ($key eq 'updated') {
137                 eval q{use Date::Parse};
138                 if (! $@) {
139                         my $time = str2time($value);
140                         $pagestate{$page}{meta}{updated}=$time if defined $time;
141                 }
142         }
144         if (! defined wantarray) {
145                 # avoid collecting duplicate data during scan pass
146                 return;
147         }
149         # Metadata handling that happens only during preprocessing pass.
150         if ($key eq 'permalink') {
151                 if (safeurl($value)) {
152                         push @{$metaheaders{$page}}, scrub('<link rel="bookmark" href="'.encode_entities($value).'" />', $destpage);
153                 }
154         }
155         elsif ($key eq 'stylesheet') {
156                 my $rel=exists $params{rel} ? $params{rel} : "alternate stylesheet";
157                 my $title=exists $params{title} ? $params{title} : $value;
158                 # adding .css to the value prevents using any old web
159                 # editable page as a stylesheet
160                 my $stylesheet=bestlink($page, $value.".css");
161                 if (! length $stylesheet) {
162                         error gettext("stylesheet not found")
163                 }
164                 push @{$metaheaders{$page}}, '<link href="'.urlto($stylesheet, $page).
165                         '" rel="'.encode_entities($rel).
166                         '" title="'.encode_entities($title).
167                         "\" type=\"text/css\" />";
168         }
169         elsif ($key eq 'openid') {
170                 my $delegate=0; # both by default
171                 if (exists $params{delegate}) {
172                         $delegate = 1 if lc $params{delegate} eq 'openid';
173                         $delegate = 2 if lc $params{delegate} eq 'openid2';
174                 }
175                 if (exists $params{server} && safeurl($params{server})) {
176                         push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
177                                 '" rel="openid.server" />' if $delegate ne 2;
178                         push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
179                                 '" rel="openid2.provider" />' if $delegate ne 1;
180                 }
181                 if (safeurl($value)) {
182                         push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
183                                 '" rel="openid.delegate" />' if $delegate ne 2;
184                         push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
185                                 '" rel="openid2.local_id" />' if $delegate ne 1;
186                 }
187                 if (exists $params{"xrds-location"} && safeurl($params{"xrds-location"})) {
188                         push @{$metaheaders{$page}}, '<meta http-equiv="X-XRDS-Location"'.
189                                 'content="'.encode_entities($params{"xrds-location"}).'" />';
190                 }
191         }
192         elsif ($key eq 'redir') {
193                 return "" if $page ne $destpage;
194                 my $safe=0;
195                 if ($value !~ /^\w+:\/\//) {
196                         my ($redir_page, $redir_anchor) = split /\#/, $value;
198                         my $link=bestlink($page, $redir_page);
199                         if (! length $link) {
200                                 error gettext("redir page not found")
201                         }
202                         add_depends($page, $link, deptype("presence"));
204                         $value=urlto($link, $page);
205                         $value.='#'.$redir_anchor if defined $redir_anchor;
206                         $safe=1;
208                         # redir cycle detection
209                         $pagestate{$page}{meta}{redir}=$link;
210                         my $at=$page;
211                         my %seen;
212                         while (exists $pagestate{$at}{meta}{redir}) {
213                                 if ($seen{$at}) {
214                                         error gettext("redir cycle is not allowed")
215                                 }
216                                 $seen{$at}=1;
217                                 $at=$pagestate{$at}{meta}{redir};
218                         }
219                 }
220                 else {
221                         $value=encode_entities($value);
222                 }
223                 my $delay=int(exists $params{delay} ? $params{delay} : 0);
224                 my $redir="<meta http-equiv=\"refresh\" content=\"$delay; URL=$value\" />";
225                 if (! $safe) {
226                         $redir=scrub($redir, $destpage);
227                 }
228                 push @{$metaheaders{$page}}, $redir;
229         }
230         elsif ($key eq 'link') {
231                 if (%params) {
232                         push @{$metaheaders{$page}}, scrub("<link href=\"".encode_entities($value)."\" ".
233                                 join(" ", map {
234                                         encode_entities($_)."=\"".encode_entities(decode_entities($params{$_}))."\""
235                                 } keys %params).
236                                 " />\n", $destpage);
237                 }
238         }
239         elsif ($key eq 'robots') {
240                 push @{$metaheaders{$page}}, '<meta name="robots"'.
241                         ' content="'.encode_entities($value).'" />';
242         }
243         elsif ($key eq 'description') {
244                 push @{$metaheaders{$page}}, '<meta name="'.encode_entities($key).
245                         '" content="'.encode_entities($value).'" />';
246         }
247         else {
248                 push @{$metaheaders{$page}}, scrub('<meta name="'.encode_entities($key).
249                         '" content="'.encode_entities($value).'" />', $destpage);
250         }
252         return "";
255 sub pagetemplate (@) {
256         my %params=@_;
257         my $page=$params{page};
258         my $destpage=$params{destpage};
259         my $template=$params{template};
261         if (exists $metaheaders{$page} && $template->query(name => "meta")) {
262                 # avoid duplicate meta lines
263                 my %seen;
264                 $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$metaheaders{$page}}));
265         }
266         if (exists $pagestate{$page}{meta}{title} && $template->query(name => "title")) {
267                 $template->param(title => $pagestate{$page}{meta}{title});
268                 $template->param(title_overridden => 1);
269         }
271         foreach my $field (qw{author authorurl description permalink}) {
272                 $template->param($field => $pagestate{$page}{meta}{$field})
273                         if exists $pagestate{$page}{meta}{$field} && $template->query(name => $field);
274         }
276         foreach my $field (qw{license copyright}) {
277                 if (exists $pagestate{$page}{meta}{$field} && $template->query(name => $field) &&
278                     ($page eq $destpage || ! exists $pagestate{$destpage}{meta}{$field} ||
279                      $pagestate{$page}{meta}{$field} ne $pagestate{$destpage}{meta}{$field})) {
280                         $template->param($field => htmlize($page, $destpage, $pagestate{$page}{meta}{$field}));
281                 }
282         }
285 sub match {
286         my $field=shift;
287         my $page=shift;
288         
289         # turn glob into a safe regexp
290         my $re=IkiWiki::glob2re(shift);
292         my $val;
293         if (exists $pagestate{$page}{meta}{$field}) {
294                 $val=$pagestate{$page}{meta}{$field};
295         }
296         elsif ($field eq 'title') {
297                 $val = pagetitle($page);
298         }
300         if (defined $val) {
301                 if ($val=~/^$re$/i) {
302                         return IkiWiki::SuccessReason->new("$re matches $field of $page", $page => $IkiWiki::DEPEND_CONTENT, "" => 1);
303                 }
304                 else {
305                         return IkiWiki::FailReason->new("$re does not match $field of $page", "" => 1);
306                 }
307         }
308         else {
309                 return IkiWiki::FailReason->new("$page does not have a $field", "" => 1);
310         }
313 package IkiWiki::PageSpec;
315 sub match_title ($$;@) {
316         IkiWiki::Plugin::meta::match("title", @_);
319 sub match_author ($$;@) {
320         IkiWiki::Plugin::meta::match("author", @_);
323 sub match_authorurl ($$;@) {
324         IkiWiki::Plugin::meta::match("authorurl", @_);
327 sub match_license ($$;@) {
328         IkiWiki::Plugin::meta::match("license", @_);
331 sub match_copyright ($$;@) {
332         IkiWiki::Plugin::meta::match("copyright", @_);