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 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 if ($key eq 'title') {
81 $title{$page}=HTML::Entities::encode_numeric($value);
83 elsif ($key eq 'permalink') {
84 $permalink{$page}=$value;
85 push @{$meta{$page}}, scrub('<link rel="bookmark" href="'.encode_entities($value).'" />');
87 elsif ($key eq 'date') {
88 eval q{use Date::Parse};
90 my $time = str2time($value);
91 $IkiWiki::pagectime{$page}=$time if defined $time;
94 elsif ($key eq 'stylesheet') {
95 my $rel=exists $params{rel} ? $params{rel} : "alternate stylesheet";
96 my $title=exists $params{title} ? $params{title} : $value;
97 # adding .css to the value prevents using any old web
98 # editable page as a stylesheet
99 my $stylesheet=bestlink($page, $value.".css");
100 if (! length $stylesheet) {
101 return "[[meta ".gettext("stylesheet not found")."]]";
103 push @{$meta{$page}}, '<link href="'.urlto($stylesheet, $page).
104 '" rel="'.encode_entities($rel).
105 '" title="'.encode_entities($title).
106 "\" type=\"text/css\" />";
108 elsif ($key eq 'openid') {
109 if (exists $params{server}) {
110 push @{$meta{$page}}, '<link href="'.encode_entities($params{server}).
111 '" rel="openid.server" />';
113 push @{$meta{$page}}, '<link href="'.encode_entities($value).
114 '" rel="openid.delegate" />';
116 elsif ($key eq 'license') {
117 push @{$meta{$page}}, '<link rel="license" href="#page_license" />';
118 $license{$page}=$value;
120 elsif ($key eq 'copyright') {
121 push @{$meta{$page}}, '<link rel="copyright" href="#page_copyright" />';
122 $copyright{$page}=$value;
124 elsif ($key eq 'redir') {
125 return "" if $page ne $destpage;
127 if ($value !~ /^\w+:\/\//) {
128 my ($redir_page, $redir_anchor) = split /\#/, $value;
130 add_depends($page, $redir_page);
131 my $link=bestlink($page, $redir_page);
132 if (! length $link) {
133 return "[[meta ".gettext("redir page not found")."]]";
136 $value=urlto($link, $page);
137 $value.='#'.$redir_anchor if defined $redir_anchor;
140 # redir cycle detection
141 $pagestate{$page}{meta}{redir}=$link;
144 while (exists $pagestate{$at}{meta}{redir}) {
146 return "[[meta ".gettext("redir cycle is not allowed")."]]";
149 $at=$pagestate{$at}{meta}{redir};
153 $value=encode_entities($value);
155 my $delay=int(exists $params{delay} ? $params{delay} : 0);
156 my $redir="<meta http-equiv=\"refresh\" content=\"$delay; URL=$value\">";
158 $redir=scrub($redir);
160 push @{$meta{$page}}, $redir;
162 elsif ($key eq 'link') {
163 return "[[meta ".gettext("link is no longer supported")."]]";
166 push @{$meta{$page}}, scrub('<meta name="'.encode_entities($key).
167 '" content="'.encode_entities($value).'" />');
168 if ($key eq 'author') {
169 $author{$page}=$value;
171 elsif ($key eq 'authorurl') {
172 $authorurl{$page}=$value;
179 sub pagetemplate (@) { #{{{
181 my $page=$params{page};
182 my $destpage=$params{destpage};
183 my $template=$params{template};
185 if (exists $meta{$page} && $template->query(name => "meta")) {
186 # avoid duplicate meta lines
188 $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$meta{$page}}));
190 if (exists $title{$page} && $template->query(name => "title")) {
191 $template->param(title => $title{$page});
192 $template->param(title_overridden => 1);
194 $template->param(permalink => $permalink{$page})
195 if exists $permalink{$page} && $template->query(name => "permalink");
196 $template->param(author => $author{$page})
197 if exists $author{$page} && $template->query(name => "author");
198 $template->param(authorurl => $authorurl{$page})
199 if exists $authorurl{$page} && $template->query(name => "authorurl");
201 if ($page ne $destpage &&
202 ((exists $license{$page} && ! exists $license{$destpage}) ||
203 (exists $copyright{$page} && ! exists $copyright{$destpage}))) {
204 # Force a scan of the destpage to get its copyright/license
205 # info. If the info is declared after an inline, it will
206 # otherwise not be available at this point.
207 IkiWiki::scan($pagesources{$destpage});
210 if (exists $license{$page} && $template->query(name => "license") &&
211 ($page eq $destpage || ! exists $license{$destpage} ||
212 $license{$page} ne $license{$destpage})) {
213 $template->param(license => htmlize($page, $destpage, $license{$page}));
215 if (exists $copyright{$page} && $template->query(name => "copyright") &&
216 ($page eq $destpage || ! exists $copyright{$destpage} ||
217 $copyright{$page} ne $copyright{$destpage})) {
218 $template->param(copyright => htmlize($page, $destpage, $copyright{$page}));