2 # Ikiwiki metadata plugin.
3 package IkiWiki::Plugin::meta;
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);
17 sub needsbuild (@) { #{{{
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};
33 if (IkiWiki::Plugin::htmlscrubber->can("sanitize")) {
34 return IkiWiki::Plugin::htmlscrubber::sanitize(content => shift);
41 sub safeurl ($) { #{{{
43 if (exists $IkiWiki::Plugin::htmlscrubber::{safe_url_regexp} &&
44 defined $IkiWiki::Plugin::htmlscrubber::safe_url_regexp) {
45 return $url=~/$IkiWiki::Plugin::htmlscrubber::safe_url_regexp/;
52 sub htmlize ($$$) { #{{{
56 return IkiWiki::htmlize($page, $destpage, pagetype($pagesources{$page}),
57 IkiWiki::linkify($page, $destpage,
58 IkiWiki::preprocess($page, $destpage, shift)));
61 sub preprocess (@) { #{{{
65 my $value=$params{$key};
67 my $page=$params{page};
69 my $destpage=$params{destpage};
70 delete $params{destpage};
71 delete $params{preview};
73 eval q{use HTML::Entities};
74 # Always decode, even if encoding later, since it might not be
76 $value=decode_entities($value);
78 # Metadata collection that needs to happen during the scan pass.
79 if ($key eq 'title') {
80 $pagestate{$page}{meta}{title}=HTML::Entities::encode_numeric($value);
83 elsif ($key eq 'description') {
84 $pagestate{$page}{meta}{description}=HTML::Entities::encode_numeric($value);
87 elsif ($key eq 'guid') {
88 $pagestate{$page}{meta}{guid}=HTML::Entities::encode_numeric($value);
91 elsif ($key eq 'license') {
92 push @{$metaheaders{$page}}, '<link rel="license" href="#page_license" />';
93 $pagestate{$page}{meta}{license}=$value;
96 elsif ($key eq 'copyright') {
97 push @{$metaheaders{$page}}, '<link rel="copyright" href="#page_copyright" />';
98 $pagestate{$page}{meta}{copyright}=$value;
101 elsif ($key eq 'link' && ! %params) {
103 push @{$links{$page}}, $value;
106 elsif ($key eq 'author') {
107 $pagestate{$page}{meta}{author}=$value;
110 elsif ($key eq 'authorurl') {
111 $pagestate{$page}{meta}{authorurl}=$value if safeurl($value);
115 if (! defined wantarray) {
116 # avoid collecting duplicate data during scan pass
120 # Metadata collection that happens only during preprocessing pass.
121 if ($key eq 'date') {
122 eval q{use Date::Parse};
124 my $time = str2time($value);
125 $IkiWiki::pagectime{$page}=$time if defined $time;
128 elsif ($key eq 'permalink') {
129 if (safeurl($value)) {
130 $pagestate{$page}{meta}{permalink}=$value;
131 push @{$metaheaders{$page}}, scrub('<link rel="bookmark" href="'.encode_entities($value).'" />');
134 elsif ($key eq 'stylesheet') {
135 my $rel=exists $params{rel} ? $params{rel} : "alternate stylesheet";
136 my $title=exists $params{title} ? $params{title} : $value;
137 # adding .css to the value prevents using any old web
138 # editable page as a stylesheet
139 my $stylesheet=bestlink($page, $value.".css");
140 if (! length $stylesheet) {
141 error gettext("stylesheet not found")
143 push @{$metaheaders{$page}}, '<link href="'.urlto($stylesheet, $page).
144 '" rel="'.encode_entities($rel).
145 '" title="'.encode_entities($title).
146 "\" type=\"text/css\" />";
148 elsif ($key eq 'openid') {
149 if (exists $params{server} && safeurl($params{server})) {
150 push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
151 '" rel="openid.server" />';
152 push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
153 '" rel="openid2.provider" />';
155 if (safeurl($value)) {
156 push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
157 '" rel="openid.delegate" />';
158 push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
159 '" rel="openid2.local_id" />';
161 if (exists $params{"xrds-location"} && safeurl($params{"xrds-location"})) {
162 push @{$metaheaders{$page}}, '<meta http-equiv="X-XRDS-Location"'.
163 'content="'.encode_entities($params{"xrds-location"}).'" />';
166 elsif ($key eq 'redir') {
167 return "" if $page ne $destpage;
169 if ($value !~ /^\w+:\/\//) {
170 my ($redir_page, $redir_anchor) = split /\#/, $value;
172 add_depends($page, $redir_page);
173 my $link=bestlink($page, $redir_page);
174 if (! length $link) {
175 error gettext("redir page not found")
178 $value=urlto($link, $page);
179 $value.='#'.$redir_anchor if defined $redir_anchor;
182 # redir cycle detection
183 $pagestate{$page}{meta}{redir}=$link;
186 while (exists $pagestate{$at}{meta}{redir}) {
188 error gettext("redir cycle is not allowed")
191 $at=$pagestate{$at}{meta}{redir};
195 $value=encode_entities($value);
197 my $delay=int(exists $params{delay} ? $params{delay} : 0);
198 my $redir="<meta http-equiv=\"refresh\" content=\"$delay; URL=$value\" />";
200 $redir=scrub($redir);
202 push @{$metaheaders{$page}}, $redir;
204 elsif ($key eq 'link') {
206 push @{$metaheaders{$page}}, scrub("<link href=\"".encode_entities($value)."\" ".
208 encode_entities($_)."=\"".encode_entities(decode_entities($params{$_}))."\""
213 elsif ($key eq 'robots') {
214 push @{$metaheaders{$page}}, '<meta name="robots"'.
215 ' content="'.encode_entities($value).'" />';
218 push @{$metaheaders{$page}}, scrub('<meta name="'.encode_entities($key).
219 '" content="'.encode_entities($value).'" />');
225 sub pagetemplate (@) { #{{{
227 my $page=$params{page};
228 my $destpage=$params{destpage};
229 my $template=$params{template};
231 if (exists $metaheaders{$page} && $template->query(name => "meta")) {
232 # avoid duplicate meta lines
234 $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$metaheaders{$page}}));
236 if (exists $pagestate{$page}{meta}{title} && $template->query(name => "title")) {
237 $template->param(title => $pagestate{$page}{meta}{title});
238 $template->param(title_overridden => 1);
241 foreach my $field (qw{author authorurl permalink}) {
242 $template->param($field => $pagestate{$page}{meta}{$field})
243 if exists $pagestate{$page}{meta}{$field} && $template->query(name => $field);
246 foreach my $field (qw{license copyright}) {
247 if (exists $pagestate{$page}{meta}{$field} && $template->query(name => $field) &&
248 ($page eq $destpage || ! exists $pagestate{$destpage}{meta}{$field} ||
249 $pagestate{$page}{meta}{$field} ne $pagestate{$destpage}{meta}{$field})) {
250 $template->param($field => htmlize($page, $destpage, $pagestate{$page}{meta}{$field}));
259 # turn glob into a safe regexp
260 my $re=IkiWiki::glob2re(shift);
263 if (exists $pagestate{$page}{meta}{$field}) {
264 $val=$pagestate{$page}{meta}{$field};
266 elsif ($field eq 'title') {
267 $val = IkiWiki::pagetitle($page);
271 if ($val=~/^$re$/i) {
272 return IkiWiki::SuccessReason->new("$re matches $field of $page");
275 return IkiWiki::FailReason->new("$re does not match $field of $page");
279 return IkiWiki::FailReason->new("$page does not have a $field");
283 package IkiWiki::PageSpec;
285 sub match_title ($$;@) { #{{{
286 IkiWiki::Plugin::meta::match("title", @_);
289 sub match_author ($$;@) { #{{{
290 IkiWiki::Plugin::meta::match("author", @_);
293 sub match_authorurl ($$;@) { #{{{
294 IkiWiki::Plugin::meta::match("authorurl", @_);
297 sub match_license ($$;@) { #{{{
298 IkiWiki::Plugin::meta::match("license", @_);
301 sub match_copyright ($$;@) { #{{{
302 IkiWiki::Plugin::meta::match("copyright", @_);