]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/meta.pm
* Only try postsignin if no other action matched. Fixes a bug where the
[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                         my ($redir_page, $redir_anchor) = split /\#/, $value;
113                         add_depends($page, $redir_page);
114                         my $link=bestlink($page, $redir_page);
115                         if (! length $link) {
116                                 return "[[meta ".gettext("redir page not found")."]]";
117                         }
119                         $value=urlto($link, $page);
120                         $value.='#'.$redir_anchor if defined $redir_anchor;
121                         $safe=1;
123                         # redir cycle detection
124                         $pagestate{$page}{meta}{redir}=$link;
125                         my $at=$page;
126                         my %seen;
127                         while (exists $pagestate{$at}{meta}{redir}) {
128                                 if ($seen{$at}) {
129                                         return "[[meta ".gettext("redir cycle is not allowed")."]]";
130                                 }
131                                 $seen{$at}=1;
132                                 $at=$pagestate{$at}{meta}{redir};
133                         }
134                 }
135                 else {
136                         $value=encode_entities($value);
137                 }
138                 my $delay=int(exists $params{delay} ? $params{delay} : 0);
139                 my $redir="<meta http-equiv=\"refresh\" content=\"$delay; URL=$value\">";
140                 if (! $safe) {
141                         $redir=scrub($redir);
142                 }
143                 push @{$meta{$page}}, $redir;
144         }
145         elsif ($key eq 'link') {
146                 return "[[meta ".gettext("link is no longer supported")."]]";
147         }
148         else {
149                 push @{$meta{$page}}, scrub('<meta name="'.encode_entities($key).
150                         '" content="'.encode_entities($value).'" />');
151                 if ($key eq 'author') {
152                         $author{$page}=$value;
153                 }
154                 elsif ($key eq 'authorurl') {
155                         $authorurl{$page}=$value;
156                 }
157         }
159         return "";
160 } # }}}
162 sub pagetemplate (@) { #{{{
163         my %params=@_;
164         my $page=$params{page};
165         my $destpage=$params{destpage};
166         my $template=$params{template};
168         if (exists $meta{$page} && $template->query(name => "meta")) {
169                 # avoid duplicate meta lines
170                 my %seen;
171                 $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$meta{$page}}));
172         }
173         if (exists $title{$page} && $template->query(name => "title")) {
174                 $template->param(title => $title{$page});
175                 $template->param(title_overridden => 1);
176         }
177         $template->param(permalink => $permalink{$page})
178                 if exists $permalink{$page} && $template->query(name => "permalink");
179         $template->param(author => $author{$page})
180                 if exists $author{$page} && $template->query(name => "author");
181         $template->param(authorurl => $authorurl{$page})
182                 if exists $authorurl{$page} && $template->query(name => "authorurl");
183                 
184         if ($page ne $destpage &&
185             ((exists $license{$page}   && ! exists $license{$destpage}) ||
186              (exists $copyright{$page} && ! exists $copyright{$destpage}))) {
187                 # Force a scan of the destpage to get its copyright/license
188                 # info. If the info is declared after an inline, it will
189                 # otherwise not be available at this point.
190                 IkiWiki::scan($pagesources{$destpage});
191         }
193         if (exists $license{$page} && $template->query(name => "license") &&
194             ($page eq $destpage || ! exists $license{$destpage} ||
195              $license{$page} ne $license{$destpage})) {
196                 $template->param(license => IkiWiki::linkify($page, $destpage, $license{$page}));
197         }
198         if (exists $copyright{$page} && $template->query(name => "copyright") &&
199             ($page eq $destpage || ! exists $copyright{$destpage} ||
200              $copyright{$page} ne $copyright{$destpage})) {
201                 $template->param(copyright => IkiWiki::linkify($page, $destpage, $copyright{$page}));
202         }
203 } # }}}