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') {
75 if ($value !~ /^\w+:\/\//) {
76 add_depends($page, $value);
77 my $link=bestlink($page, $value);
79 return "[[meta ".gettext("redir page not found")."]]";
81 $pagestate{$page}{meta}{redir}=$link;
82 if ($pagestate{$link}{meta}{redir}) {
83 # TODO: real cycle detection
84 return "[[meta ".gettext("redir not allowed to point to a page that contains a redir")."]]";
86 $value=urlto($link, $destpage);
90 $value=encode_entities($value);
92 my $delay=int(exists $params{delay} ? $params{delay} : 0);
93 my $redir="<meta http-equiv=\"refresh\" content=\"$delay; URL=$value\">";
99 elsif ($key eq 'title') {
100 $title{$page}=HTML::Entities::encode_numeric($value);
102 elsif ($key eq 'permalink') {
103 $permalink{$page}=$value;
104 $meta{$page}.=scrub("<link rel=\"bookmark\" href=\"".encode_entities($value)."\" />\n");
106 elsif ($key eq 'date') {
107 eval q{use Date::Parse};
109 my $time = str2time($value);
110 $IkiWiki::pagectime{$page}=$time if defined $time;
113 elsif ($key eq 'stylesheet') {
114 my $rel=exists $params{rel} ? $params{rel} : "alternate stylesheet";
115 my $title=exists $params{title} ? $params{title} : $value;
116 # adding .css to the value prevents using any old web
117 # editable page as a stylesheet
118 my $stylesheet=bestlink($page, $value.".css");
119 if (! length $stylesheet) {
120 return "[[meta ".gettext("stylesheet not found")."]]";
122 $meta{$page}.='<link href="'.urlto($stylesheet, $page).
123 '" rel="'.encode_entities($rel).
124 '" title="'.encode_entities($title).
125 "\" type=\"text/css\" />\n";
127 elsif ($key eq 'openid') {
128 if (exists $params{server}) {
129 $meta{$page}.='<link href="'.encode_entities($params{server}).
130 "\" rel=\"openid.server\" />\n";
132 $meta{$page}.='<link href="'.encode_entities($value).
133 "\" rel=\"openid.delegate\" />\n";
135 elsif ($key eq 'license') {
136 $meta{$page}.="<link rel=\"license\" href=\"#page_license\" />\n";
137 $license{$page}=$value;
139 elsif ($key eq 'copyright') {
140 $meta{$page}.="<link rel=\"copyright\" href=\"#page_copyright\" />\n";
141 $copyright{$page}=$value;
144 $meta{$page}.=scrub("<meta name=\"".encode_entities($key).
145 "\" content=\"".encode_entities($value)."\" />\n");
146 if ($key eq 'author') {
147 $author{$page}=$value;
149 elsif ($key eq 'authorurl') {
150 $authorurl{$page}=$value;
157 sub pagetemplate (@) { #{{{
159 my $page=$params{page};
160 my $destpage=$params{destpage};
161 my $template=$params{template};
163 $template->param(meta => $meta{$page})
164 if exists $meta{$page} && $template->query(name => "meta");
165 if (exists $title{$page} && $template->query(name => "title")) {
166 $template->param(title => $title{$page});
167 $template->param(title_overridden => 1);
169 $template->param(permalink => $permalink{$page})
170 if exists $permalink{$page} && $template->query(name => "permalink");
171 $template->param(author => $author{$page})
172 if exists $author{$page} && $template->query(name => "author");
173 $template->param(authorurl => $authorurl{$page})
174 if exists $authorurl{$page} && $template->query(name => "authorurl");
176 if ($page ne $destpage &&
177 ((exists $license{$page} && ! exists $license{$destpage}) ||
178 (exists $copyright{$page} && ! exists $copyright{$destpage}))) {
179 # Force a scan of the destpage to get its copyright/license
180 # info. If the info is declared after an inline, it will
181 # otherwise not be available at this point.
182 IkiWiki::scan($pagesources{$destpage});
185 if (exists $license{$page} && $template->query(name => "license") &&
186 ($page eq $destpage || ! exists $license{$destpage} ||
187 $license{$page} ne $license{$destpage})) {
188 $template->param(license => IkiWiki::linkify($page, $destpage, $license{$page}));
190 if (exists $copyright{$page} && $template->query(name => "copyright") &&
191 ($page eq $destpage || ! exists $copyright{$destpage} ||
192 $copyright{$page} ne $copyright{$destpage})) {
193 $template->param(copyright => IkiWiki::linkify($page, $destpage, $copyright{$page}));