2 # Ikiwiki metadata plugin.
3 package IkiWiki::Plugin::meta;
18 hook(type => "needsbuild", id => "meta", call => \&needsbuild);
19 hook(type => "preprocess", id => "meta", call => \&preprocess, scan => 1);
20 hook(type => "pagetemplate", id => "meta", call => \&pagetemplate);
23 sub needsbuild (@) { #{{{
25 foreach my $page (keys %pagestate) {
26 if (exists $pagestate{$page}{meta}) {
27 if (grep { $_ eq $pagesources{$page} } @$needsbuild) {
28 # remove state, it will be re-added
29 # if the preprocessor directive is still
30 # there during the rebuild
31 delete $pagestate{$page}{meta};
38 if (IkiWiki::Plugin::htmlscrubber->can("sanitize")) {
39 return IkiWiki::Plugin::htmlscrubber::sanitize(content => shift);
46 sub htmlize ($$$) { #{{{
51 $text=IkiWiki::htmlize($page, pagetype($pagesources{$page}),
52 IkiWiki::linkify($page, $destpage,
53 IkiWiki::preprocess($page, $destpage, $text)));
55 # hack to get rid of enclosing junk added by markdown
63 sub preprocess (@) { #{{{
67 my $value=$params{$key};
69 my $page=$params{page};
71 my $destpage=$params{destpage};
72 delete $params{destpage};
73 delete $params{preview};
75 eval q{use HTML::Entities};
76 # Always decode, even if encoding later, since it might not be
78 $value=decode_entities($value);
80 # Metadata collection that needs to happen during the scan pass.
81 if ($key eq 'title') {
82 $title{$page}=HTML::Entities::encode_numeric($value);
84 elsif ($key eq 'license') {
85 push @{$meta{$page}}, '<link rel="license" href="#page_license" />';
86 $license{$page}=$value;
89 elsif ($key eq 'copyright') {
90 push @{$meta{$page}}, '<link rel="copyright" href="#page_copyright" />';
91 $copyright{$page}=$value;
94 elsif ($key eq 'link' && ! %params) {
96 push @{$links{$page}}, $value;
99 elsif ($key eq 'author') {
100 $author{$page}=$value;
103 elsif ($key eq 'authorurl') {
104 $authorurl{$page}=$value;
108 if (! defined wantarray) {
109 # avoid collecting duplicate data during scan pass
113 # Metadata collection that happens only during preprocessing pass.
114 if ($key eq 'date') {
115 eval q{use Date::Parse};
117 my $time = str2time($value);
118 $IkiWiki::pagectime{$page}=$time if defined $time;
121 elsif ($key eq 'permalink') {
122 $permalink{$page}=$value;
123 push @{$meta{$page}}, scrub('<link rel="bookmark" href="'.encode_entities($value).'" />');
125 elsif ($key eq 'stylesheet') {
126 my $rel=exists $params{rel} ? $params{rel} : "alternate stylesheet";
127 my $title=exists $params{title} ? $params{title} : $value;
128 # adding .css to the value prevents using any old web
129 # editable page as a stylesheet
130 my $stylesheet=bestlink($page, $value.".css");
131 if (! length $stylesheet) {
132 return "[[meta ".gettext("stylesheet not found")."]]";
134 push @{$meta{$page}}, '<link href="'.urlto($stylesheet, $page).
135 '" rel="'.encode_entities($rel).
136 '" title="'.encode_entities($title).
137 "\" type=\"text/css\" />";
139 elsif ($key eq 'openid') {
140 if (exists $params{server}) {
141 push @{$meta{$page}}, '<link href="'.encode_entities($params{server}).
142 '" rel="openid.server" />';
144 push @{$meta{$page}}, '<link href="'.encode_entities($value).
145 '" rel="openid.delegate" />';
147 elsif ($key eq 'redir') {
148 return "" if $page ne $destpage;
150 if ($value !~ /^\w+:\/\//) {
151 my ($redir_page, $redir_anchor) = split /\#/, $value;
153 add_depends($page, $redir_page);
154 my $link=bestlink($page, $redir_page);
155 if (! length $link) {
156 return "[[meta ".gettext("redir page not found")."]]";
159 $value=urlto($link, $page);
160 $value.='#'.$redir_anchor if defined $redir_anchor;
163 # redir cycle detection
164 $pagestate{$page}{meta}{redir}=$link;
167 while (exists $pagestate{$at}{meta}{redir}) {
169 return "[[meta ".gettext("redir cycle is not allowed")."]]";
172 $at=$pagestate{$at}{meta}{redir};
176 $value=encode_entities($value);
178 my $delay=int(exists $params{delay} ? $params{delay} : 0);
179 my $redir="<meta http-equiv=\"refresh\" content=\"$delay; URL=$value\">";
181 $redir=scrub($redir);
183 push @{$meta{$page}}, $redir;
185 elsif ($key eq 'link') {
187 $meta{$page}.=scrub("<link href=\"".encode_entities($value)."\" ".
189 encode_entities($_)."=\"".encode_entities(decode_entities($params{$_}))."\""
195 push @{$meta{$page}}, scrub('<meta name="'.encode_entities($key).
196 '" content="'.encode_entities($value).'" />');
202 sub pagetemplate (@) { #{{{
204 my $page=$params{page};
205 my $destpage=$params{destpage};
206 my $template=$params{template};
208 if (exists $meta{$page} && $template->query(name => "meta")) {
209 # avoid duplicate meta lines
211 $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$meta{$page}}));
213 if (exists $title{$page} && $template->query(name => "title")) {
214 $template->param(title => $title{$page});
215 $template->param(title_overridden => 1);
217 $template->param(permalink => $permalink{$page})
218 if exists $permalink{$page} && $template->query(name => "permalink");
219 $template->param(author => $author{$page})
220 if exists $author{$page} && $template->query(name => "author");
221 $template->param(authorurl => $authorurl{$page})
222 if exists $authorurl{$page} && $template->query(name => "authorurl");
224 if (exists $license{$page} && $template->query(name => "license") &&
225 ($page eq $destpage || ! exists $license{$destpage} ||
226 $license{$page} ne $license{$destpage})) {
227 $template->param(license => htmlize($page, $destpage, $license{$page}));
229 if (exists $copyright{$page} && $template->query(name => "copyright") &&
230 ($page eq $destpage || ! exists $copyright{$destpage} ||
231 $copyright{$page} ne $copyright{$destpage})) {
232 $template->param(copyright => htmlize($page, $destpage, $copyright{$page}));