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);
28 foreach my $page (keys %pagestate) {
29 if (exists $pagestate{$page}{meta}) {
30 if (exists $pagesources{$page} &&
31 grep { $_ eq $pagesources{$page} } @$needsbuild) {
32 # remove state, it will be re-added
33 # if the preprocessor directive is still
34 # there during the rebuild
35 delete $pagestate{$page}{meta};
42 if (IkiWiki::Plugin::htmlscrubber->can("sanitize")) {
43 return IkiWiki::Plugin::htmlscrubber::sanitize(
44 content => shift, destpage => shift);
53 if (exists $IkiWiki::Plugin::htmlscrubber::{safe_url_regexp} &&
54 defined $IkiWiki::Plugin::htmlscrubber::safe_url_regexp) {
55 return $url=~/$IkiWiki::Plugin::htmlscrubber::safe_url_regexp/;
66 return IkiWiki::htmlize($page, $destpage, pagetype($pagesources{$page}),
67 IkiWiki::linkify($page, $destpage,
68 IkiWiki::preprocess($page, $destpage, shift)));
75 my $value=$params{$key};
77 my $page=$params{page};
79 my $destpage=$params{destpage};
80 delete $params{destpage};
81 delete $params{preview};
83 eval q{use HTML::Entities};
84 # Always decode, even if encoding later, since it might not be
86 $value=decode_entities($value);
88 # Metadata collection that needs to happen during the scan pass.
89 if ($key eq 'title') {
90 $pagestate{$page}{meta}{title}=HTML::Entities::encode_numeric($value);
93 elsif ($key eq 'description') {
94 $pagestate{$page}{meta}{description}=HTML::Entities::encode_numeric($value);
97 elsif ($key eq 'guid') {
98 $pagestate{$page}{meta}{guid}=HTML::Entities::encode_numeric($value);
101 elsif ($key eq 'license') {
102 push @{$metaheaders{$page}}, '<link rel="license" href="#page_license" />';
103 $pagestate{$page}{meta}{license}=$value;
106 elsif ($key eq 'copyright') {
107 push @{$metaheaders{$page}}, '<link rel="copyright" href="#page_copyright" />';
108 $pagestate{$page}{meta}{copyright}=$value;
111 elsif ($key eq 'link' && ! %params) {
113 add_link($page, $value);
116 elsif ($key eq 'author') {
117 $pagestate{$page}{meta}{author}=$value;
120 elsif ($key eq 'authorurl') {
121 $pagestate{$page}{meta}{authorurl}=$value if safeurl($value);
124 elsif ($key eq 'date') {
125 eval q{use Date::Parse};
127 my $time = str2time($value);
128 $IkiWiki::pagectime{$page}=$time if defined $time;
131 elsif ($key eq 'updated') {
132 eval q{use Date::Parse};
134 my $time = str2time($value);
135 $pagestate{$page}{meta}{updated}=$time if defined $time;
139 if (! defined wantarray) {
140 # avoid collecting duplicate data during scan pass
144 # Metadata collection that happens only during preprocessing pass.
145 if ($key eq 'permalink') {
146 if (safeurl($value)) {
147 $pagestate{$page}{meta}{permalink}=$value;
148 push @{$metaheaders{$page}}, scrub('<link rel="bookmark" href="'.encode_entities($value).'" />', $destpage);
151 elsif ($key eq 'stylesheet') {
152 my $rel=exists $params{rel} ? $params{rel} : "alternate stylesheet";
153 my $title=exists $params{title} ? $params{title} : $value;
154 # adding .css to the value prevents using any old web
155 # editable page as a stylesheet
156 my $stylesheet=bestlink($page, $value.".css");
157 if (! length $stylesheet) {
158 error gettext("stylesheet not found")
160 push @{$metaheaders{$page}}, '<link href="'.urlto($stylesheet, $page).
161 '" rel="'.encode_entities($rel).
162 '" title="'.encode_entities($title).
163 "\" type=\"text/css\" />";
165 elsif ($key eq 'openid') {
166 my $delegate=0; # both by default
167 if (exists $params{delegate}) {
168 $delegate = 1 if lc $params{delegate} eq 'openid';
169 $delegate = 2 if lc $params{delegate} eq 'openid2';
171 if (exists $params{server} && safeurl($params{server})) {
172 push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
173 '" rel="openid.server" />' if $delegate ne 2;
174 push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
175 '" rel="openid2.provider" />' if $delegate ne 1;
177 if (safeurl($value)) {
178 push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
179 '" rel="openid.delegate" />' if $delegate ne 2;
180 push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
181 '" rel="openid2.local_id" />' if $delegate ne 1;
183 if (exists $params{"xrds-location"} && safeurl($params{"xrds-location"})) {
184 push @{$metaheaders{$page}}, '<meta http-equiv="X-XRDS-Location"'.
185 'content="'.encode_entities($params{"xrds-location"}).'" />';
188 elsif ($key eq 'redir') {
189 return "" if $page ne $destpage;
191 if ($value !~ /^\w+:\/\//) {
192 my ($redir_page, $redir_anchor) = split /\#/, $value;
194 my $link=bestlink($page, $redir_page);
195 if (! length $link) {
196 error gettext("redir page not found")
198 add_depends($page, $link, deptype("presence"));
200 $value=urlto($link, $page);
201 $value.='#'.$redir_anchor if defined $redir_anchor;
204 # redir cycle detection
205 $pagestate{$page}{meta}{redir}=$link;
208 while (exists $pagestate{$at}{meta}{redir}) {
210 error gettext("redir cycle is not allowed")
213 $at=$pagestate{$at}{meta}{redir};
217 $value=encode_entities($value);
219 my $delay=int(exists $params{delay} ? $params{delay} : 0);
220 my $redir="<meta http-equiv=\"refresh\" content=\"$delay; URL=$value\" />";
222 $redir=scrub($redir, $destpage);
224 push @{$metaheaders{$page}}, $redir;
226 elsif ($key eq 'link') {
228 push @{$metaheaders{$page}}, scrub("<link href=\"".encode_entities($value)."\" ".
230 encode_entities($_)."=\"".encode_entities(decode_entities($params{$_}))."\""
235 elsif ($key eq 'robots') {
236 push @{$metaheaders{$page}}, '<meta name="robots"'.
237 ' content="'.encode_entities($value).'" />';
240 push @{$metaheaders{$page}}, scrub('<meta name="'.encode_entities($key).
241 '" content="'.encode_entities($value).'" />', $destpage);
247 sub pagetemplate (@) {
249 my $page=$params{page};
250 my $destpage=$params{destpage};
251 my $template=$params{template};
253 if (exists $metaheaders{$page} && $template->query(name => "meta")) {
254 # avoid duplicate meta lines
256 $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$metaheaders{$page}}));
258 if (exists $pagestate{$page}{meta}{title} && $template->query(name => "title")) {
259 $template->param(title => $pagestate{$page}{meta}{title});
260 $template->param(title_overridden => 1);
263 foreach my $field (qw{author authorurl permalink}) {
264 $template->param($field => $pagestate{$page}{meta}{$field})
265 if exists $pagestate{$page}{meta}{$field} && $template->query(name => $field);
268 foreach my $field (qw{license copyright}) {
269 if (exists $pagestate{$page}{meta}{$field} && $template->query(name => $field) &&
270 ($page eq $destpage || ! exists $pagestate{$destpage}{meta}{$field} ||
271 $pagestate{$page}{meta}{$field} ne $pagestate{$destpage}{meta}{$field})) {
272 $template->param($field => htmlize($page, $destpage, $pagestate{$page}{meta}{$field}));
281 # turn glob into a safe regexp
282 my $re=IkiWiki::glob2re(shift);
285 if (exists $pagestate{$page}{meta}{$field}) {
286 $val=$pagestate{$page}{meta}{$field};
288 elsif ($field eq 'title') {
289 $val = pagetitle($page);
293 if ($val=~/^$re$/i) {
294 return IkiWiki::SuccessReason->new("$re matches $field of $page", $page => $IkiWiki::DEPEND_CONTENT, "" => 1);
297 return IkiWiki::FailReason->new("$re does not match $field of $page", "" => 1);
301 return IkiWiki::FailReason->new("$page does not have a $field", "" => 1);
305 package IkiWiki::PageSpec;
307 sub match_title ($$;@) {
308 IkiWiki::Plugin::meta::match("title", @_);
311 sub match_author ($$;@) {
312 IkiWiki::Plugin::meta::match("author", @_);
315 sub match_authorurl ($$;@) {
316 IkiWiki::Plugin::meta::match("authorurl", @_);
319 sub match_license ($$;@) {
320 IkiWiki::Plugin::meta::match("license", @_);
323 sub match_copyright ($$;@) {
324 IkiWiki::Plugin::meta::match("copyright", @_);