]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/meta.pm
968e6ccee789de718bac558f6dd30cf58b0acaab
[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 => "preprocess", id => "meta", call => \&preprocess, scan => 1);
19         hook(type => "filter", id => "meta", call => \&filter);
20         hook(type => "pagetemplate", id => "meta", call => \&pagetemplate);
21 } # }}}
23 sub filter (@) { #{{{
24         my %params=@_;
25         
26         $meta{$params{page}}='';
27         delete $pagestate{$params{page}}{meta}{redir};
29         return $params{content};
30 } # }}}
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 } #}}}
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};
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);
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                 my $safe=0;
75                 if ($value !~ /^\w+:\/\//) {
76                         add_depends($page, $value);
77                         my $link=bestlink($page, $value);
78                         if (! length $link) {
79                                 return "[[meta ".gettext("redir page not found")."]]";
80                         }
81                         $pagestate{$page}{meta}{redir}=$link;
82                         if ($pagestate{$link}{meta}{redir}) {
83                                 # TODO: real cycle detection
84                                 return "[[meta ".gettext("redir not allowed to point to a page that contains a redir")."]]";
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         }
154         return "";
155 } # }}}
157 sub pagetemplate (@) { #{{{
158         my %params=@_;
159         my $page=$params{page};
160         my $destpage=$params{destpage};
161         my $template=$params{template};
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         }
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 } # }}}