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);
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 preprocess (@) { #{{{
50 my $value=$params{$key};
52 my $page=$params{page};
54 my $destpage=$params{destpage};
55 delete $params{destpage};
56 delete $params{preview};
58 eval q{use HTML::Entities};
59 # Always decode, even if encoding later, since it might not be
61 $value=decode_entities($value);
63 if ($key eq 'title') {
64 $title{$page}=HTML::Entities::encode_numeric($value);
66 elsif ($key eq 'permalink') {
67 $permalink{$page}=$value;
68 push @{$meta{$page}}, scrub('<link rel="bookmark" href="'.encode_entities($value).'" />');
70 elsif ($key eq 'date') {
71 eval q{use Date::Parse};
73 my $time = str2time($value);
74 $IkiWiki::pagectime{$page}=$time if defined $time;
77 elsif ($key eq 'stylesheet') {
78 my $rel=exists $params{rel} ? $params{rel} : "alternate stylesheet";
79 my $title=exists $params{title} ? $params{title} : $value;
80 # adding .css to the value prevents using any old web
81 # editable page as a stylesheet
82 my $stylesheet=bestlink($page, $value.".css");
83 if (! length $stylesheet) {
84 return "[[meta ".gettext("stylesheet not found")."]]";
86 push @{$meta{$page}}, '<link href="'.urlto($stylesheet, $page).
87 '" rel="'.encode_entities($rel).
88 '" title="'.encode_entities($title).
89 "\" type=\"text/css\" />";
91 elsif ($key eq 'openid') {
92 if (exists $params{server}) {
93 push @{$meta{$page}}, '<link href="'.encode_entities($params{server}).
94 '" rel="openid.server" />';
96 push @{$meta{$page}}, '<link href="'.encode_entities($value).
97 '" rel="openid.delegate" />';
99 elsif ($key eq 'license') {
100 push @{$meta{$page}}, '<link rel="license" href="#page_license" />';
101 $license{$page}=$value;
103 elsif ($key eq 'copyright') {
104 push @{$meta{$page}}, '<link rel="copyright" href="#page_copyright" />';
105 $copyright{$page}=$value;
107 elsif ($key eq 'redir') {
108 return "" if $page ne $destpage;
110 if ($value !~ /^\w+:\/\//) {
111 my ($redir_page, $redir_anchor) = split /\#/, $value;
113 add_depends($page, $redir_page);
114 my $link=bestlink($page, $redir_page);
115 if (! length $link) {
116 return "[[meta ".gettext("redir page not found")."]]";
119 $value=urlto($link, $page);
120 $value.='#'.$redir_anchor if defined $redir_anchor;
123 # redir cycle detection
124 $pagestate{$page}{meta}{redir}=$link;
127 while (exists $pagestate{$at}{meta}{redir}) {
129 return "[[meta ".gettext("redir cycle is not allowed")."]]";
132 $at=$pagestate{$at}{meta}{redir};
136 $value=encode_entities($value);
138 my $delay=int(exists $params{delay} ? $params{delay} : 0);
139 my $redir="<meta http-equiv=\"refresh\" content=\"$delay; URL=$value\">";
141 $redir=scrub($redir);
143 push @{$meta{$page}}, $redir;
145 elsif ($key eq 'link') {
146 return "[[meta ".gettext("link is no longer supported")."]]";
149 push @{$meta{$page}}, scrub('<meta name="'.encode_entities($key).
150 '" content="'.encode_entities($value).'" />');
151 if ($key eq 'author') {
152 $author{$page}=$value;
154 elsif ($key eq 'authorurl') {
155 $authorurl{$page}=$value;
162 sub pagetemplate (@) { #{{{
164 my $page=$params{page};
165 my $destpage=$params{destpage};
166 my $template=$params{template};
168 if (exists $meta{$page} && $template->query(name => "meta")) {
169 # avoid duplicate meta lines
171 $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$meta{$page}}));
173 if (exists $title{$page} && $template->query(name => "title")) {
174 $template->param(title => $title{$page});
175 $template->param(title_overridden => 1);
177 $template->param(permalink => $permalink{$page})
178 if exists $permalink{$page} && $template->query(name => "permalink");
179 $template->param(author => $author{$page})
180 if exists $author{$page} && $template->query(name => "author");
181 $template->param(authorurl => $authorurl{$page})
182 if exists $authorurl{$page} && $template->query(name => "authorurl");
184 if ($page ne $destpage &&
185 ((exists $license{$page} && ! exists $license{$destpage}) ||
186 (exists $copyright{$page} && ! exists $copyright{$destpage}))) {
187 # Force a scan of the destpage to get its copyright/license
188 # info. If the info is declared after an inline, it will
189 # otherwise not be available at this point.
190 IkiWiki::scan($pagesources{$destpage});
193 if (exists $license{$page} && $template->query(name => "license") &&
194 ($page eq $destpage || ! exists $license{$destpage} ||
195 $license{$page} ne $license{$destpage})) {
196 $template->param(license => IkiWiki::linkify($page, $destpage, $license{$page}));
198 if (exists $copyright{$page} && $template->query(name => "copyright") &&
199 ($page eq $destpage || ! exists $copyright{$destpage} ||
200 $copyright{$page} ne $copyright{$destpage})) {
201 $template->param(copyright => IkiWiki::linkify($page, $destpage, $copyright{$page}));