* Change order of linkify and preprocess; first preprocess and then linkify.
[ikiwiki] / 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                 $meta{$page}.="<link rel=\"bookmark\" href=\"".encode_entities($value)."\" />\n";
66         }
67         else {
68                 $meta{$page}.="<meta name=\"".encode_entities($key).
69                         "\" content=\"".encode_entities($value)."\" />\n";
70                 if ($key eq 'author') {
71                         $author{$page}=$value;
72                 }
73                 elsif ($key eq 'authorurl') {
74                         $authorurl{$page}=$value;
75                 }
76         }
77
78         return "";
79 } # }}}
80
81 sub pagetemplate (@) { #{{{
82         my %params=@_;
83         my $page=$params{page};
84         my $template=$params{template};
85
86         $template->param(meta => $meta{$page})
87                 if exists $meta{$page} && $template->query(name => "meta");
88         if (exists $title{$page} && $template->query(name => "title")) {
89                 $template->param(title => $title{$page});
90                 $template->param(title_overridden => 1);
91         }
92         $template->param(permalink => $permalink{$page})
93                 if exists $permalink{$page} && $template->query(name => "permalink");
94         $template->param(author => $author{$page})
95                 if exists $author{$page} && $template->query(name => "author");
96         $template->param(authorurl => $authorurl{$page})
97                 if exists $authorurl{$page} && $template->query(name => "authorurl");
98         
99 } # }}}
100
101 1