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, 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);
82 elsif ($key eq 'license') {
83 push @{$metaheaders{$page}}, '<link rel="license" href="#page_license" />';
84 $pagestate{$page}{meta}{license}=$value;
87 elsif ($key eq 'copyright') {
88 push @{$metaheaders{$page}}, '<link rel="copyright" href="#page_copyright" />';
89 $pagestate{$page}{meta}{copyright}=$value;
92 elsif ($key eq 'link' && ! %params) {
94 push @{$links{$page}}, $value;
97 elsif ($key eq 'author') {
98 $pagestate{$page}{meta}{author}=$value;
101 elsif ($key eq 'authorurl') {
102 $pagestate{$page}{meta}{authorurl}=$value if safeurl($value);
106 if (! defined wantarray) {
107 # avoid collecting duplicate data during scan pass
111 # Metadata collection that happens only during preprocessing pass.
112 if ($key eq 'date') {
113 eval q{use Date::Parse};
115 my $time = str2time($value);
116 $IkiWiki::pagectime{$page}=$time if defined $time;
119 elsif ($key eq 'permalink') {
120 if (safeurl($value)) {
121 $pagestate{$page}{meta}{permalink}=$value;
122 push @{$metaheaders{$page}}, scrub('<link rel="bookmark" href="'.encode_entities($value).'" />');
125 elsif ($key eq 'stylesheet') {
126 my $rel=exists $params{rel} ? $params{rel} : "alternate stylesheet";
127 my $title=exists $params{title} ? $params{title} : $value;
128 # adding .css to the value prevents using any old web
129 # editable page as a stylesheet
130 my $stylesheet=bestlink($page, $value.".css");
131 if (! length $stylesheet) {
132 return "[[meta ".gettext("stylesheet not found")."]]";
134 push @{$metaheaders{$page}}, '<link href="'.urlto($stylesheet, $page).
135 '" rel="'.encode_entities($rel).
136 '" title="'.encode_entities($title).
137 "\" type=\"text/css\" />";
139 elsif ($key eq 'openid') {
140 if (exists $params{server} && safeurl($params{server})) {
141 push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
142 '" rel="openid.server" />';
144 if (safeurl($value)) {
145 push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
146 '" rel="openid.delegate" />';
149 elsif ($key eq 'redir') {
150 return "" if $page ne $destpage;
152 if ($value !~ /^\w+:\/\//) {
153 my ($redir_page, $redir_anchor) = split /\#/, $value;
155 add_depends($page, $redir_page);
156 my $link=bestlink($page, $redir_page);
157 if (! length $link) {
158 return "[[meta ".gettext("redir page not found")."]]";
161 $value=urlto($link, $page);
162 $value.='#'.$redir_anchor if defined $redir_anchor;
165 # redir cycle detection
166 $pagestate{$page}{meta}{redir}=$link;
169 while (exists $pagestate{$at}{meta}{redir}) {
171 return "[[meta ".gettext("redir cycle is not allowed")."]]";
174 $at=$pagestate{$at}{meta}{redir};
178 $value=encode_entities($value);
180 my $delay=int(exists $params{delay} ? $params{delay} : 0);
181 my $redir="<meta http-equiv=\"refresh\" content=\"$delay; URL=$value\">";
183 $redir=scrub($redir);
185 push @{$metaheaders{$page}}, $redir;
187 elsif ($key eq 'link') {
189 push @{$metaheaders{$page}}, scrub("<link href=\"".encode_entities($value)."\" ".
191 encode_entities($_)."=\"".encode_entities(decode_entities($params{$_}))."\""
197 push @{$metaheaders{$page}}, scrub('<meta name="'.encode_entities($key).
198 '" content="'.encode_entities($value).'" />');
204 sub pagetemplate (@) { #{{{
206 my $page=$params{page};
207 my $destpage=$params{destpage};
208 my $template=$params{template};
210 if (exists $metaheaders{$page} && $template->query(name => "meta")) {
211 # avoid duplicate meta lines
213 $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$metaheaders{$page}}));
215 if (exists $pagestate{$page}{meta}{title} && $template->query(name => "title")) {
216 $template->param(title => $pagestate{$page}{meta}{title});
217 $template->param(title_overridden => 1);
220 foreach my $field (qw{author authorurl permalink}) {
221 $template->param($field => $pagestate{$page}{meta}{$field})
222 if exists $pagestate{$page}{meta}{$field} && $template->query(name => $field);
225 foreach my $field (qw{license copyright}) {
226 if (exists $pagestate{$page}{meta}{$field} && $template->query(name => $field) &&
227 ($page eq $destpage || ! exists $pagestate{$destpage}{meta}{$field} ||
228 $pagestate{$page}{meta}{$field} ne $pagestate{$destpage}{meta}{$field})) {
229 $template->param($field => htmlize($page, $destpage, $pagestate{$page}{meta}{$field}));
238 # turn glob into a safe regexp
239 my $re=quotemeta(shift);
244 if (exists $pagestate{$page}{meta}{$field}) {
245 $val=$pagestate{$page}{meta}{$field};
247 elsif ($field eq 'title') {
248 $val=pagetitle($page);
252 if ($val=~/^$re$/i) {
253 return IkiWiki::SuccessReason->new("$re matches $field of $page");
256 return IkiWiki::FailReason->new("$re does not match $field of $page");
260 return IkiWiki::FailReason->new("$page does not have a $field");
264 package IkiWiki::PageSpec;
266 sub match_title ($$;@) { #{{{
267 IkiWiki::Plugin::meta::match("title", @_);
270 sub match_author ($$;@) { #{{{
271 IkiWiki::Plugin::meta::match("author", @_);
274 sub match_authorurl ($$;@) { #{{{
275 IkiWiki::Plugin::meta::match("authorurl", @_);
278 sub match_license ($$;@) { #{{{
279 IkiWiki::Plugin::meta::match("license", @_);
282 sub match_copyright ($$;@) { #{{{
283 IkiWiki::Plugin::meta::match("copyright", @_);