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);
18 sub getsetup () { #{{{
26 sub needsbuild (@) { #{{{
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(content => shift);
50 sub safeurl ($) { #{{{
52 if (exists $IkiWiki::Plugin::htmlscrubber::{safe_url_regexp} &&
53 defined $IkiWiki::Plugin::htmlscrubber::safe_url_regexp) {
54 return $url=~/$IkiWiki::Plugin::htmlscrubber::safe_url_regexp/;
61 sub htmlize ($$$) { #{{{
65 return IkiWiki::htmlize($page, $destpage, pagetype($pagesources{$page}),
66 IkiWiki::linkify($page, $destpage,
67 IkiWiki::preprocess($page, $destpage, shift)));
70 sub preprocess (@) { #{{{
74 my $value=$params{$key};
76 my $page=$params{page};
78 my $destpage=$params{destpage};
79 delete $params{destpage};
80 delete $params{preview};
82 eval q{use HTML::Entities};
83 # Always decode, even if encoding later, since it might not be
85 $value=decode_entities($value);
87 # Metadata collection that needs to happen during the scan pass.
88 if ($key eq 'title') {
89 $pagestate{$page}{meta}{title}=HTML::Entities::encode_numeric($value);
92 elsif ($key eq 'description') {
93 $pagestate{$page}{meta}{description}=HTML::Entities::encode_numeric($value);
96 elsif ($key eq 'guid') {
97 $pagestate{$page}{meta}{guid}=HTML::Entities::encode_numeric($value);
100 elsif ($key eq 'license') {
101 push @{$metaheaders{$page}}, '<link rel="license" href="#page_license" />';
102 $pagestate{$page}{meta}{license}=$value;
105 elsif ($key eq 'copyright') {
106 push @{$metaheaders{$page}}, '<link rel="copyright" href="#page_copyright" />';
107 $pagestate{$page}{meta}{copyright}=$value;
110 elsif ($key eq 'link' && ! %params) {
112 push @{$links{$page}}, $value;
115 elsif ($key eq 'author') {
116 $pagestate{$page}{meta}{author}=$value;
119 elsif ($key eq 'authorurl') {
120 $pagestate{$page}{meta}{authorurl}=$value if safeurl($value);
124 if (! defined wantarray) {
125 # avoid collecting duplicate data during scan pass
129 # Metadata collection that happens only during preprocessing pass.
130 if ($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 'permalink') {
138 if (safeurl($value)) {
139 $pagestate{$page}{meta}{permalink}=$value;
140 push @{$metaheaders{$page}}, scrub('<link rel="bookmark" href="'.encode_entities($value).'" />');
143 elsif ($key eq 'stylesheet') {
144 my $rel=exists $params{rel} ? $params{rel} : "alternate stylesheet";
145 my $title=exists $params{title} ? $params{title} : $value;
146 # adding .css to the value prevents using any old web
147 # editable page as a stylesheet
148 my $stylesheet=bestlink($page, $value.".css");
149 if (! length $stylesheet) {
150 error gettext("stylesheet not found")
152 push @{$metaheaders{$page}}, '<link href="'.urlto($stylesheet, $page).
153 '" rel="'.encode_entities($rel).
154 '" title="'.encode_entities($title).
155 "\" type=\"text/css\" />";
157 elsif ($key eq 'openid') {
158 if (exists $params{server} && safeurl($params{server})) {
159 push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
160 '" rel="openid.server" />';
161 push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
162 '" rel="openid2.provider" />';
164 if (safeurl($value)) {
165 push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
166 '" rel="openid.delegate" />';
167 push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
168 '" rel="openid2.local_id" />';
170 if (exists $params{"xrds-location"} && safeurl($params{"xrds-location"})) {
171 push @{$metaheaders{$page}}, '<meta http-equiv="X-XRDS-Location"'.
172 'content="'.encode_entities($params{"xrds-location"}).'" />';
175 elsif ($key eq 'redir') {
176 return "" if $page ne $destpage;
178 if ($value !~ /^\w+:\/\//) {
179 my ($redir_page, $redir_anchor) = split /\#/, $value;
181 add_depends($page, $redir_page);
182 my $link=bestlink($page, $redir_page);
183 if (! length $link) {
184 error gettext("redir page not found")
187 $value=urlto($link, $page);
188 $value.='#'.$redir_anchor if defined $redir_anchor;
191 # redir cycle detection
192 $pagestate{$page}{meta}{redir}=$link;
195 while (exists $pagestate{$at}{meta}{redir}) {
197 error gettext("redir cycle is not allowed")
200 $at=$pagestate{$at}{meta}{redir};
204 $value=encode_entities($value);
206 my $delay=int(exists $params{delay} ? $params{delay} : 0);
207 my $redir="<meta http-equiv=\"refresh\" content=\"$delay; URL=$value\" />";
209 $redir=scrub($redir);
211 push @{$metaheaders{$page}}, $redir;
213 elsif ($key eq 'link') {
215 push @{$metaheaders{$page}}, scrub("<link href=\"".encode_entities($value)."\" ".
217 encode_entities($_)."=\"".encode_entities(decode_entities($params{$_}))."\""
222 elsif ($key eq 'robots') {
223 push @{$metaheaders{$page}}, '<meta name="robots"'.
224 ' content="'.encode_entities($value).'" />';
227 push @{$metaheaders{$page}}, scrub('<meta name="'.encode_entities($key).
228 '" content="'.encode_entities($value).'" />');
234 sub pagetemplate (@) { #{{{
236 my $page=$params{page};
237 my $destpage=$params{destpage};
238 my $template=$params{template};
240 if (exists $metaheaders{$page} && $template->query(name => "meta")) {
241 # avoid duplicate meta lines
243 $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$metaheaders{$page}}));
245 if (exists $pagestate{$page}{meta}{title} && $template->query(name => "title")) {
246 $template->param(title => $pagestate{$page}{meta}{title});
247 $template->param(title_overridden => 1);
250 foreach my $field (qw{author authorurl permalink}) {
251 $template->param($field => $pagestate{$page}{meta}{$field})
252 if exists $pagestate{$page}{meta}{$field} && $template->query(name => $field);
255 foreach my $field (qw{license copyright}) {
256 if (exists $pagestate{$page}{meta}{$field} && $template->query(name => $field) &&
257 ($page eq $destpage || ! exists $pagestate{$destpage}{meta}{$field} ||
258 $pagestate{$page}{meta}{$field} ne $pagestate{$destpage}{meta}{$field})) {
259 $template->param($field => htmlize($page, $destpage, $pagestate{$page}{meta}{$field}));
268 # turn glob into a safe regexp
269 my $re=IkiWiki::glob2re(shift);
272 if (exists $pagestate{$page}{meta}{$field}) {
273 $val=$pagestate{$page}{meta}{$field};
275 elsif ($field eq 'title') {
276 $val = pagetitle($page);
280 if ($val=~/^$re$/i) {
281 return IkiWiki::SuccessReason->new("$re matches $field of $page");
284 return IkiWiki::FailReason->new("$re does not match $field of $page");
288 return IkiWiki::FailReason->new("$page does not have a $field");
292 package IkiWiki::PageSpec;
294 sub match_title ($$;@) { #{{{
295 IkiWiki::Plugin::meta::match("title", @_);
298 sub match_author ($$;@) { #{{{
299 IkiWiki::Plugin::meta::match("author", @_);
302 sub match_authorurl ($$;@) { #{{{
303 IkiWiki::Plugin::meta::match("authorurl", @_);
306 sub match_license ($$;@) { #{{{
307 IkiWiki::Plugin::meta::match("license", @_);
310 sub match_copyright ($$;@) { #{{{
311 IkiWiki::Plugin::meta::match("copyright", @_);