2 # Copyright © 2008-2011 Joey Hess
3 # Copyright © 2009-2011 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 => "trail", call => \&preprocess_trail, scan => 1);
16 hook(type => "preprocess", id => "trailinline", call => \&preprocess_trailinline, scan => 1);
17 hook(type => "preprocess", id => "trailitem", call => \&preprocess_trailitem, scan => 1);
18 hook(type => "preprocess", id => "traillink", call => \&preprocess_traillink, scan => 1);
19 hook(type => "pagetemplate", id => "trail", call => \&pagetemplate);
24 If a page C<$T> is a trail, then it can have
28 =item * C<$pagestate{$T}{trail}{contents}>
30 Reference to an array of pagespecs or links in the trail.
32 =item * C<$pagestate{$T}{trail}{sort}>
34 A [[ikiwiki/pagespec/sorting]] order; if absent or undef, the trail is in
35 the order given by the links that form it
37 =item * C<$pagestate{$T}{trail}{circular}>
39 True if this trail is circular (i.e. going "next" from the last item is
40 allowed, and takes you back to the first)
42 =item * C<$pagestate{$T}{trail}{reverse}>
44 True if C<sort> is to be reversed.
48 If a page C<$M> is a member of a trail C<$T>, then it has
52 =item * C<$pagestate{$M}{trail}{item}{$T}[0]>
54 The page before this one in C<$T> at the last rebuild, or undef.
56 =item * C<$pagestate{$M}{trail}{item}{$T}[1]>
58 The page after this one in C<$T> at the last refresh, or undef.
74 foreach my $page (keys %pagestate) {
75 if (exists $pagestate{$page}{trail}) {
76 if (exists $pagesources{$page} &&
77 grep { $_ eq $pagesources{$page} } @$needsbuild) {
78 # Remove state, it will be re-added
79 # if the preprocessor directive is still
80 # there during the rebuild. {item} is the
81 # only thing that's added for items, not
82 # trails, and it's harmless to delete that -
83 # the item is being rebuilt anyway.
84 delete $pagestate{$page}{trail};
95 The `trail` directive is supplied by the [[plugins/contrib/trail]]
96 plugin. It sets options for the trail represented by this page. Example usage:
98 \[[!trail sort="meta(date)" circular="no" pages="blog/posts/*"]]
100 The available options are:
102 * `sort`: sets a [[ikiwiki/pagespec/sorting]] order; if not specified, the
103 items of the trail are ordered according to the first link to each item
104 found on the trail page
106 * `circular`: if set to `yes` or `1`, the trail is made into a loop by
107 making the last page's "next" link point to the first page, and the first
108 page's "previous" link point to the last page
110 * `pages`: add the given pages to the trail
114 sub preprocess_trail (@) {
117 # avoid collecting everything in the preprocess stage if we already
118 # did in the scan stage
119 if (defined wantarray) {
120 return "" if $scanned;
126 if (exists $params{circular}) {
127 $pagestate{$params{page}}{trail}{circular} =
128 IkiWiki::yesno($params{circular});
131 if (exists $params{sort}) {
132 $pagestate{$params{page}}{trail}{sort} = $params{sort};
135 if (exists $params{reverse}) {
136 $pagestate{$params{page}}{trail}{reverse} = $params{reverse};
139 if (exists $params{pages}) {
140 push @{$pagestate{$params{page}}{trail}{contents}}, "pagespec $params{pages}";
143 if (exists $params{pagenames}) {
144 my @list = map { "link $_" } split ' ', $params{pagenames};
145 push @{$pagestate{$params{page}}{trail}{contents}}, @list;
153 The `trailinline` directive is supplied by the [[plugins/contrib/trail]]
154 plugin. It behaves like the [[trail]] and [[inline]] directives combined.
155 Like [[inline]], it includes the selected pages into the page with the
156 directive and/or an RSS or Atom feed; like [[trail]], it turns the
157 included pages into a "trail" in which each page has a link to the
158 previous and next pages.
160 \[[!inline sort="meta(date)" circular="no" pages="blog/posts/*"]]
162 All the options for the [[inline]] and [[trail]] directives are valid.
164 The `show`, `skip` and `feedshow` options from [[inline]] do not apply
167 * `sort`: sets a [[ikiwiki/pagespec/sorting]] order; if not specified, the
168 items of the trail are ordered according to the first link to each item
169 found on the trail page
171 * `circular`: if set to `yes` or `1`, the trail is made into a loop by
172 making the last page's "next" link point to the first page, and the first
173 page's "previous" link point to the last page
175 * `pages`: add the given pages to the trail
179 sub preprocess_trailinline (@) {
182 if (defined wantarray) {
183 scalar preprocess_trail(%params);
185 if (IkiWiki->can("preprocess_inline")) {
186 return IkiWiki::preprocess_inline(@_);
189 error("trailinline directive requires the inline plugin");
193 preprocess_trail(%params);
199 The `trailitem` directive is supplied by the [[plugins/contrib/trail]] plugin.
200 It is used like this:
202 \[[!trailitem some_other_page]]
204 to add `some_other_page` to the trail represented by this page, without
205 generating a visible hyperlink.
209 sub preprocess_trailitem (@) {
213 # avoid collecting everything in the preprocess stage if we already
214 # did in the scan stage
215 if (defined wantarray) {
216 return "" if $scanned;
223 my $trail = $params{page};
225 $link = linkpage($link);
227 add_link($params{page}, $link, 'trail');
228 push @{$pagestate{$params{page}}{trail}{contents}}, "link $link";
235 The `traillink` directive is supplied by the [[plugins/contrib/trail]] plugin.
236 It generates a visible [[ikiwiki/WikiLink]], and also adds the linked page to
237 the trail represented by the page containing the directive.
239 In its simplest form, the first parameter is like the content of a WikiLink:
241 \[[!traillink some_other_page]]
243 The displayed text can also be overridden, either with a `|` symbol or with
246 \[[!traillink Click_here_to_start_the_trail|some_other_page]]
247 \[[!traillink some_other_page text="Click here to start the trail"]]
251 sub preprocess_traillink (@) {
256 my $trail = $params{page};
260 ([^\|]+) # 1: link text
264 (.+) # 2: page to link to
268 $link = linkpage($2);
270 add_link($params{page}, $link, 'trail');
272 # avoid collecting everything in the preprocess stage if we already
273 # did in the scan stage
275 if (defined wantarray) {
282 push @{$pagestate{$params{page}}{trail}{contents}}, [link => $link] unless $already;
284 if (defined $linktext) {
285 $linktext = pagetitle($linktext);
288 if (exists $params{text}) {
289 $linktext = $params{text};
292 if (defined $linktext) {
293 return htmllink($trail, $params{destpage},
294 $link, linktext => $linktext);
297 return htmllink($trail, $params{destpage}, $link);
300 # trail => [member1, member2]
301 my %trail_to_members;
302 # member => { trail => [prev, next] }
303 # e.g. if %trail_to_members = (
304 # trail1 => ["member1", "member2"],
305 # trail2 => ["member0", "member1"],
308 # then $member_to_trails{member1} = {
309 # trail1 => [undef, "member2"],
310 # trail2 => ["member0", undef],
312 my %member_to_trails;
315 my %rebuild_trail_members;
318 my ($old, $new) = @_;
320 foreach my $trail (keys %$old) {
321 if (! exists $new->{$trail}) {
324 my ($old_p, $old_n) = @{$old->{$trail}};
325 my ($new_p, $new_n) = @{$new->{$trail}};
326 $old_p = "" unless defined $old_p;
327 $old_n = "" unless defined $old_n;
328 $new_p = "" unless defined $new_p;
329 $new_n = "" unless defined $new_n;
330 if ($old_p ne $new_p) {
333 if ($old_n ne $new_n) {
338 foreach my $trail (keys %$new) {
339 if (! exists $old->{$trail}) {
347 my $done_prerender = 0;
352 return if $done_prerender;
354 $origsubs{render_backlinks} = \&IkiWiki::render_backlinks;
355 inject(name => "IkiWiki::render_backlinks", call => \&render_backlinks);
357 %trail_to_members = ();
358 %member_to_trails = ();
360 foreach my $trail (keys %pagestate) {
361 next unless exists $pagestate{$trail}{trail}{contents};
364 my @contents = @{$pagestate{$trail}{trail}{contents}};
367 foreach my $c (@contents) {
368 if ($c =~ m/^pagespec (.*)$/) {
369 push @$members, pagespec_match_list($trail, $1);
371 elsif ($c =~ m/^link (.*)$/) {
372 my $best = bestlink($trail, $1);
373 push @$members, $best if length $best;
377 if (defined $pagestate{$trail}{trail}{sort}) {
379 @$members = pagespec_match_list($trail, 'internal(*)',
381 sort => $pagestate{$trail}{trail}{sort});
384 if (IkiWiki::yesno $pagestate{$trail}{trail}{reverse}) {
385 @$members = reverse @$members;
391 foreach my $member (@$members) {
392 push @tmp, $member unless $seen{$member};
397 for (my $i = 0; $i <= $#$members; $i++) {
398 my $member = $members->[$i];
400 $prev = $members->[$i - 1] if $i > 0;
401 my $next = $members->[$i + 1];
403 add_depends($member, $trail);
405 $member_to_trails{$member}{$trail} = [$prev, $next];
408 if ((scalar @$members) > 1 && $pagestate{$trail}{trail}{circular}) {
409 $member_to_trails{$members->[0]}{$trail}[0] = $members->[$#$members];
410 $member_to_trails{$members->[$#$members]}{$trail}[1] = $members->[0];
413 $trail_to_members{$trail} = $members;
416 foreach my $member (keys %pagestate) {
417 if (exists $pagestate{$member}{trail}{item} &&
418 ! exists $member_to_trails{$member}) {
419 $rebuild_trail_members{$member} = 1;
420 delete $pagestate{$member}{trailitem};
424 foreach my $member (keys %member_to_trails) {
425 if (! exists $pagestate{$member}{trail}{item}) {
426 $rebuild_trail_members{$member} = 1;
429 if (trails_differ($pagestate{$member}{trail}{item},
430 $member_to_trails{$member})) {
431 $rebuild_trail_members{$member} = 1;
435 $pagestate{$member}{trail}{item} = $member_to_trails{$member};
441 # This is called at about the right time that we can hijack it to render
443 sub render_backlinks ($) {
446 foreach my $member (keys %rebuild_trail_members) {
447 next unless exists $pagesources{$member};
449 IkiWiki::render($pagesources{$member}, sprintf(gettext("building %s, its previous or next page has changed"), $member));
452 $origsubs{render_backlinks}($blc);
457 if (defined ($pagestate{$page}{meta}{title})) {
458 return $pagestate{$page}{meta}{title};
460 return pagetitle(IkiWiki::basename($page));
465 sub pagetemplate (@) {
467 my $page = $params{page};
468 my $template = $params{template};
470 if ($template->query(name => 'trails') && ! $recursive) {
474 my $inner = template("trails.tmpl", blind_cache => 1);
475 IkiWiki::run_hooks(pagetemplate => sub {
476 shift->(%params, template => $inner)
478 $template->param(trails => $inner->output);
482 if ($template->query(name => 'trailloop')) {
487 # sort backlinks by page name to have a consistent order
488 foreach my $trail (sort keys %{$member_to_trails{$page}}) {
490 my $members = $trail_to_members{$trail};
491 my ($prev, $next) = @{$member_to_trails{$page}{$trail}};
492 my ($prevurl, $nexturl, $prevtitle, $nexttitle);
495 add_depends($params{destpage}, $prev);
496 $prevurl = urlto($prev, $page);
497 $prevtitle = title_of($prev);
501 add_depends($params{destpage}, $next);
502 $nexturl = urlto($next, $page);
503 $nexttitle = title_of($next);
508 prevtitle => $prevtitle,
511 nexttitle => $nexttitle,
514 trailtitle => title_of($trail),
515 trailurl => urlto($trail, $page),
519 $template->param(trailloop => \@trails);