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