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};
93 The `trail` directive is supplied by the [[plugins/contrib/trail]]
94 plugin. It sets options for the trail represented by this page. Example usage:
96 \[[!trail sort="meta(date)" circular="no" pages="blog/posts/*"]]
98 The available options are:
100 * `sort`: sets a [[ikiwiki/pagespec/sorting]] order; if not specified, the
101 items of the trail are ordered according to the first link to each item
102 found on the trail page
104 * `circular`: if set to `yes` or `1`, the trail is made into a loop by
105 making the last page's "next" link point to the first page, and the first
106 page's "previous" link point to the last page
108 * `pages`: add the given pages to the trail
112 sub preprocess_trail (@) {
115 if (exists $params{circular}) {
116 $pagestate{$params{page}}{trail}{circular} =
117 IkiWiki::yesno($params{circular});
120 if (exists $params{sort}) {
121 $pagestate{$params{page}}{trail}{sort} = $params{sort};
124 if (exists $params{reverse}) {
125 $pagestate{$params{page}}{trail}{reverse} = $params{reverse};
128 if (exists $params{pages}) {
129 push @{$pagestate{$params{page}}{trail}{contents}}, "pagespec $params{pages}";
132 if (exists $params{pagenames}) {
133 my @list = map { "link $_" } split ' ', $params{pagenames};
134 push @{$pagestate{$params{page}}{trail}{contents}}, @list;
142 The `trailinline` directive is supplied by the [[plugins/contrib/trail]]
143 plugin. It behaves like the [[trail]] and [[inline]] directives combined.
144 Like [[inline]], it includes the selected pages into the page with the
145 directive and/or an RSS or Atom feed; like [[trail]], it turns the
146 included pages into a "trail" in which each page has a link to the
147 previous and next pages.
149 \[[!inline sort="meta(date)" circular="no" pages="blog/posts/*"]]
151 All the options for the [[inline]] and [[trail]] directives are valid.
153 The `show`, `skip` and `feedshow` options from [[inline]] do not apply
156 * `sort`: sets a [[ikiwiki/pagespec/sorting]] order; if not specified, the
157 items of the trail are ordered according to the first link to each item
158 found on the trail page
160 * `circular`: if set to `yes` or `1`, the trail is made into a loop by
161 making the last page's "next" link point to the first page, and the first
162 page's "previous" link point to the last page
164 * `pages`: add the given pages to the trail
168 sub preprocess_trailinline (@) {
169 preprocess_trail(@_);
170 return unless defined wantarray;
172 if (IkiWiki->can("preprocess_inline")) {
173 return IkiWiki::preprocess_inline(@_);
176 error("trailinline directive requires the inline plugin");
182 The `trailitem` directive is supplied by the [[plugins/contrib/trail]] plugin.
183 It is used like this:
185 \[[!trailitem some_other_page]]
187 to add `some_other_page` to the trail represented by this page, without
188 generating a visible hyperlink.
192 sub preprocess_trailitem (@) {
197 my $trail = $params{page};
199 $link = linkpage($link);
201 add_link($params{page}, $link, 'trail');
202 push @{$pagestate{$params{page}}{trail}{contents}}, "link $link";
209 The `traillink` directive is supplied by the [[plugins/contrib/trail]] plugin.
210 It generates a visible [[ikiwiki/WikiLink]], and also adds the linked page to
211 the trail represented by the page containing the directive.
213 In its simplest form, the first parameter is like the content of a WikiLink:
215 \[[!traillink some_other_page]]
217 The displayed text can also be overridden, either with a `|` symbol or with
220 \[[!traillink Click_here_to_start_the_trail|some_other_page]]
221 \[[!traillink some_other_page text="Click here to start the trail"]]
225 sub preprocess_traillink (@) {
230 my $trail = $params{page};
234 ([^\|]+) # 1: link text
238 (.+) # 2: page to link to
242 $link = linkpage($2);
244 add_link($params{page}, $link, 'trail');
245 push @{$pagestate{$params{page}}{trail}{contents}}, "link $link";
247 if (defined $linktext) {
248 $linktext = pagetitle($linktext);
251 if (exists $params{text}) {
252 $linktext = $params{text};
255 if (defined $linktext) {
256 return htmllink($trail, $params{destpage},
257 $link, linktext => $linktext);
260 return htmllink($trail, $params{destpage}, $link);
263 # trail => [member1, member2]
264 my %trail_to_members;
265 # member => { trail => [prev, next] }
266 # e.g. if %trail_to_members = (
267 # trail1 => ["member1", "member2"],
268 # trail2 => ["member0", "member1"],
271 # then $member_to_trails{member1} = {
272 # trail1 => [undef, "member2"],
273 # trail2 => ["member0", undef],
275 my %member_to_trails;
278 my %rebuild_trail_members;
281 my ($old, $new) = @_;
283 foreach my $trail (keys %$old) {
284 if (! exists $new->{$trail}) {
287 my ($old_p, $old_n) = @{$old->{$trail}};
288 my ($new_p, $new_n) = @{$new->{$trail}};
289 $old_p = "" unless defined $old_p;
290 $old_n = "" unless defined $old_n;
291 $new_p = "" unless defined $new_p;
292 $new_n = "" unless defined $new_n;
293 if ($old_p ne $new_p) {
296 if ($old_n ne $new_n) {
301 foreach my $trail (keys %$new) {
302 if (! exists $old->{$trail}) {
310 my $done_prerender = 0;
315 return if $done_prerender;
317 $origsubs{render_backlinks} = \&IkiWiki::render_backlinks;
318 inject(name => "IkiWiki::render_backlinks", call => \&render_backlinks);
320 %trail_to_members = ();
321 %member_to_trails = ();
323 foreach my $trail (keys %pagestate) {
324 next unless exists $pagestate{$trail}{trail}{contents};
327 my @contents = @{$pagestate{$trail}{trail}{contents}};
330 foreach my $c (@contents) {
331 if ($c =~ m/^pagespec (.*)$/) {
332 push @$members, pagespec_match_list($trail, $1);
334 elsif ($c =~ m/^link (.*)$/) {
335 my $best = bestlink($trail, $1);
336 push @$members, $best if length $best;
340 if (defined $pagestate{$trail}{trail}{sort}) {
342 @$members = pagespec_match_list($trail, 'internal(*)',
344 sort => $pagestate{$trail}{trail}{sort});
347 if (IkiWiki::yesno $pagestate{$trail}{trail}{reverse}) {
348 @$members = reverse @$members;
354 foreach my $member (@$members) {
355 push @tmp, $member unless $seen{$member};
360 for (my $i = 0; $i <= $#$members; $i++) {
361 my $member = $members->[$i];
363 $prev = $members->[$i - 1] if $i > 0;
364 my $next = $members->[$i + 1];
366 add_depends($member, $trail);
368 $member_to_trails{$member}{$trail} = [$prev, $next];
371 if ((scalar @$members) > 1 && $pagestate{$trail}{trail}{circular}) {
372 $member_to_trails{$members->[0]}{$trail}[0] = $members->[$#$members];
373 $member_to_trails{$members->[$#$members]}{$trail}[1] = $members->[0];
376 $trail_to_members{$trail} = $members;
379 foreach my $member (keys %pagestate) {
380 if (exists $pagestate{$member}{trail}{item} &&
381 ! exists $member_to_trails{$member}) {
382 $rebuild_trail_members{$member} = 1;
383 delete $pagestate{$member}{trailitem};
387 foreach my $member (keys %member_to_trails) {
388 if (! exists $pagestate{$member}{trail}{item}) {
389 $rebuild_trail_members{$member} = 1;
392 if (trails_differ($pagestate{$member}{trail}{item},
393 $member_to_trails{$member})) {
394 $rebuild_trail_members{$member} = 1;
398 $pagestate{$member}{trail}{item} = $member_to_trails{$member};
404 # This is called at about the right time that we can hijack it to render
406 sub render_backlinks ($) {
409 foreach my $member (keys %rebuild_trail_members) {
410 next unless exists $pagesources{$member};
412 IkiWiki::render($pagesources{$member}, sprintf(gettext("building %s, its previous or next page has changed"), $member));
415 $origsubs{render_backlinks}($blc);
420 if (defined ($pagestate{$page}{meta}{title})) {
421 return $pagestate{$page}{meta}{title};
423 return pagetitle(IkiWiki::basename($page));
428 sub pagetemplate (@) {
430 my $page = $params{page};
431 my $template = $params{template};
433 if ($template->query(name => 'trails') && ! $recursive) {
437 my $inner = template("trails.tmpl", blind_cache => 1);
438 IkiWiki::run_hooks(pagetemplate => sub {
439 shift->(%params, template => $inner)
441 $template->param(trails => $inner->output);
445 if ($template->query(name => 'trailloop')) {
450 # sort backlinks by page name to have a consistent order
451 foreach my $trail (sort keys %{$member_to_trails{$page}}) {
453 my $members = $trail_to_members{$trail};
454 my ($prev, $next) = @{$member_to_trails{$page}{$trail}};
455 my ($prevurl, $nexturl, $prevtitle, $nexttitle);
458 add_depends($params{destpage}, $prev);
459 $prevurl = urlto($prev, $page);
460 $prevtitle = title_of($prev);
464 add_depends($params{destpage}, $next);
465 $nexturl = urlto($next, $page);
466 $nexttitle = title_of($next);
471 prevtitle => $prevtitle,
474 nexttitle => $nexttitle,
477 trailtitle => title_of($trail),
478 trailurl => urlto($trail, $page),
482 $template->param(trailloop => \@trails);