2 # Ikiwiki metadata plugin.
3 package IkiWiki::Plugin::meta;
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);
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};
43 if (IkiWiki::Plugin::htmlscrubber->can("sanitize")) {
44 return IkiWiki::Plugin::htmlscrubber::sanitize(
45 content => shift, destpage => 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/;
67 return IkiWiki::htmlize($page, $destpage, pagetype($pagesources{$page}),
68 IkiWiki::linkify($page, $destpage,
69 IkiWiki::preprocess($page, $destpage, shift)));
76 my $value=$params{$key};
78 my $page=$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
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);
94 elsif ($key eq 'description') {
95 $pagestate{$page}{meta}{description}=HTML::Entities::encode_numeric($value);
98 elsif ($key eq 'guid') {
99 $pagestate{$page}{meta}{guid}=HTML::Entities::encode_numeric($value);
102 elsif ($key eq 'license') {
103 push @{$metaheaders{$page}}, '<link rel="license" href="#page_license" />';
104 $pagestate{$page}{meta}{license}=$value;
107 elsif ($key eq 'copyright') {
108 push @{$metaheaders{$page}}, '<link rel="copyright" href="#page_copyright" />';
109 $pagestate{$page}{meta}{copyright}=$value;
112 elsif ($key eq 'link' && ! %params) {
114 add_link($page, $value);
117 elsif ($key eq 'author') {
118 $pagestate{$page}{meta}{author}=$value;
121 elsif ($key eq 'authorurl') {
122 $pagestate{$page}{meta}{authorurl}=$value if safeurl($value);
125 elsif ($key eq 'permalink') {
126 $pagestate{$page}{meta}{permalink}=$value if safeurl($value);
129 elsif ($key eq 'date') {
130 eval q{use Date::Parse};
132 my $time = str2time($value);
133 $IkiWiki::pagectime{$page}=$time if defined $time;
136 elsif ($key eq 'updated') {
137 eval q{use Date::Parse};
139 my $time = str2time($value);
140 $pagestate{$page}{meta}{updated}=$time if defined $time;
144 if (! defined wantarray) {
145 # avoid collecting duplicate data during scan pass
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);
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")
164 push @{$metaheaders{$page}}, '<link href="'.urlto($stylesheet, $page).
165 '" rel="'.encode_entities($rel).
166 '" title="'.encode_entities($title).
167 "\" type=\"text/css\" />";
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';
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;
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;
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"}).'" />';
192 elsif ($key eq 'redir') {
193 return "" if $page ne $destpage;
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")
202 add_depends($page, $link, deptype("presence"));
204 $value=urlto($link, $page);
205 $value.='#'.$redir_anchor if defined $redir_anchor;
208 # redir cycle detection
209 $pagestate{$page}{meta}{redir}=$link;
212 while (exists $pagestate{$at}{meta}{redir}) {
214 error gettext("redir cycle is not allowed")
217 $at=$pagestate{$at}{meta}{redir};
221 $value=encode_entities($value);
223 my $delay=int(exists $params{delay} ? $params{delay} : 0);
224 my $redir="<meta http-equiv=\"refresh\" content=\"$delay; URL=$value\" />";
226 $redir=scrub($redir, $destpage);
228 push @{$metaheaders{$page}}, $redir;
230 elsif ($key eq 'link') {
232 push @{$metaheaders{$page}}, scrub("<link href=\"".encode_entities($value)."\" ".
234 encode_entities($_)."=\"".encode_entities(decode_entities($params{$_}))."\""
239 elsif ($key eq 'robots') {
240 push @{$metaheaders{$page}}, '<meta name="robots"'.
241 ' content="'.encode_entities($value).'" />';
243 elsif ($key eq 'description') {
244 push @{$metaheaders{$page}}, '<meta name="'.encode_entities($key).
245 '" content="'.encode_entities($value).'" />';
248 push @{$metaheaders{$page}}, scrub('<meta name="'.encode_entities($key).
249 '" content="'.encode_entities($value).'" />', $destpage);
255 sub pagetemplate (@) {
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
264 $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$metaheaders{$page}}));
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);
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);
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}));
289 # turn glob into a safe regexp
290 my $re=IkiWiki::glob2re(shift);
293 if (exists $pagestate{$page}{meta}{$field}) {
294 $val=$pagestate{$page}{meta}{$field};
296 elsif ($field eq 'title') {
297 $val = pagetitle($page);
301 if ($val=~/^$re$/i) {
302 return IkiWiki::SuccessReason->new("$re matches $field of $page", $page => $IkiWiki::DEPEND_CONTENT, "" => 1);
305 return IkiWiki::FailReason->new("$re does not match $field of $page", "" => 1);
309 return IkiWiki::FailReason->new("$page does not have a $field", "" => 1);
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", @_);