]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/meta.pm
releasing version 2.31.1
[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 htmlize ($$$) { #{{{
42         my $page = shift;
43         my $destpage = shift;
45         return IkiWiki::htmlize($page, pagetype($pagesources{$page}),
46                 IkiWiki::linkify($page, $destpage,
47                 IkiWiki::preprocess($page, $destpage, shift)));
48 }
50 sub preprocess (@) { #{{{
51         return "" unless @_;
52         my %params=@_;
53         my $key=shift;
54         my $value=$params{$key};
55         delete $params{$key};
56         my $page=$params{page};
57         delete $params{page};
58         my $destpage=$params{destpage};
59         delete $params{destpage};
60         delete $params{preview};
62         eval q{use HTML::Entities};
63         # Always decode, even if encoding later, since it might not be
64         # fully encoded.
65         $value=decode_entities($value);
67         # Metadata collection that needs to happen during the scan pass.
68         if ($key eq 'title') {
69                 $pagestate{$page}{meta}{title}=HTML::Entities::encode_numeric($value);
70         }
71         elsif ($key eq 'license') {
72                 push @{$metaheaders{$page}}, '<link rel="license" href="#page_license" />';
73                 $pagestate{$page}{meta}{license}=$value;
74                 return "";
75         }
76         elsif ($key eq 'copyright') {
77                 push @{$metaheaders{$page}}, '<link rel="copyright" href="#page_copyright" />';
78                 $pagestate{$page}{meta}{copyright}=$value;
79                 return "";
80         }
81         elsif ($key eq 'link' && ! %params) {
82                 # hidden WikiLink
83                 push @{$links{$page}}, $value;
84                 return "";
85         }
86         elsif ($key eq 'author') {
87                 $pagestate{$page}{meta}{author}=$value;
88                 # fallthorough
89         }
90         elsif ($key eq 'authorurl') {
91                 $pagestate{$page}{meta}{authorurl}=$value;
92                 # fallthrough
93         }
95         if (! defined wantarray) {
96                 # avoid collecting duplicate data during scan pass
97                 return;
98         }
100         # Metadata collection that happens only during preprocessing pass.
101         if ($key eq 'date') {
102                 eval q{use Date::Parse};
103                 if (! $@) {
104                         my $time = str2time($value);
105                         $IkiWiki::pagectime{$page}=$time if defined $time;
106                 }
107         }
108         elsif ($key eq 'permalink') {
109                 $pagestate{$page}{meta}{permalink}=$value;
110                 push @{$metaheaders{$page}}, scrub('<link rel="bookmark" href="'.encode_entities($value).'" />');
111         }
112         elsif ($key eq 'stylesheet') {
113                 my $rel=exists $params{rel} ? $params{rel} : "alternate stylesheet";
114                 my $title=exists $params{title} ? $params{title} : $value;
115                 # adding .css to the value prevents using any old web
116                 # editable page as a stylesheet
117                 my $stylesheet=bestlink($page, $value.".css");
118                 if (! length $stylesheet) {
119                         return "[[meta ".gettext("stylesheet not found")."]]";
120                 }
121                 push @{$metaheaders{$page}}, '<link href="'.urlto($stylesheet, $page).
122                         '" rel="'.encode_entities($rel).
123                         '" title="'.encode_entities($title).
124                         "\" type=\"text/css\" />";
125         }
126         elsif ($key eq 'openid') {
127                 if (exists $params{server}) {
128                         push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
129                                 '" rel="openid.server" />';
130                 }
131                 push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
132                         '" rel="openid.delegate" />';
133         }
134         elsif ($key eq 'redir') {
135                 return "" if $page ne $destpage;
136                 my $safe=0;
137                 if ($value !~ /^\w+:\/\//) {
138                         my ($redir_page, $redir_anchor) = split /\#/, $value;
140                         add_depends($page, $redir_page);
141                         my $link=bestlink($page, $redir_page);
142                         if (! length $link) {
143                                 return "[[meta ".gettext("redir page not found")."]]";
144                         }
146                         $value=urlto($link, $page);
147                         $value.='#'.$redir_anchor if defined $redir_anchor;
148                         $safe=1;
150                         # redir cycle detection
151                         $pagestate{$page}{meta}{redir}=$link;
152                         my $at=$page;
153                         my %seen;
154                         while (exists $pagestate{$at}{meta}{redir}) {
155                                 if ($seen{$at}) {
156                                         return "[[meta ".gettext("redir cycle is not allowed")."]]";
157                                 }
158                                 $seen{$at}=1;
159                                 $at=$pagestate{$at}{meta}{redir};
160                         }
161                 }
162                 else {
163                         $value=encode_entities($value);
164                 }
165                 my $delay=int(exists $params{delay} ? $params{delay} : 0);
166                 my $redir="<meta http-equiv=\"refresh\" content=\"$delay; URL=$value\">";
167                 if (! $safe) {
168                         $redir=scrub($redir);
169                 }
170                 push @{$metaheaders{$page}}, $redir;
171         }
172         elsif ($key eq 'link') {
173                 if (%params) {
174                         push @{$metaheaders{$page}}, scrub("<link href=\"".encode_entities($value)."\" ".
175                                 join(" ", map {
176                                         encode_entities($_)."=\"".encode_entities(decode_entities($params{$_}))."\""
177                                 } keys %params).
178                                 " />\n");
179                 }
180         }
181         else {
182                 push @{$metaheaders{$page}}, scrub('<meta name="'.encode_entities($key).
183                         '" content="'.encode_entities($value).'" />');
184         }
186         return "";
187 } # }}}
189 sub pagetemplate (@) { #{{{
190         my %params=@_;
191         my $page=$params{page};
192         my $destpage=$params{destpage};
193         my $template=$params{template};
195         if (exists $metaheaders{$page} && $template->query(name => "meta")) {
196                 # avoid duplicate meta lines
197                 my %seen;
198                 $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$metaheaders{$page}}));
199         }
200         if (exists $pagestate{$page}{meta}{title} && $template->query(name => "title")) {
201                 $template->param(title => $pagestate{$page}{meta}{title});
202                 $template->param(title_overridden => 1);
203         }
205         foreach my $field (qw{author authorurl permalink}) {
206                 $template->param($field => $pagestate{$page}{meta}{$field})
207                         if exists $pagestate{$page}{meta}{$field} && $template->query(name => $field);
208         }
210         foreach my $field (qw{license copyright}) {
211                 if (exists $pagestate{$page}{meta}{$field} && $template->query(name => $field) &&
212                     ($page eq $destpage || ! exists $pagestate{$destpage}{meta}{$field} ||
213                      $pagestate{$page}{meta}{$field} ne $pagestate{$destpage}{meta}{$field})) {
214                         $template->param($field => htmlize($page, $destpage, $pagestate{$page}{meta}{$field}));
215                 }
216         }
217 } # }}}
219 sub match { #{{{
220         my $field=shift;
221         my $page=shift;
222         
223         # turn glob into a safe regexp
224         my $re=quotemeta(shift);
225         $re=~s/\\\*/.*/g;
226         $re=~s/\\\?/./g;
228         my $val;
229         if (exists $pagestate{$page}{meta}{$field}) {
230                 $val=$pagestate{$page}{meta}{$field};
231         }
232         elsif ($field eq 'title') {
233                 $val=pagetitle($page);
234         }
236         if (defined $val) {
237                 if ($val=~/^$re$/i) {
238                         return IkiWiki::SuccessReason->new("$re matches $field of $page");
239                 }
240                 else {
241                         return IkiWiki::FailReason->new("$re does not match $field of $page");
242                 }
243         }
244         else {
245                 return IkiWiki::FailReason->new("$page does not have a $field");
246         }
247 } #}}}
249 package IkiWiki::PageSpec;
251 sub match_title ($$;@) { #{{{
252         IkiWiki::Plugin::meta::match("title", @_);      
253 } #}}}
255 sub match_author ($$;@) { #{{{
256         IkiWiki::Plugin::meta::match("author", @_);
257 } #}}}
259 sub match_authorurl ($$;@) { #{{{
260         IkiWiki::Plugin::meta::match("authorurl", @_);
261 } #}}}
263 sub match_license ($$;@) { #{{{
264         IkiWiki::Plugin::meta::match("license", @_);
265 } #}}}
267 sub match_copyright ($$;@) { #{{{
268         IkiWiki::Plugin::meta::match("copyright", @_);
269 } #}}}