2 # Copyright © 2008-2011 Joey Hess
3 # Copyright © 2009-2012 Simon McVittie <http://smcv.pseudorandom.co.uk/>
4 # Licensed under the GNU GPL, version 2, or any later version published by the
5 # Free Software Foundation
6 package IkiWiki::Plugin::trail;
13 hook(type => "getsetup", id => "trail", call => \&getsetup);
14 hook(type => "needsbuild", id => "trail", call => \&needsbuild);
15 hook(type => "preprocess", id => "trailoptions", call => \&preprocess_trailoptions, scan => 1);
16 hook(type => "preprocess", id => "trailitem", call => \&preprocess_trailitem, scan => 1);
17 hook(type => "preprocess", id => "trailitems", call => \&preprocess_trailitems, scan => 1);
18 hook(type => "preprocess", id => "traillink", call => \&preprocess_traillink, scan => 1);
19 hook(type => "pagetemplate", id => "trail", call => \&pagetemplate);
20 hook(type => "build_affected", id => "trail", call => \&build_affected);
25 If a page C<$T> is a trail, then it can have
29 =item * C<$pagestate{$T}{trail}{contents}>
31 Reference to an array of lists each containing either:
35 =item * C<[link, "link"]>
37 A link specification, pointing to the same page that C<[[link]]> would select
39 =item * C<[pagespec, "posts/*", "age", 0]>
41 A match by pagespec; the third array element is the sort order and the fourth
42 is whether to reverse sorting
46 =item * C<$pagestate{$T}{trail}{sort}>
48 A [[ikiwiki/pagespec/sorting]] order; if absent or undef, the trail is in
49 the order given by the links that form it
51 =item * C<$pagestate{$T}{trail}{circular}>
53 True if this trail is circular (i.e. going "next" from the last item is
54 allowed, and takes you back to the first)
56 =item * C<$pagestate{$T}{trail}{reverse}>
58 True if C<sort> is to be reversed.
62 If a page C<$M> is a member of a trail C<$T>, then it has
66 =item * C<$pagestate{$M}{trail}{item}{$T}[0]>
68 The page before this one in C<$T> at the last rebuild, or undef.
70 =item * C<$pagestate{$M}{trail}{item}{$T}[1]>
72 The page after this one in C<$T> at the last refresh, or undef.
88 foreach my $page (keys %pagestate) {
89 if (exists $pagestate{$page}{trail}) {
90 if (exists $pagesources{$page} &&
91 grep { $_ eq $pagesources{$page} } @$needsbuild) {
92 # Remove state, it will be re-added
93 # if the preprocessor directive is still
94 # there during the rebuild. {item} is the
95 # only thing that's added for items, not
96 # trails, and it's harmless to delete that -
97 # the item is being rebuilt anyway.
98 delete $pagestate{$page}{trail};
107 sub preprocess_trailoptions (@) {
110 if (exists $params{circular}) {
111 $pagestate{$params{page}}{trail}{circular} =
112 IkiWiki::yesno($params{circular});
115 if (exists $params{sort}) {
116 $pagestate{$params{page}}{trail}{sort} = $params{sort};
119 if (exists $params{reverse}) {
120 $pagestate{$params{page}}{trail}{reverse} = $params{reverse};
126 sub preprocess_trailitem (@) {
130 # avoid collecting everything in the preprocess stage if we already
131 # did in the scan stage
132 if (defined wantarray) {
133 return "" if $scanned;
140 my $trail = $params{page};
142 $link = linkpage($link);
144 add_link($params{page}, $link, 'trail');
145 push @{$pagestate{$params{page}}{trail}{contents}}, [link => $link];
150 sub preprocess_trailitems (@) {
153 # avoid collecting everything in the preprocess stage if we already
154 # did in the scan stage
155 if (defined wantarray) {
156 return "" if $scanned;
162 # trail members from a pagespec ought to be in some sort of order,
163 # and path is a nice obvious default
164 $params{sort} = 'path' unless exists $params{sort};
165 $params{reverse} = 'no' unless exists $params{reverse};
167 if (exists $params{pages}) {
168 push @{$pagestate{$params{page}}{trail}{contents}},
169 ["pagespec" => $params{pages}, $params{sort},
170 IkiWiki::yesno($params{reverse})];
173 if (exists $params{pagenames}) {
174 my @list = map { [link => $_] } split ' ', $params{pagenames};
175 push @{$pagestate{$params{page}}{trail}{contents}}, @list;
181 sub preprocess_traillink (@) {
186 my $trail = $params{page};
190 ([^\|]+) # 1: link text
194 (.+) # 2: page to link to
198 $link = linkpage($2);
200 add_link($params{page}, $link, 'trail');
202 # avoid collecting everything in the preprocess stage if we already
203 # did in the scan stage
205 if (defined wantarray) {
212 push @{$pagestate{$params{page}}{trail}{contents}}, [link => $link] unless $already;
214 if (defined $linktext) {
215 $linktext = pagetitle($linktext);
218 if (exists $params{text}) {
219 $linktext = $params{text};
222 if (defined $linktext) {
223 return htmllink($trail, $params{destpage},
224 $link, linktext => $linktext);
227 return htmllink($trail, $params{destpage}, $link);
230 # trail => [member1, member2]
231 my %trail_to_members;
232 # member => { trail => [prev, next] }
233 # e.g. if %trail_to_members = (
234 # trail1 => ["member1", "member2"],
235 # trail2 => ["member0", "member1"],
238 # then $member_to_trails{member1} = {
239 # trail1 => [undef, "member2"],
240 # trail2 => ["member0", undef],
242 my %member_to_trails;
245 my %rebuild_trail_members;
248 my ($old, $new) = @_;
250 foreach my $trail (keys %$old) {
251 if (! exists $new->{$trail}) {
254 my ($old_p, $old_n) = @{$old->{$trail}};
255 my ($new_p, $new_n) = @{$new->{$trail}};
256 $old_p = "" unless defined $old_p;
257 $old_n = "" unless defined $old_n;
258 $new_p = "" unless defined $new_p;
259 $new_n = "" unless defined $new_n;
260 if ($old_p ne $new_p) {
263 if ($old_n ne $new_n) {
268 foreach my $trail (keys %$new) {
269 if (! exists $old->{$trail}) {
277 my $done_prerender = 0;
280 return if $done_prerender;
282 %trail_to_members = ();
283 %member_to_trails = ();
285 foreach my $trail (keys %pagestate) {
286 next unless exists $pagestate{$trail}{trail}{contents};
289 my @contents = @{$pagestate{$trail}{trail}{contents}};
291 foreach my $c (@contents) {
292 if ($c->[0] eq 'pagespec') {
293 push @$members, pagespec_match_list($trail,
294 $c->[1], sort => $c->[2],
297 elsif ($c->[0] eq 'link') {
298 my $best = bestlink($trail, $c->[1]);
299 push @$members, $best if length $best;
303 if (defined $pagestate{$trail}{trail}{sort}) {
305 @$members = pagespec_match_list($trail, 'internal(*)',
307 sort => $pagestate{$trail}{trail}{sort});
310 if (IkiWiki::yesno $pagestate{$trail}{trail}{reverse}) {
311 @$members = reverse @$members;
317 foreach my $member (@$members) {
318 push @tmp, $member unless $seen{$member};
323 for (my $i = 0; $i <= $#$members; $i++) {
324 my $member = $members->[$i];
326 $prev = $members->[$i - 1] if $i > 0;
327 my $next = $members->[$i + 1];
329 add_depends($member, $trail);
331 $member_to_trails{$member}{$trail} = [$prev, $next];
334 if ((scalar @$members) > 1 && $pagestate{$trail}{trail}{circular}) {
335 $member_to_trails{$members->[0]}{$trail}[0] = $members->[$#$members];
336 $member_to_trails{$members->[$#$members]}{$trail}[1] = $members->[0];
339 $trail_to_members{$trail} = $members;
342 foreach my $member (keys %pagestate) {
343 if (exists $pagestate{$member}{trail}{item} &&
344 ! exists $member_to_trails{$member}) {
345 $rebuild_trail_members{$member} = 1;
346 delete $pagestate{$member}{trailitem};
350 foreach my $member (keys %member_to_trails) {
351 if (! exists $pagestate{$member}{trail}{item}) {
352 $rebuild_trail_members{$member} = 1;
355 if (trails_differ($pagestate{$member}{trail}{item},
356 $member_to_trails{$member})) {
357 $rebuild_trail_members{$member} = 1;
361 $pagestate{$member}{trail}{item} = $member_to_trails{$member};
370 foreach my $member (keys %rebuild_trail_members) {
371 $affected{$member} = sprintf(gettext("building %s, its previous or next page has changed"), $member);
379 if (defined ($pagestate{$page}{meta}{title})) {
380 return $pagestate{$page}{meta}{title};
382 return pagetitle(IkiWiki::basename($page));
387 sub pagetemplate (@) {
389 my $page = $params{page};
390 my $template = $params{template};
392 if ($template->query(name => 'trails') && ! $recursive) {
396 my $inner = template("trails.tmpl", blind_cache => 1);
397 IkiWiki::run_hooks(pagetemplate => sub {
398 shift->(%params, template => $inner)
400 $template->param(trails => $inner->output);
404 if ($template->query(name => 'trailloop')) {
409 # sort backlinks by page name to have a consistent order
410 foreach my $trail (sort keys %{$member_to_trails{$page}}) {
412 my $members = $trail_to_members{$trail};
413 my ($prev, $next) = @{$member_to_trails{$page}{$trail}};
414 my ($prevurl, $nexturl, $prevtitle, $nexttitle);
417 add_depends($params{destpage}, $prev);
418 $prevurl = urlto($prev, $page);
419 $prevtitle = title_of($prev);
423 add_depends($params{destpage}, $next);
424 $nexturl = urlto($next, $page);
425 $nexttitle = title_of($next);
430 prevtitle => $prevtitle,
433 nexttitle => $nexttitle,
436 trailtitle => title_of($trail),
437 trailurl => urlto($trail, $page),
441 $template->param(trailloop => \@trails);