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 $T is a trail, then it can have
27 # * $pagestate{$T}{trail}{contents}
28 # Reference to an array of lists each containing either:
30 # A link specification, pointing to the same page that [[link]]
32 # - [pagespec, "posts/*", "age", 0]
33 # A match by pagespec; the third array element is the sort order
34 # and the fourth is whether to reverse sorting
36 # * $pagestate{$T}{trail}{sort}
37 # A sorting order; if absent or undef, the trail is in the order given
38 # by the links that form it
40 # * $pagestate{$T}{trail}{circular}
41 # True if this trail is circular (i.e. going "next" from the last item is
42 # allowed, and takes you back to the first)
44 # * $pagestate{$T}{trail}{reverse}
45 # True if C<sort> is to be reversed.
47 # If a page $M is a member of a trail $T, then it has
49 # * $pagestate{$M}{trail}{item}{$T}[0]
50 # The page before this one in C<$T> at the last rebuild, or undef.
52 # * $pagestate{$M}{trail}{item}{$T}[1]
53 # The page after this one in C<$T> at the last refresh, or undef.
65 foreach my $page (keys %pagestate) {
66 if (exists $pagestate{$page}{trail}) {
67 if (exists $pagesources{$page} &&
68 grep { $_ eq $pagesources{$page} } @$needsbuild) {
69 # Remove state, it will be re-added
70 # if the preprocessor directive is still
71 # there during the rebuild. {item} is the
72 # only thing that's added for items, not
73 # trails, and it's harmless to delete that -
74 # the item is being rebuilt anyway.
75 delete $pagestate{$page}{trail};
84 sub preprocess_trailoptions (@) {
87 if (exists $params{circular}) {
88 $pagestate{$params{page}}{trail}{circular} =
89 IkiWiki::yesno($params{circular});
92 if (exists $params{sort}) {
93 $pagestate{$params{page}}{trail}{sort} = $params{sort};
96 if (exists $params{reverse}) {
97 $pagestate{$params{page}}{trail}{reverse} = $params{reverse};
103 sub preprocess_trailitem (@) {
107 # avoid collecting everything in the preprocess stage if we already
108 # did in the scan stage
109 if (defined wantarray) {
110 return "" if $scanned;
117 my $trail = $params{page};
119 $link = linkpage($link);
121 add_link($params{page}, $link, 'trail');
122 push @{$pagestate{$params{page}}{trail}{contents}}, [link => $link];
127 sub preprocess_trailitems (@) {
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;
139 # trail members from a pagespec ought to be in some sort of order,
140 # and path is a nice obvious default
141 $params{sort} = 'path' unless exists $params{sort};
142 $params{reverse} = 'no' unless exists $params{reverse};
144 if (exists $params{pages}) {
145 push @{$pagestate{$params{page}}{trail}{contents}},
146 ["pagespec" => $params{pages}, $params{sort},
147 IkiWiki::yesno($params{reverse})];
150 if (exists $params{pagenames}) {
151 my @list = map { [link => $_] } split ' ', $params{pagenames};
152 push @{$pagestate{$params{page}}{trail}{contents}}, @list;
158 sub preprocess_traillink (@) {
163 my $trail = $params{page};
167 ([^\|]+) # 1: link text
171 (.+) # 2: page to link to
175 $link = linkpage($2);
177 add_link($params{page}, $link, 'trail');
179 # avoid collecting everything in the preprocess stage if we already
180 # did in the scan stage
182 if (defined wantarray) {
189 push @{$pagestate{$params{page}}{trail}{contents}}, [link => $link] unless $already;
191 if (defined $linktext) {
192 $linktext = pagetitle($linktext);
195 if (exists $params{text}) {
196 $linktext = $params{text};
199 if (defined $linktext) {
200 return htmllink($trail, $params{destpage},
201 $link, linktext => $linktext);
204 return htmllink($trail, $params{destpage}, $link);
207 # trail => [member1, member2]
208 my %trail_to_members;
209 # member => { trail => [prev, next] }
210 # e.g. if %trail_to_members = (
211 # trail1 => ["member1", "member2"],
212 # trail2 => ["member0", "member1"],
215 # then $member_to_trails{member1} = {
216 # trail1 => [undef, "member2"],
217 # trail2 => ["member0", undef],
219 my %member_to_trails;
222 my %rebuild_trail_members;
225 my ($old, $new) = @_;
227 foreach my $trail (keys %$old) {
228 if (! exists $new->{$trail}) {
231 my ($old_p, $old_n) = @{$old->{$trail}};
232 my ($new_p, $new_n) = @{$new->{$trail}};
233 $old_p = "" unless defined $old_p;
234 $old_n = "" unless defined $old_n;
235 $new_p = "" unless defined $new_p;
236 $new_n = "" unless defined $new_n;
237 if ($old_p ne $new_p) {
240 if ($old_n ne $new_n) {
245 foreach my $trail (keys %$new) {
246 if (! exists $old->{$trail}) {
254 my $done_prerender = 0;
257 return if $done_prerender;
259 %trail_to_members = ();
260 %member_to_trails = ();
262 foreach my $trail (keys %pagestate) {
263 next unless exists $pagestate{$trail}{trail}{contents};
266 my @contents = @{$pagestate{$trail}{trail}{contents}};
268 foreach my $c (@contents) {
269 if ($c->[0] eq 'pagespec') {
270 push @$members, pagespec_match_list($trail,
271 $c->[1], sort => $c->[2],
274 elsif ($c->[0] eq 'link') {
275 my $best = bestlink($trail, $c->[1]);
276 push @$members, $best if length $best;
280 if (defined $pagestate{$trail}{trail}{sort}) {
282 @$members = pagespec_match_list($trail, 'internal(*)',
284 sort => $pagestate{$trail}{trail}{sort});
287 if (IkiWiki::yesno $pagestate{$trail}{trail}{reverse}) {
288 @$members = reverse @$members;
294 foreach my $member (@$members) {
295 push @tmp, $member unless $seen{$member};
300 for (my $i = 0; $i <= $#$members; $i++) {
301 my $member = $members->[$i];
303 $prev = $members->[$i - 1] if $i > 0;
304 my $next = $members->[$i + 1];
306 add_depends($member, $trail);
308 $member_to_trails{$member}{$trail} = [$prev, $next];
311 if ((scalar @$members) > 1 && $pagestate{$trail}{trail}{circular}) {
312 $member_to_trails{$members->[0]}{$trail}[0] = $members->[$#$members];
313 $member_to_trails{$members->[$#$members]}{$trail}[1] = $members->[0];
316 $trail_to_members{$trail} = $members;
319 foreach my $member (keys %pagestate) {
320 if (exists $pagestate{$member}{trail}{item} &&
321 ! exists $member_to_trails{$member}) {
322 $rebuild_trail_members{$member} = 1;
323 delete $pagestate{$member}{trailitem};
327 foreach my $member (keys %member_to_trails) {
328 if (! exists $pagestate{$member}{trail}{item}) {
329 $rebuild_trail_members{$member} = 1;
332 if (trails_differ($pagestate{$member}{trail}{item},
333 $member_to_trails{$member})) {
334 $rebuild_trail_members{$member} = 1;
338 $pagestate{$member}{trail}{item} = $member_to_trails{$member};
347 foreach my $member (keys %rebuild_trail_members) {
348 $affected{$member} = sprintf(gettext("building %s, its previous or next page has changed"), $member);
356 if (defined ($pagestate{$page}{meta}{title})) {
357 return $pagestate{$page}{meta}{title};
359 return pagetitle(IkiWiki::basename($page));
364 sub pagetemplate (@) {
366 my $page = $params{page};
367 my $template = $params{template};
369 if ($template->query(name => 'trails') && ! $recursive) {
373 my $inner = template("trails.tmpl", blind_cache => 1);
374 IkiWiki::run_hooks(pagetemplate => sub {
375 shift->(%params, template => $inner)
377 $template->param(trails => $inner->output);
381 if ($template->query(name => 'trailloop')) {
386 # sort backlinks by page name to have a consistent order
387 foreach my $trail (sort keys %{$member_to_trails{$page}}) {
389 my $members = $trail_to_members{$trail};
390 my ($prev, $next) = @{$member_to_trails{$page}{$trail}};
391 my ($prevurl, $nexturl, $prevtitle, $nexttitle);
394 add_depends($params{destpage}, $prev);
395 $prevurl = urlto($prev, $page);
396 $prevtitle = title_of($prev);
400 add_depends($params{destpage}, $next);
401 $nexturl = urlto($next, $page);
402 $nexttitle = title_of($next);
407 prevtitle => $prevtitle,
410 nexttitle => $nexttitle,
413 trailtitle => title_of($trail),
414 trailurl => urlto($trail, $page),
418 $template->param(trailloop => \@trails);