]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/meta.pm
improve a string
[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;
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 } # }}}
21 sub filter (@) { #{{{
22         my %params=@_;
23         
24         $meta{$params{page}}='';
26         return $params{content};
27 } # }}}
29 sub scrub ($) { #{{{
30         if (IkiWiki::Plugin::htmlscrubber->can("sanitize")) {
31                 return IkiWiki::Plugin::htmlscrubber::sanitize(content => shift);
32         }
33         else {
34                 return shift;
35         }
36 } #}}}
38 sub preprocess (@) { #{{{
39         if (! @_) {
40                 return "";
41         }
42         my %params=@_;
43         my $key=shift;
44         my $value=$params{$key};
45         delete $params{$key};
46         my $page=$params{page};
47         delete $params{page};
48         delete $params{destpage};
49         delete $params{preview};
51         eval q{use HTML::Entities};
52         # Always dencode, even if encoding later, since it might not be
53         # fully encoded.
54         $value=decode_entities($value);
56         if ($key eq 'link') {
57                 if (%params) {
58                         $meta{$page}.=scrub("<link href=\"".encode_entities($value)."\" ".
59                                 join(" ", map { encode_entities($_)."=\"".encode_entities(decode_entities($params{$_}))."\"" } keys %params).
60                                 " />\n");
61                 }
62                 else {
63                         # hidden WikiLink
64                         push @{$links{$page}}, $value;
65                 }
66         }
67         elsif ($key eq 'title') {
68                 $title{$page}=encode_entities($value);
69         }
70         elsif ($key eq 'permalink') {
71                 $permalink{$page}=$value;
72                 $meta{$page}.=scrub("<link rel=\"bookmark\" href=\"".encode_entities($value)."\" />\n");
73         }
74         elsif ($key eq 'date') {
75                 eval q{use Date::Parse};
76                 if (! $@) {
77                         my $time = str2time($value);
78                         $IkiWiki::pagectime{$page}=$time if defined $time;
79                 }
80         }
81         elsif ($key eq 'stylesheet') {
82                 my $rel=exists $params{rel} ? $params{rel} : "alternate stylesheet";
83                 my $title=exists $params{title} ? $params{title} : $value;
84                 # adding .css to the value prevents using any old web
85                 # editable page as a stylesheet
86                 my $stylesheet=bestlink($page, $value.".css");
87                 if (! length $stylesheet) {
88                         return "[[meta ".gettext("stylesheet not found")."]]";
89                 }
90                 $meta{$page}.='<link href="'.$stylesheet.
91                         '" rel="'.encode_entities($rel).
92                         '" title="'.encode_entities($title).
93                         "\" style=\"text/css\" />\n";
94         }
95         elsif ($key eq 'openid') {
96                 if (exists $params{server}) {
97                         $meta{$page}.='<link href="'.encode_entities($params{server}).
98                                 "\" rel=\"openid.server\" />\n";
99                 }
100                 $meta{$page}.='<link href="'.encode_entities($value).
101                         "\" rel=\"openid.delegate\" />\n";
102         }
103         else {
104                 $meta{$page}.=scrub("<meta name=\"".encode_entities($key).
105                         "\" content=\"".encode_entities($value)."\" />\n");
106                 if ($key eq 'author') {
107                         $author{$page}=$value;
108                 }
109                 elsif ($key eq 'authorurl') {
110                         $authorurl{$page}=$value;
111                 }
112         }
114         return "";
115 } # }}}
117 sub pagetemplate (@) { #{{{
118         my %params=@_;
119         my $page=$params{page};
120         my $template=$params{template};
122         $template->param(meta => $meta{$page})
123                 if exists $meta{$page} && $template->query(name => "meta");
124         if (exists $title{$page} && $template->query(name => "title")) {
125                 $template->param(title => $title{$page});
126                 $template->param(title_overridden => 1);
127         }
128         $template->param(permalink => $permalink{$page})
129                 if exists $permalink{$page} && $template->query(name => "permalink");
130         $template->param(author => $author{$page})
131                 if exists $author{$page} && $template->query(name => "author");
132         $template->param(authorurl => $authorurl{$page})
133                 if exists $authorurl{$page} && $template->query(name => "authorurl");
134         
135 } # }}}