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 htmlize ($$$) { #{{{
45 return IkiWiki::htmlize($page, pagetype($pagesources{$page}),
46 IkiWiki::linkify($page, $destpage,
47 IkiWiki::preprocess($page, $destpage, shift)));
50 sub preprocess (@) { #{{{
54 my $value=$params{$key};
56 my $page=$params{page};
58 my $destpage=$params{destpage};
59 delete $params{destpage};
60 delete $params{preview};
62 eval q{use HTML::Entities};
63 # Always decode, even if encoding later, since it might not be
65 $value=decode_entities($value);
67 # Metadata collection that needs to happen during the scan pass.
68 if ($key eq 'title') {
69 $pagestate{$page}{meta}{title}=HTML::Entities::encode_numeric($value);
71 elsif ($key eq 'license') {
72 push @{$metaheaders{$page}}, '<link rel="license" href="#page_license" />';
73 $pagestate{$page}{meta}{license}=$value;
76 elsif ($key eq 'copyright') {
77 push @{$metaheaders{$page}}, '<link rel="copyright" href="#page_copyright" />';
78 $pagestate{$page}{meta}{copyright}=$value;
81 elsif ($key eq 'link' && ! %params) {
83 push @{$links{$page}}, $value;
86 elsif ($key eq 'author') {
87 $pagestate{$page}{meta}{author}=$value;
90 elsif ($key eq 'authorurl') {
91 $pagestate{$page}{meta}{authorurl}=$value;
95 if (! defined wantarray) {
96 # avoid collecting duplicate data during scan pass
100 # Metadata collection that happens only during preprocessing pass.
101 if ($key eq 'date') {
102 eval q{use Date::Parse};
104 my $time = str2time($value);
105 $IkiWiki::pagectime{$page}=$time if defined $time;
108 elsif ($key eq 'permalink') {
109 $pagestate{$page}{meta}{permalink}=$value;
110 push @{$metaheaders{$page}}, scrub('<link rel="bookmark" href="'.encode_entities($value).'" />');
112 elsif ($key eq 'stylesheet') {
113 my $rel=exists $params{rel} ? $params{rel} : "alternate stylesheet";
114 my $title=exists $params{title} ? $params{title} : $value;
115 # adding .css to the value prevents using any old web
116 # editable page as a stylesheet
117 my $stylesheet=bestlink($page, $value.".css");
118 if (! length $stylesheet) {
119 return "[[meta ".gettext("stylesheet not found")."]]";
121 push @{$metaheaders{$page}}, '<link href="'.urlto($stylesheet, $page).
122 '" rel="'.encode_entities($rel).
123 '" title="'.encode_entities($title).
124 "\" type=\"text/css\" />";
126 elsif ($key eq 'openid') {
127 if (exists $params{server}) {
128 push @{$metaheaders{$page}}, '<link href="'.encode_entities($params{server}).
129 '" rel="openid.server" />';
131 push @{$metaheaders{$page}}, '<link href="'.encode_entities($value).
132 '" rel="openid.delegate" />';
134 elsif ($key eq 'redir') {
135 return "" if $page ne $destpage;
137 if ($value !~ /^\w+:\/\//) {
138 my ($redir_page, $redir_anchor) = split /\#/, $value;
140 add_depends($page, $redir_page);
141 my $link=bestlink($page, $redir_page);
142 if (! length $link) {
143 return "[[meta ".gettext("redir page not found")."]]";
146 $value=urlto($link, $page);
147 $value.='#'.$redir_anchor if defined $redir_anchor;
150 # redir cycle detection
151 $pagestate{$page}{meta}{redir}=$link;
154 while (exists $pagestate{$at}{meta}{redir}) {
156 return "[[meta ".gettext("redir cycle is not allowed")."]]";
159 $at=$pagestate{$at}{meta}{redir};
163 $value=encode_entities($value);
165 my $delay=int(exists $params{delay} ? $params{delay} : 0);
166 my $redir="<meta http-equiv=\"refresh\" content=\"$delay; URL=$value\">";
168 $redir=scrub($redir);
170 push @{$metaheaders{$page}}, $redir;
172 elsif ($key eq 'link') {
174 push @{$metaheaders{$page}}, scrub("<link href=\"".encode_entities($value)."\" ".
176 encode_entities($_)."=\"".encode_entities(decode_entities($params{$_}))."\""
182 push @{$metaheaders{$page}}, scrub('<meta name="'.encode_entities($key).
183 '" content="'.encode_entities($value).'" />');
189 sub pagetemplate (@) { #{{{
191 my $page=$params{page};
192 my $destpage=$params{destpage};
193 my $template=$params{template};
195 if (exists $metaheaders{$page} && $template->query(name => "meta")) {
196 # avoid duplicate meta lines
198 $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$metaheaders{$page}}));
200 if (exists $pagestate{$page}{meta}{title} && $template->query(name => "title")) {
201 $template->param(title => $pagestate{$page}{meta}{title});
202 $template->param(title_overridden => 1);
205 foreach my $field (qw{author authorurl permalink}) {
206 $template->param($field => $pagestate{$page}{meta}{$field})
207 if exists $pagestate{$page}{meta}{$field} && $template->query(name => $field);
210 foreach my $field (qw{license copyright}) {
211 if (exists $pagestate{$page}{meta}{$field} && $template->query(name => $field) &&
212 ($page eq $destpage || ! exists $pagestate{$destpage}{meta}{$field} ||
213 $pagestate{$page}{meta}{$field} ne $pagestate{$destpage}{meta}{$field})) {
214 $template->param($field => htmlize($page, $destpage, $pagestate{$page}{meta}{$field}));
223 # turn glob into a safe regexp
224 my $re=quotemeta(shift);
229 if (exists $pagestate{$page}{meta}{$field}) {
230 $val=$pagestate{$page}{meta}{$field};
232 elsif ($field eq 'title') {
233 $val=pagetitle($page);
237 if ($val=~/^$re$/i) {
238 return IkiWiki::SuccessReason->new("$re matches $field of $page");
241 return IkiWiki::FailReason->new("$re does not match $field of $page");
245 return IkiWiki::FailReason->new("$page does not have a $field");
249 package IkiWiki::PageSpec;
251 sub match_title ($$;@) { #{{{
252 IkiWiki::Plugin::meta::match("title", @_);
255 sub match_author ($$;@) { #{{{
256 IkiWiki::Plugin::meta::match("author", @_);
259 sub match_authorurl ($$;@) { #{{{
260 IkiWiki::Plugin::meta::match("authorurl", @_);
263 sub match_license ($$;@) { #{{{
264 IkiWiki::Plugin::meta::match("license", @_);
267 sub match_copyright ($$;@) { #{{{
268 IkiWiki::Plugin::meta::match("copyright", @_);