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