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);
16 hook(type => "sort", id => "meta_title", call => \&sort_meta_title);
30 foreach my $page (keys %pagestate) {
31 if (exists $pagestate{$page}{meta}) {
32 if (exists $pagesources{$page} &&
33 grep { $_ eq $pagesources{$page} } @$needsbuild) {
34 # remove state, it will be re-added
35 # if the preprocessor directive is still
36 # there during the rebuild
37 delete $pagestate{$page}{meta};
44 if (IkiWiki::Plugin::htmlscrubber->can("sanitize")) {
45 return IkiWiki::Plugin::htmlscrubber::sanitize(
46 content => shift, destpage => shift);
55 if (exists $IkiWiki::Plugin::htmlscrubber::{safe_url_regexp} &&
56 defined $IkiWiki::Plugin::htmlscrubber::safe_url_regexp) {
57 return $url=~/$IkiWiki::Plugin::htmlscrubber::safe_url_regexp/;
68 return IkiWiki::htmlize($page, $destpage, pagetype($pagesources{$page}),
69 IkiWiki::linkify($page, $destpage,
70 IkiWiki::preprocess($page, $destpage, shift)));
77 my $value=$params{$key};
79 my $page=$params{page};
81 my $destpage=$params{destpage};
82 delete $params{destpage};
83 delete $params{preview};
85 eval q{use HTML::Entities};
86 # Always decode, even if encoding later, since it might not be
88 $value=decode_entities($value);
90 # Metadata collection that needs to happen during the scan pass.
91 if ($key eq 'title') {
92 $pagestate{$page}{meta}{title}=HTML::Entities::encode_numeric($value);
95 elsif ($key eq 'description') {
96 $pagestate{$page}{meta}{description}=HTML::Entities::encode_numeric($value);
99 elsif ($key eq 'guid') {
100 $pagestate{$page}{meta}{guid}=HTML::Entities::encode_numeric($value);
103 elsif ($key eq 'license') {
104 push @{$metaheaders{$page}}, '<link rel="license" href="#page_license" />';
105 $pagestate{$page}{meta}{license}=$value;
108 elsif ($key eq 'copyright') {
109 push @{$metaheaders{$page}}, '<link rel="copyright" href="#page_copyright" />';
110 $pagestate{$page}{meta}{copyright}=$value;
113 elsif ($key eq 'link' && ! %params) {
115 add_link($page, $value);
118 elsif ($key eq 'author') {
119 $pagestate{$page}{meta}{author}=$value;
122 elsif ($key eq 'authorurl') {
123 $pagestate{$page}{meta}{authorurl}=$value if safeurl($value);
126 elsif ($key eq 'permalink') {
127 $pagestate{$page}{meta}{permalink}=$value if safeurl($value);
130 elsif ($key eq 'date') {
131 eval q{use Date::Parse};
133 my $time = str2time($value);
134 $IkiWiki::pagectime{$page}=$time if defined $time;
137 elsif ($key eq 'updated') {
138 eval q{use Date::Parse};
140 my $time = str2time($value);
141 $pagestate{$page}{meta}{updated}=$time if defined $time;
145 if (! defined wantarray) {
146 # avoid collecting duplicate data during scan pass
150 # Metadata handling that happens only during preprocessing pass.
151 if ($key eq 'permalink') {
152 if (safeurl($value)) {
153 push @{$metaheaders{$page}}, scrub('<link rel="bookmark" href="'.encode_entities($value).'" />', $destpage);
156 elsif ($key eq 'stylesheet') {
157 my $rel=exists $params{rel} ? $params{rel} : "alternate stylesheet";
158 my $title=exists $params{title} ? $params{title} : $value;
159 # adding .css to the value prevents using any old web
160 # editable page as a stylesheet
161 my $stylesheet=bestlink($page, $value.".css");
162 if (! length $stylesheet) {
163 error gettext("stylesheet not found")
165 push @{$metaheaders{$page}}, '<link href="'.urlto($stylesheet, $page).
166 '" rel="'.encode_entities($rel).
167 '" title="'.encode_entities($title).
168 "\" type=\"text/css\" />";
170 elsif ($key eq 'openid') {
171 my $delegate=0; # both by default
172 if (exists $params{delegate}) {
173 $delegate = 1 if lc $params{delegate} eq 'openid';
174 $delegate = 2 if lc $params{delegate} eq 'openid2';
176 if (exists $params{server} && safeurl($params{server})) {
177 push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
178 '" rel="openid.server" />' if $delegate ne 2;
179 push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
180 '" rel="openid2.provider" />' if $delegate ne 1;
182 if (safeurl($value)) {
183 push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
184 '" rel="openid.delegate" />' if $delegate ne 2;
185 push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
186 '" rel="openid2.local_id" />' if $delegate ne 1;
188 if (exists $params{"xrds-location"} && safeurl($params{"xrds-location"})) {
189 push @{$metaheaders{$page}}, '<meta http-equiv="X-XRDS-Location"'.
190 'content="'.encode_entities($params{"xrds-location"}).'" />';
193 elsif ($key eq 'redir') {
194 return "" if $page ne $destpage;
196 if ($value !~ /^\w+:\/\//) {
197 my ($redir_page, $redir_anchor) = split /\#/, $value;
199 my $link=bestlink($page, $redir_page);
200 if (! length $link) {
201 error gettext("redir page not found")
203 add_depends($page, $link, deptype("presence"));
205 $value=urlto($link, $page);
206 $value.='#'.$redir_anchor if defined $redir_anchor;
209 # redir cycle detection
210 $pagestate{$page}{meta}{redir}=$link;
213 while (exists $pagestate{$at}{meta}{redir}) {
215 error gettext("redir cycle is not allowed")
218 $at=$pagestate{$at}{meta}{redir};
222 $value=encode_entities($value);
224 my $delay=int(exists $params{delay} ? $params{delay} : 0);
225 my $redir="<meta http-equiv=\"refresh\" content=\"$delay; URL=$value\" />";
227 $redir=scrub($redir, $destpage);
229 push @{$metaheaders{$page}}, $redir;
231 elsif ($key eq 'link') {
233 push @{$metaheaders{$page}}, scrub("<link href=\"".encode_entities($value)."\" ".
235 encode_entities($_)."=\"".encode_entities(decode_entities($params{$_}))."\""
240 elsif ($key eq 'robots') {
241 push @{$metaheaders{$page}}, '<meta name="robots"'.
242 ' content="'.encode_entities($value).'" />';
244 elsif ($key eq 'description') {
245 push @{$metaheaders{$page}}, '<meta name="'.encode_entities($key).
246 '" content="'.encode_entities($value).'" />';
249 push @{$metaheaders{$page}}, scrub('<meta name="'.encode_entities($key).
250 '" content="'.encode_entities($value).'" />', $destpage);
256 sub pagetemplate (@) {
258 my $page=$params{page};
259 my $destpage=$params{destpage};
260 my $template=$params{template};
262 if (exists $metaheaders{$page} && $template->query(name => "meta")) {
263 # avoid duplicate meta lines
265 $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$metaheaders{$page}}));
267 if (exists $pagestate{$page}{meta}{title} && $template->query(name => "title")) {
268 $template->param(title => $pagestate{$page}{meta}{title});
269 $template->param(title_overridden => 1);
272 foreach my $field (qw{author authorurl description permalink}) {
273 $template->param($field => $pagestate{$page}{meta}{$field})
274 if exists $pagestate{$page}{meta}{$field} && $template->query(name => $field);
277 foreach my $field (qw{license copyright}) {
278 if (exists $pagestate{$page}{meta}{$field} && $template->query(name => $field) &&
279 ($page eq $destpage || ! exists $pagestate{$destpage}{meta}{$field} ||
280 $pagestate{$page}{meta}{$field} ne $pagestate{$destpage}{meta}{$field})) {
281 $template->param($field => htmlize($page, $destpage, $pagestate{$page}{meta}{$field}));
287 my $title = $pagestate{$_[0]}{meta}{title};
289 if (defined $title) {
293 return pagetitle(IkiWiki::basename($_[0]));
296 sub sort_meta_title {
297 return title($_[0]) cmp title($_[1]);
304 # turn glob into a safe regexp
305 my $re=IkiWiki::glob2re(shift);
308 if (exists $pagestate{$page}{meta}{$field}) {
309 $val=$pagestate{$page}{meta}{$field};
311 elsif ($field eq 'title') {
312 $val = pagetitle($page);
316 if ($val=~/^$re$/i) {
317 return IkiWiki::SuccessReason->new("$re matches $field of $page", $page => $IkiWiki::DEPEND_CONTENT, "" => 1);
320 return IkiWiki::FailReason->new("$re does not match $field of $page", "" => 1);
324 return IkiWiki::FailReason->new("$page does not have a $field", "" => 1);
328 package IkiWiki::PageSpec;
330 sub match_title ($$;@) {
331 IkiWiki::Plugin::meta::match("title", @_);
334 sub match_author ($$;@) {
335 IkiWiki::Plugin::meta::match("author", @_);
338 sub match_authorurl ($$;@) {
339 IkiWiki::Plugin::meta::match("authorurl", @_);
342 sub match_license ($$;@) {
343 IkiWiki::Plugin::meta::match("license", @_);
346 sub match_copyright ($$;@) {
347 IkiWiki::Plugin::meta::match("copyright", @_);