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