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 add_depends($page, $value);
112 my $link=bestlink($page, $value);
113 if (! length $link) {
114 return "[[meta ".gettext("redir page not found")."]]";
117 $value=urlto($link, $page);
120 # redir cycle detection
121 $pagestate{$page}{meta}{redir}=$link;
124 while (exists $pagestate{$at}{meta}{redir}) {
126 return "[[meta ".gettext("redir cycle is not allowed")."]]";
129 $at=$pagestate{$at}{meta}{redir};
133 $value=encode_entities($value);
135 my $delay=int(exists $params{delay} ? $params{delay} : 0);
136 my $redir="<meta http-equiv=\"refresh\" content=\"$delay; URL=$value\">";
138 $redir=scrub($redir);
140 push @{$meta{$page}}, $redir;
142 elsif ($key eq 'link') {
143 return "[[meta ".gettext("link is no longer supported")."]]";
146 push @{$meta{$page}}, scrub('<meta name="'.encode_entities($key).
147 '" content="'.encode_entities($value).'" />');
148 if ($key eq 'author') {
149 $author{$page}=$value;
151 elsif ($key eq 'authorurl') {
152 $authorurl{$page}=$value;
159 sub pagetemplate (@) { #{{{
161 my $page=$params{page};
162 my $destpage=$params{destpage};
163 my $template=$params{template};
165 if (exists $meta{$page} && $template->query(name => "meta")) {
166 # avoid duplicate meta lines
168 $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$meta{$page}}));
170 if (exists $title{$page} && $template->query(name => "title")) {
171 $template->param(title => $title{$page});
172 $template->param(title_overridden => 1);
174 $template->param(permalink => $permalink{$page})
175 if exists $permalink{$page} && $template->query(name => "permalink");
176 $template->param(author => $author{$page})
177 if exists $author{$page} && $template->query(name => "author");
178 $template->param(authorurl => $authorurl{$page})
179 if exists $authorurl{$page} && $template->query(name => "authorurl");
181 if ($page ne $destpage &&
182 ((exists $license{$page} && ! exists $license{$destpage}) ||
183 (exists $copyright{$page} && ! exists $copyright{$destpage}))) {
184 # Force a scan of the destpage to get its copyright/license
185 # info. If the info is declared after an inline, it will
186 # otherwise not be available at this point.
187 IkiWiki::scan($pagesources{$destpage});
190 if (exists $license{$page} && $template->query(name => "license") &&
191 ($page eq $destpage || ! exists $license{$destpage} ||
192 $license{$page} ne $license{$destpage})) {
193 $template->param(license => IkiWiki::linkify($page, $destpage, $license{$page}));
195 if (exists $copyright{$page} && $template->query(name => "copyright") &&
196 ($page eq $destpage || ! exists $copyright{$destpage} ||
197 $copyright{$page} ne $copyright{$destpage})) {
198 $template->param(copyright => IkiWiki::linkify($page, $destpage, $copyright{$page}));