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