2 # Ikiwiki metadata plugin.
3 package IkiWiki::Plugin::meta;
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);
26 $meta{$params{page}}='';
28 return $params{content};
32 if (IkiWiki::Plugin::htmlscrubber->can("sanitize")) {
33 return IkiWiki::Plugin::htmlscrubber::sanitize(content => shift);
40 sub preprocess (@) { #{{{
46 my $value=$params{$key};
48 my $page=$params{page};
50 delete $params{destpage};
51 delete $params{preview};
53 eval q{use HTML::Entities};
54 # Always dencode, even if encoding later, since it might not be
56 $value=decode_entities($value);
60 $meta{$page}.=scrub("<link href=\"".encode_entities($value)."\" ".
61 join(" ", map { encode_entities($_)."=\"".encode_entities(decode_entities($params{$_}))."\"" } keys %params).
66 push @{$links{$page}}, $value;
69 elsif ($key eq 'title') {
70 $title{$page}=HTML::Entities::encode_numeric($value);
72 elsif ($key eq 'permalink') {
73 $permalink{$page}=$value;
74 $meta{$page}.=scrub("<link rel=\"bookmark\" href=\"".encode_entities($value)."\" />\n");
76 elsif ($key eq 'date') {
77 eval q{use Date::Parse};
79 my $time = str2time($value);
80 $IkiWiki::pagectime{$page}=$time if defined $time;
83 elsif ($key eq 'stylesheet') {
84 my $rel=exists $params{rel} ? $params{rel} : "alternate stylesheet";
85 my $title=exists $params{title} ? $params{title} : $value;
86 # adding .css to the value prevents using any old web
87 # editable page as a stylesheet
88 my $stylesheet=bestlink($page, $value.".css");
89 if (! length $stylesheet) {
90 return "[[meta ".gettext("stylesheet not found")."]]";
92 $meta{$page}.='<link href="'.urlto($stylesheet, $page).
93 '" rel="'.encode_entities($rel).
94 '" title="'.encode_entities($title).
95 "\" type=\"text/css\" />\n";
97 elsif ($key eq 'openid') {
98 if (exists $params{server}) {
99 $meta{$page}.='<link href="'.encode_entities($params{server}).
100 "\" rel=\"openid.server\" />\n";
102 $meta{$page}.='<link href="'.encode_entities($value).
103 "\" rel=\"openid.delegate\" />\n";
105 elsif ($key eq 'license') {
106 $meta{$page}.="<link rel=\"license\" href=\"#page_license\" />\n";
107 $license{$page}=$value;
109 elsif ($key eq 'copyright') {
110 $meta{$page}.="<link rel=\"copyright\" href=\"#page_copyright\" />\n";
111 $copyright{$page}=$value;
114 $meta{$page}.=scrub("<meta name=\"".encode_entities($key).
115 "\" content=\"".encode_entities($value)."\" />\n");
116 if ($key eq 'author') {
117 $author{$page}=$value;
119 elsif ($key eq 'authorurl') {
120 $authorurl{$page}=$value;
127 sub pagetemplate (@) { #{{{
129 my $page=$params{page};
130 my $destpage=$params{destpage};
131 my $template=$params{template};
133 $template->param(meta => $meta{$page})
134 if exists $meta{$page} && $template->query(name => "meta");
135 if (exists $title{$page} && $template->query(name => "title")) {
136 $template->param(title => $title{$page});
137 $template->param(title_overridden => 1);
139 $template->param(permalink => $permalink{$page})
140 if exists $permalink{$page} && $template->query(name => "permalink");
141 $template->param(author => $author{$page})
142 if exists $author{$page} && $template->query(name => "author");
143 $template->param(authorurl => $authorurl{$page})
144 if exists $authorurl{$page} && $template->query(name => "authorurl");
146 if ($page ne $destpage &&
147 ((exists $license{$page} && ! exists $license{$destpage}) ||
148 (exists $copyright{$page} && ! exists $copyright{$destpage}))) {
149 # Force a scan of the destpage to get its copyright/license
150 # info. If the info is declared after an inline, it will
151 # otherwise not be available at this point.
152 IkiWiki::scan($pagesources{$destpage});
155 if (exists $license{$page} && $template->query(name => "license") &&
156 ($page eq $destpage || ! exists $license{$destpage} ||
157 $license{$page} ne $license{$destpage})) {
158 $template->param(license => IkiWiki::linkify($page, $destpage, $license{$page}));
160 if (exists $copyright{$page} && $template->query(name => "copyright") &&
161 ($page eq $destpage || ! exists $copyright{$destpage} ||
162 $copyright{$page} ne $copyright{$destpage})) {
163 $template->param(copyright => IkiWiki::linkify($page, $destpage, $copyright{$page}));