if an inlined page has the same copyright or license as the page it's
[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 2.00;
8
9 my %meta;
10 my %title;
11 my %permalink;
12 my %author;
13 my %authorurl;
14 my %license;
15 my %copyright;
16
17 sub import { #{{{
18         hook(type => "preprocess", id => "meta", call => \&preprocess, scan => 1);
19         hook(type => "filter", id => "meta", call => \&filter);
20         hook(type => "pagetemplate", id => "meta", call => \&pagetemplate);
21 } # }}}
22
23 sub filter (@) { #{{{
24         my %params=@_;
25         
26         $meta{$params{page}}='';
27
28         return $params{content};
29 } # }}}
30
31 sub scrub ($) { #{{{
32         if (IkiWiki::Plugin::htmlscrubber->can("sanitize")) {
33                 return IkiWiki::Plugin::htmlscrubber::sanitize(content => shift);
34         }
35         else {
36                 return shift;
37         }
38 } #}}}
39
40 sub preprocess (@) { #{{{
41         if (! @_) {
42                 return "";
43         }
44         my %params=@_;
45         my $key=shift;
46         my $value=$params{$key};
47         delete $params{$key};
48         my $page=$params{page};
49         delete $params{page};
50         my $destpage=$params{destpage};
51         delete $params{destpage};
52         delete $params{preview};
53
54         eval q{use HTML::Entities};
55         # Always dencode, even if encoding later, since it might not be
56         # fully encoded.
57         $value=decode_entities($value);
58
59         if ($key eq 'link') {
60                 if (%params) {
61                         $meta{$page}.=scrub("<link href=\"".encode_entities($value)."\" ".
62                                 join(" ", map { encode_entities($_)."=\"".encode_entities(decode_entities($params{$_}))."\"" } keys %params).
63                                 " />\n");
64                 }
65                 else {
66                         # hidden WikiLink
67                         push @{$links{$page}}, $value;
68                 }
69         }
70         elsif ($key eq 'title') {
71                 $title{$page}=HTML::Entities::encode_numeric($value);
72         }
73         elsif ($key eq 'permalink') {
74                 $permalink{$page}=$value;
75                 $meta{$page}.=scrub("<link rel=\"bookmark\" href=\"".encode_entities($value)."\" />\n");
76         }
77         elsif ($key eq 'date') {
78                 eval q{use Date::Parse};
79                 if (! $@) {
80                         my $time = str2time($value);
81                         $IkiWiki::pagectime{$page}=$time if defined $time;
82                 }
83         }
84         elsif ($key eq 'stylesheet') {
85                 my $rel=exists $params{rel} ? $params{rel} : "alternate stylesheet";
86                 my $title=exists $params{title} ? $params{title} : $value;
87                 # adding .css to the value prevents using any old web
88                 # editable page as a stylesheet
89                 my $stylesheet=bestlink($page, $value.".css");
90                 if (! length $stylesheet) {
91                         return "[[meta ".gettext("stylesheet not found")."]]";
92                 }
93                 $meta{$page}.='<link href="'.urlto($stylesheet, $page).
94                         '" rel="'.encode_entities($rel).
95                         '" title="'.encode_entities($title).
96                         "\" type=\"text/css\" />\n";
97         }
98         elsif ($key eq 'openid') {
99                 if (exists $params{server}) {
100                         $meta{$page}.='<link href="'.encode_entities($params{server}).
101                                 "\" rel=\"openid.server\" />\n";
102                 }
103                 $meta{$page}.='<link href="'.encode_entities($value).
104                         "\" rel=\"openid.delegate\" />\n";
105         }
106         elsif ($key eq 'license') {
107                 $meta{$page}.="<link rel=\"license\" href=\"#page_license\" />\n";
108                 $license{$page}=IkiWiki::linkify($page, $destpage, $value);
109         }
110         elsif ($key eq 'copyright') {
111                 $meta{$page}.="<link rel=\"copyright\" href=\"#page_copyright\" />\n";
112                 $copyright{$page}=IkiWiki::linkify($page, $destpage, $value);
113         }
114         else {
115                 $meta{$page}.=scrub("<meta name=\"".encode_entities($key).
116                         "\" content=\"".encode_entities($value)."\" />\n");
117                 if ($key eq 'author') {
118                         $author{$page}=$value;
119                 }
120                 elsif ($key eq 'authorurl') {
121                         $authorurl{$page}=$value;
122                 }
123         }
124
125         return "";
126 } # }}}
127
128 sub pagetemplate (@) { #{{{
129         my %params=@_;
130         my $page=$params{page};
131         my $destpage=$params{destpage};
132         my $template=$params{template};
133
134         $template->param(meta => $meta{$page})
135                 if exists $meta{$page} && $template->query(name => "meta");
136         if (exists $title{$page} && $template->query(name => "title")) {
137                 $template->param(title => $title{$page});
138                 $template->param(title_overridden => 1);
139         }
140         $template->param(permalink => $permalink{$page})
141                 if exists $permalink{$page} && $template->query(name => "permalink");
142         $template->param(author => $author{$page})
143                 if exists $author{$page} && $template->query(name => "author");
144         $template->param(authorurl => $authorurl{$page})
145                 if exists $authorurl{$page} && $template->query(name => "authorurl");
146
147         if (exists $license{$page} && $template->query(name => "license") &&
148             ($page ne $destpage || ! exists $license{$destpage} ||
149               $license{$page} ne $license{$destpage})) {
150                 $template->param(license => $license{$page})
151         }
152         if (exists $copyright{$page} && $template->query(name => "copyright") &&
153             ($page ne $destpage || ! exists $copyright{$destpage} ||
154               $copyright{$page} ne $copyright{$destpage})) {
155                 $template->param(copyright => $copyright{$page})
156         }
157 } # }}}
158
159 1