fix
[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;
8
9 my %meta;
10 my %title;
11 my %permalink;
12 my %author;
13 my %authorurl;
14
15 sub import { #{{{
16         IkiWiki::hook(type => "preprocess", id => "meta", 
17                 call => \&preprocess);
18         IkiWiki::hook(type => "filter", id => "meta", 
19                 call => \&filter);
20         IkiWiki::hook(type => "pagetemplate", id => "meta", 
21                 call => \&pagetemplate);
22 } # }}}
23
24 sub filter (@) { #{{{
25         my %params=@_;
26         
27         $meta{$params{page}}='';
28
29         return $params{content};
30 } # }}}
31
32 sub preprocess (@) { #{{{
33         if (! @_) {
34                 return "";
35         }
36         my %params=@_;
37         my $key=shift;
38         my $value=$params{$key};
39         delete $params{$key};
40         my $page=$params{page};
41         delete $params{page};
42         delete $params{destpage};
43
44         eval q{use HTML::Entities};
45         # Always dencode, even if encoding later, since it might not be
46         # fully encoded.
47         $value=decode_entities($value);
48
49         if ($key eq 'link') {
50                 if (%params) {
51                         $meta{$page}.="<link href=\"".encode_entities($value)."\" ".
52                                 join(" ", map { encode_entities($_)."=\"".encode_entities(decode_entities($params{$_}))."\"" } keys %params).
53                                 " />\n";
54                 }
55                 else {
56                         # hidden WikiLink
57                         push @{$IkiWiki::links{$page}}, $value;
58                 }
59         }
60         elsif ($key eq 'title') {
61                 $title{$page}=$value;
62         }
63         elsif ($key eq 'permalink') {
64                 $permalink{$page}=$value;
65         }
66         else {
67                 $meta{$page}.="<meta name=\"".encode_entities($key).
68                         "\" content=\"".encode_entities($value)."\" />\n";
69                 if ($key eq 'author') {
70                         $author{$page}=$value;
71                 }
72                 elsif ($key eq 'authorurl') {
73                         $authorurl{$page}=$value;
74                 }
75         }
76
77         return "";
78 } # }}}
79
80 sub pagetemplate (@) { #{{{
81         my %params=@_;
82         my $page=$params{page};
83         my $template=$params{template};
84
85         $template->param(meta => $meta{$page})
86                 if exists $meta{$page} && $template->query(name => "meta");
87         $template->param(title => $title{$page})
88                 if exists $title{$page} && $template->query(name => "title");
89         $template->param(permalink => $permalink{$page})
90                 if exists $permalink{$page} && $template->query(name => "permalink");
91         $template->param(author => $author{$page})
92                 if exists $author{$page} && $template->query(name => "author");
93         $template->param(authorurl => $authorurl{$page})
94                 if exists $authorurl{$page} && $template->query(name => "authorurl");
95         
96 } # }}}
97
98 1