]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/meta.pm
improve error message if shortcuts.mdwn DNE
[git.ikiwiki.info.git] / IkiWiki / Plugin / meta.pm
1 #!/usr/bin/perl
2 # Ikiwiki metadata plugin.
3 package IkiWiki::Plugin::meta;
5 use warnings;
6 use strict;
7 use IkiWiki 2.00;
9 my %meta;
10 my %title;
11 my %permalink;
12 my %author;
13 my %authorurl;
14 my %license;
15 my %copyright;
17 sub import { #{{{
18         hook(type => "needsbuild", id => "meta", call => \&needsbuild);
19         hook(type => "preprocess", id => "meta", call => \&preprocess);
20         hook(type => "pagetemplate", id => "meta", call => \&pagetemplate);
21 } # }}}
23 sub needsbuild (@) { #{{{
24         my $needsbuild=shift;
25         foreach my $page (keys %pagestate) {
26                 if (exists $pagestate{$page}{meta}) {
27                         if (grep { $_ eq $pagesources{$page} } @$needsbuild) {
28                                 # remove state, it will be re-added
29                                 # if the preprocessor directive is still
30                                 # there during the rebuild
31                                 delete $pagestate{$page}{meta};
32                         }
33                 }
34         }
35 }
37 sub scrub ($) { #{{{
38         if (IkiWiki::Plugin::htmlscrubber->can("sanitize")) {
39                 return IkiWiki::Plugin::htmlscrubber::sanitize(content => shift);
40         }
41         else {
42                 return shift;
43         }
44 } #}}}
46 sub preprocess (@) { #{{{
47         return "" unless @_;
48         my %params=@_;
49         my $key=shift;
50         my $value=$params{$key};
51         delete $params{$key};
52         my $page=$params{page};
53         delete $params{page};
54         my $destpage=$params{destpage};
55         delete $params{destpage};
56         delete $params{preview};
58         eval q{use HTML::Entities};
59         # Always decode, even if encoding later, since it might not be
60         # fully encoded.
61         $value=decode_entities($value);
63         if ($key eq 'title') {
64                 $title{$page}=HTML::Entities::encode_numeric($value);
65         }
66         elsif ($key eq 'permalink') {
67                 $permalink{$page}=$value;
68                 push @{$meta{$page}}, scrub('<link rel="bookmark" href="'.encode_entities($value).'" />');
69         }
70         elsif ($key eq 'date') {
71                 eval q{use Date::Parse};
72                 if (! $@) {
73                         my $time = str2time($value);
74                         $IkiWiki::pagectime{$page}=$time if defined $time;
75                 }
76         }
77         elsif ($key eq 'stylesheet') {
78                 my $rel=exists $params{rel} ? $params{rel} : "alternate stylesheet";
79                 my $title=exists $params{title} ? $params{title} : $value;
80                 # adding .css to the value prevents using any old web
81                 # editable page as a stylesheet
82                 my $stylesheet=bestlink($page, $value.".css");
83                 if (! length $stylesheet) {
84                         return "[[meta ".gettext("stylesheet not found")."]]";
85                 }
86                 push @{$meta{$page}}, '<link href="'.urlto($stylesheet, $page).
87                         '" rel="'.encode_entities($rel).
88                         '" title="'.encode_entities($title).
89                         "\" type=\"text/css\" />";
90         }
91         elsif ($key eq 'openid') {
92                 if (exists $params{server}) {
93                         push @{$meta{$page}}, '<link href="'.encode_entities($params{server}).
94                                 '" rel="openid.server" />';
95                 }
96                 push @{$meta{$page}}, '<link href="'.encode_entities($value).
97                         '" rel="openid.delegate" />';
98         }
99         elsif ($key eq 'license') {
100                 push @{$meta{$page}}, '<link rel="license" href="#page_license" />';
101                 $license{$page}=$value;
102         }
103         elsif ($key eq 'copyright') {
104                 push @{$meta{$page}}, '<link rel="copyright" href="#page_copyright" />';
105                 $copyright{$page}=$value;
106         }
107         elsif ($key eq 'redir') {
108                 return "" if $page ne $destpage;
109                 my $safe=0;
110                 if ($value !~ /^\w+:\/\//) {
111                         add_depends($page, $value);
112                         my $link=bestlink($page, $value);
113                         if (! length $link) {
114                                 return "[[meta ".gettext("redir page not found")."]]";
115                         }
117                         $value=urlto($link, $page);
118                         $safe=1;
120                         # redir cycle detection
121                         $pagestate{$page}{meta}{redir}=$link;
122                         my $at=$page;
123                         my %seen;
124                         while (exists $pagestate{$at}{meta}{redir}) {
125                                 if ($seen{$at}) {
126                                         return "[[meta ".gettext("redir cycle is not allowed")."]]";
127                                 }
128                                 $seen{$at}=1;
129                                 $at=$pagestate{$at}{meta}{redir};
130                         }
131                 }
132                 else {
133                         $value=encode_entities($value);
134                 }
135                 my $delay=int(exists $params{delay} ? $params{delay} : 0);
136                 my $redir="<meta http-equiv=\"refresh\" content=\"$delay; URL=$value\">";
137                 if (! $safe) {
138                         $redir=scrub($redir);
139                 }
140                 push @{$meta{$page}}, $redir;
141         }
142         elsif ($key eq 'link') {
143                 return "[[meta ".gettext("link is no longer supported")."]]";
144         }
145         else {
146                 push @{$meta{$page}}, scrub('<meta name="'.encode_entities($key).
147                         '" content="'.encode_entities($value).'" />');
148                 if ($key eq 'author') {
149                         $author{$page}=$value;
150                 }
151                 elsif ($key eq 'authorurl') {
152                         $authorurl{$page}=$value;
153                 }
154         }
156         return "";
157 } # }}}
159 sub pagetemplate (@) { #{{{
160         my %params=@_;
161         my $page=$params{page};
162         my $destpage=$params{destpage};
163         my $template=$params{template};
165         if (exists $meta{$page} && $template->query(name => "meta")) {
166                 # avoid duplicate meta lines
167                 my %seen;
168                 $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$meta{$page}}));
169         }
170         if (exists $title{$page} && $template->query(name => "title")) {
171                 $template->param(title => $title{$page});
172                 $template->param(title_overridden => 1);
173         }
174         $template->param(permalink => $permalink{$page})
175                 if exists $permalink{$page} && $template->query(name => "permalink");
176         $template->param(author => $author{$page})
177                 if exists $author{$page} && $template->query(name => "author");
178         $template->param(authorurl => $authorurl{$page})
179                 if exists $authorurl{$page} && $template->query(name => "authorurl");
180                 
181         if ($page ne $destpage &&
182             ((exists $license{$page}   && ! exists $license{$destpage}) ||
183              (exists $copyright{$page} && ! exists $copyright{$destpage}))) {
184                 # Force a scan of the destpage to get its copyright/license
185                 # info. If the info is declared after an inline, it will
186                 # otherwise not be available at this point.
187                 IkiWiki::scan($pagesources{$destpage});
188         }
190         if (exists $license{$page} && $template->query(name => "license") &&
191             ($page eq $destpage || ! exists $license{$destpage} ||
192              $license{$page} ne $license{$destpage})) {
193                 $template->param(license => IkiWiki::linkify($page, $destpage, $license{$page}));
194         }
195         if (exists $copyright{$page} && $template->query(name => "copyright") &&
196             ($page eq $destpage || ! exists $copyright{$destpage} ||
197              $copyright{$page} ne $copyright{$destpage})) {
198                 $template->param(copyright => IkiWiki::linkify($page, $destpage, $copyright{$page}));
199         }
200 } # }}}