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}}='';
27 delete $pagestate{$params{page}}{meta}{redir};
29 return $params{content};
33 if (IkiWiki::Plugin::htmlscrubber->can("sanitize")) {
34 return IkiWiki::Plugin::htmlscrubber::sanitize(content => shift);
41 sub preprocess (@) { #{{{
47 my $value=$params{$key};
49 my $page=$params{page};
51 my $destpage=$params{destpage};
52 delete $params{destpage};
53 delete $params{preview};
55 eval q{use HTML::Entities};
56 # Always dencode, even if encoding later, since it might not be
58 $value=decode_entities($value);
62 $meta{$page}.=scrub("<link href=\"".encode_entities($value)."\" ".
64 encode_entities($_)."=\"".encode_entities(decode_entities($params{$_}))."\""
70 push @{$links{$page}}, $value;
73 elsif ($key eq 'redir') {
74 return "" if $destpage ne $page;
76 if ($value !~ /^\w+:\/\//) {
77 add_depends($page, $value);
78 my $link=bestlink($page, $value);
80 return "[[meta ".gettext("redir page not found")."]]";
83 $value=urlto($link, $page);
86 # redir cycle detection
87 $pagestate{$page}{meta}{redir}=$link;
90 while (exists $pagestate{$at}{meta}{redir}) {
92 return "[[meta ".gettext("redir cycle is not allowed")."]]";
95 $at=$pagestate{$at}{meta}{redir};
99 $value=encode_entities($value);
101 my $delay=int(exists $params{delay} ? $params{delay} : 0);
102 my $redir="<meta http-equiv=\"refresh\" content=\"$delay; URL=$value\">";
104 $redir=scrub($redir);
106 $meta{$page}.=$redir;
108 elsif ($key eq 'title') {
109 $title{$page}=HTML::Entities::encode_numeric($value);
111 elsif ($key eq 'permalink') {
112 $permalink{$page}=$value;
113 $meta{$page}.=scrub("<link rel=\"bookmark\" href=\"".encode_entities($value)."\" />\n");
115 elsif ($key eq 'date') {
116 eval q{use Date::Parse};
118 my $time = str2time($value);
119 $IkiWiki::pagectime{$page}=$time if defined $time;
122 elsif ($key eq 'stylesheet') {
123 my $rel=exists $params{rel} ? $params{rel} : "alternate stylesheet";
124 my $title=exists $params{title} ? $params{title} : $value;
125 # adding .css to the value prevents using any old web
126 # editable page as a stylesheet
127 my $stylesheet=bestlink($page, $value.".css");
128 if (! length $stylesheet) {
129 return "[[meta ".gettext("stylesheet not found")."]]";
131 $meta{$page}.='<link href="'.urlto($stylesheet, $page).
132 '" rel="'.encode_entities($rel).
133 '" title="'.encode_entities($title).
134 "\" type=\"text/css\" />\n";
136 elsif ($key eq 'openid') {
137 if (exists $params{server}) {
138 $meta{$page}.='<link href="'.encode_entities($params{server}).
139 "\" rel=\"openid.server\" />\n";
141 $meta{$page}.='<link href="'.encode_entities($value).
142 "\" rel=\"openid.delegate\" />\n";
144 elsif ($key eq 'license') {
145 $meta{$page}.="<link rel=\"license\" href=\"#page_license\" />\n";
146 $license{$page}=$value;
148 elsif ($key eq 'copyright') {
149 $meta{$page}.="<link rel=\"copyright\" href=\"#page_copyright\" />\n";
150 $copyright{$page}=$value;
153 $meta{$page}.=scrub("<meta name=\"".encode_entities($key).
154 "\" content=\"".encode_entities($value)."\" />\n");
155 if ($key eq 'author') {
156 $author{$page}=$value;
158 elsif ($key eq 'authorurl') {
159 $authorurl{$page}=$value;
166 sub pagetemplate (@) { #{{{
168 my $page=$params{page};
169 my $destpage=$params{destpage};
170 my $template=$params{template};
172 $template->param(meta => $meta{$page})
173 if exists $meta{$page} && $template->query(name => "meta");
174 if (exists $title{$page} && $template->query(name => "title")) {
175 $template->param(title => $title{$page});
176 $template->param(title_overridden => 1);
178 $template->param(permalink => $permalink{$page})
179 if exists $permalink{$page} && $template->query(name => "permalink");
180 $template->param(author => $author{$page})
181 if exists $author{$page} && $template->query(name => "author");
182 $template->param(authorurl => $authorurl{$page})
183 if exists $authorurl{$page} && $template->query(name => "authorurl");
185 if ($page ne $destpage &&
186 ((exists $license{$page} && ! exists $license{$destpage}) ||
187 (exists $copyright{$page} && ! exists $copyright{$destpage}))) {
188 # Force a scan of the destpage to get its copyright/license
189 # info. If the info is declared after an inline, it will
190 # otherwise not be available at this point.
191 IkiWiki::scan($pagesources{$destpage});
194 if (exists $license{$page} && $template->query(name => "license") &&
195 ($page eq $destpage || ! exists $license{$destpage} ||
196 $license{$page} ne $license{$destpage})) {
197 $template->param(license => IkiWiki::linkify($page, $destpage, $license{$page}));
199 if (exists $copyright{$page} && $template->query(name => "copyright") &&
200 ($page eq $destpage || ! exists $copyright{$destpage} ||
201 $copyright{$page} ne $copyright{$destpage})) {
202 $template->param(copyright => IkiWiki::linkify($page, $destpage, $copyright{$page}));