]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/trail.pm
Merge remote-tracking branch 'smcv/ready/trail'
[git.ikiwiki.info.git] / IkiWiki / Plugin / trail.pm
1 #!/usr/bin/perl
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;
8 use warnings;
9 use strict;
10 use IkiWiki 3.00;
12 sub import {
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);
21 }
23 # Page state
24
25 # If a page $T is a trail, then it can have
26
27 # * $pagestate{$T}{trail}{contents} 
28 #   Reference to an array of lists each containing either:
29 #     - [pagenames => "page1", "page2"]
30 #       Those literal pages
31 #     - [link => "link"]
32 #       A link specification, pointing to the same page that [[link]]
33 #       would select
34 #     - [pagespec => "posts/*", "age", 0]
35 #       A match by pagespec; the third array element is the sort order
36 #       and the fourth is whether to reverse sorting
37
38 # * $pagestate{$T}{trail}{sort}
39 #   A sorting order; if absent or undef, the trail is in the order given
40 #   by the links that form it
41 #
42 # * $pagestate{$T}{trail}{circular}
43 #   True if this trail is circular (i.e. going "next" from the last item is
44 #   allowed, and takes you back to the first)
45 #
46 # * $pagestate{$T}{trail}{reverse}
47 #   True if C<sort> is to be reversed.
48
49 # If a page $M is a member of a trail $T, then it has
50 #
51 # * $pagestate{$M}{trail}{item}{$T}[0]
52 #   The page before this one in C<$T> at the last rebuild, or undef.
53 #
54 # * $pagestate{$M}{trail}{item}{$T}[1]
55 #   The page after this one in C<$T> at the last refresh, or undef.
57 sub getsetup () {
58         return
59                 plugin => {
60                         safe => 1,
61                         rebuild => undef,
62                 },
63 }
65 # Cache of pages' old titles, so we can tell whether they changed
66 my %old_trail_titles;
68 sub needsbuild (@) {
69         my $needsbuild=shift;
71         foreach my $page (keys %pagestate) {
72                 if (exists $pagestate{$page}{trail}) {
73                         if (exists $pagesources{$page} &&
74                             grep { $_ eq $pagesources{$page} } @$needsbuild) {
75                                 # Remember its title, so we can know whether
76                                 # it changed.
77                                 $old_trail_titles{$page} = title_of($page);
79                                 # Remove state, it will be re-added
80                                 # if the preprocessor directive is still
81                                 # there during the rebuild. {item} is the
82                                 # only thing that's added for items, not
83                                 # trails, and it's harmless to delete that -
84                                 # the item is being rebuilt anyway.
85                                 delete $pagestate{$page}{trail};
86                         }
87                 }
88         }
90         return $needsbuild;
91 }
93 my $scanned = 0;
95 sub preprocess_trailoptions (@) {
96         my %params = @_;
98         if (exists $params{circular}) {
99                 $pagestate{$params{page}}{trail}{circular} =
100                         IkiWiki::yesno($params{circular});
101         }
103         if (exists $params{sort}) {
104                 $pagestate{$params{page}}{trail}{sort} = $params{sort};
105         }
107         if (exists $params{reverse}) {
108                 $pagestate{$params{page}}{trail}{reverse} = $params{reverse};
109         }
111         return "";
114 sub preprocess_trailitem (@) {
115         my $link = shift;
116         shift;
118         # avoid collecting everything in the preprocess stage if we already
119         # did in the scan stage
120         if (defined wantarray) {
121                 return "" if $scanned;
122         }
123         else {
124                 $scanned = 1;
125         }
127         my %params = @_;
128         my $trail = $params{page};
130         $link = linkpage($link);
132         add_link($params{page}, $link, 'trail');
133         push @{$pagestate{$params{page}}{trail}{contents}}, [link => $link];
135         return "";
138 sub preprocess_trailitems (@) {
139         my %params = @_;
141         # avoid collecting everything in the preprocess stage if we already
142         # did in the scan stage
143         if (defined wantarray) {
144                 return "" if $scanned;
145         }
146         else {
147                 $scanned = 1;
148         }
150         # trail members from a pagespec ought to be in some sort of order,
151         # and path is a nice obvious default
152         $params{sort} = 'path' unless exists $params{sort};
153         $params{reverse} = 'no' unless exists $params{reverse};
155         if (exists $params{pages}) {
156                 push @{$pagestate{$params{page}}{trail}{contents}},
157                         ["pagespec" => $params{pages}, $params{sort},
158                                 IkiWiki::yesno($params{reverse})];
159         }
161         if (exists $params{pagenames}) {
162                 push @{$pagestate{$params{page}}{trail}{contents}},
163                         [pagenames => (split ' ', $params{pagenames})];
164         }
166         return "";
169 sub preprocess_traillink (@) {
170         my $link = shift;
171         shift;
173         my %params = @_;
174         my $trail = $params{page};
176         $link =~ qr{
177                         (?:
178                                 ([^\|]+)        # 1: link text
179                                 \|              # followed by |
180                         )?                      # optional
182                         (.+)                    # 2: page to link to
183                 }x;
185         my $linktext = $1;
186         $link = linkpage($2);
188         add_link($params{page}, $link, 'trail');
190         # avoid collecting everything in the preprocess stage if we already
191         # did in the scan stage
192         my $already;
193         if (defined wantarray) {
194                 $already = $scanned;
195         }
196         else {
197                 $scanned = 1;
198         }
200         push @{$pagestate{$params{page}}{trail}{contents}}, [link => $link] unless $already;
202         if (defined $linktext) {
203                 $linktext = pagetitle($linktext);
204         }
206         if (exists $params{text}) {
207                 $linktext = $params{text};
208         }
210         if (defined $linktext) {
211                 return htmllink($trail, $params{destpage},
212                         $link, linktext => $linktext);
213         }
215         return htmllink($trail, $params{destpage}, $link);
218 # trail => [member1, member2]
219 my %trail_to_members;
220 # member => { trail => [prev, next] }
221 # e.g. if %trail_to_members = (
222 #       trail1 => ["member1", "member2"],
223 #       trail2 => ["member0", "member1"],
224 # )
226 # then $member_to_trails{member1} = {
227 #       trail1 => [undef, "member2"],
228 #       trail2 => ["member0", undef],
229 # }
230 my %member_to_trails;
232 # member => 1
233 my %rebuild_trail_members;
235 sub trails_differ {
236         my ($old, $new) = @_;
238         foreach my $trail (keys %$old) {
239                 if (! exists $new->{$trail}) {
240                         return 1;
241                 }
243                 if (exists $old_trail_titles{$trail} &&
244                         title_of($trail) ne $old_trail_titles{$trail}) {
245                         return 1;
246                 }
248                 my ($old_p, $old_n) = @{$old->{$trail}};
249                 my ($new_p, $new_n) = @{$new->{$trail}};
250                 $old_p = "" unless defined $old_p;
251                 $old_n = "" unless defined $old_n;
252                 $new_p = "" unless defined $new_p;
253                 $new_n = "" unless defined $new_n;
254                 if ($old_p ne $new_p) {
255                         return 1;
256                 }
258                 if (exists $old_trail_titles{$old_p} &&
259                         title_of($old_p) ne $old_trail_titles{$old_p}) {
260                         return 1;
261                 }
263                 if ($old_n ne $new_n) {
264                         return 1;
265                 }
267                 if (exists $old_trail_titles{$old_n} &&
268                         title_of($old_n) ne $old_trail_titles{$old_n}) {
269                         return 1;
270                 }
271         }
273         foreach my $trail (keys %$new) {
274                 if (! exists $old->{$trail}) {
275                         return 1;
276                 }
277         }
279         return 0;
282 my $done_prerender = 0;
284 sub prerender {
285         return if $done_prerender;
287         %trail_to_members = ();
288         %member_to_trails = ();
290         foreach my $trail (keys %pagestate) {
291                 next unless exists $pagestate{$trail}{trail}{contents};
293                 my $members = [];
294                 my @contents = @{$pagestate{$trail}{trail}{contents}};
296                 foreach my $c (@contents) {
297                         if ($c->[0] eq 'pagespec') {
298                                 push @$members, pagespec_match_list($trail,
299                                         $c->[1], sort => $c->[2],
300                                         reverse => $c->[3]);
301                         }
302                         elsif ($c->[0] eq 'pagenames') {
303                                 my @pagenames = @$c;
304                                 shift @pagenames;
305                                 foreach my $page (@pagenames) {
306                                         if (exists $pagesources{$page}) {
307                                                 push @$members, $page;
308                                         }
309                                         else {
310                                                 # rebuild trail if it turns up
311                                                 add_depends($trail, $page, deptype("presence"));
312                                         }
313                                 }
314                         }
315                         elsif ($c->[0] eq 'link') {
316                                 my $best = bestlink($trail, $c->[1]);
317                                 push @$members, $best if length $best;
318                         }
319                 }
321                 if (defined $pagestate{$trail}{trail}{sort}) {
322                         # re-sort
323                         @$members = pagespec_match_list($trail, 'internal(*)',
324                                 list => $members,
325                                 sort => $pagestate{$trail}{trail}{sort});
326                 }
328                 if (IkiWiki::yesno $pagestate{$trail}{trail}{reverse}) {
329                         @$members = reverse @$members;
330                 }
332                 # uniquify
333                 my %seen;
334                 my @tmp;
335                 foreach my $member (@$members) {
336                         push @tmp, $member unless $seen{$member};
337                         $seen{$member} = 1;
338                 }
339                 $members = [@tmp];
341                 for (my $i = 0; $i <= $#$members; $i++) {
342                         my $member = $members->[$i];
343                         my $prev;
344                         $prev = $members->[$i - 1] if $i > 0;
345                         my $next = $members->[$i + 1];
347                         $member_to_trails{$member}{$trail} = [$prev, $next];
348                 }
350                 if ((scalar @$members) > 1 && $pagestate{$trail}{trail}{circular}) {
351                         $member_to_trails{$members->[0]}{$trail}[0] = $members->[$#$members];
352                         $member_to_trails{$members->[$#$members]}{$trail}[1] = $members->[0];
353                 }
355                 $trail_to_members{$trail} = $members;
356         }
358         foreach my $member (keys %pagestate) {
359                 if (exists $pagestate{$member}{trail}{item} &&
360                         ! exists $member_to_trails{$member}) {
361                         $rebuild_trail_members{$member} = 1;
362                         delete $pagestate{$member}{trail}{item};
363                 }
364         }
366         foreach my $member (keys %member_to_trails) {
367                 if (! exists $pagestate{$member}{trail}{item}) {
368                         $rebuild_trail_members{$member} = 1;
369                 }
370                 else {
371                         if (trails_differ($pagestate{$member}{trail}{item},
372                                         $member_to_trails{$member})) {
373                                 $rebuild_trail_members{$member} = 1;
374                         }
375                 }
377                 $pagestate{$member}{trail}{item} = $member_to_trails{$member};
378         }
380         $done_prerender = 1;
383 sub build_affected {
384         my %affected;
386         # In principle we might not have done this yet, although in practice
387         # at least the trail itself has probably changed, and its template
388         # almost certainly contains TRAILS or TRAILLOOP, triggering our
389         # prerender as a side-effect.
390         prerender();
392         foreach my $member (keys %rebuild_trail_members) {
393                 $affected{$member} = sprintf(gettext("building %s, its previous or next page has changed"), $member);
394         }
396         return %affected;
399 sub title_of ($) {
400         my $page = shift;
401         if (defined ($pagestate{$page}{meta}{title})) {
402                 return $pagestate{$page}{meta}{title};
403         }
404         return pagetitle(IkiWiki::basename($page));
407 my $recursive = 0;
409 sub pagetemplate (@) {
410         my %params = @_;
411         my $page = $params{page};
412         my $template = $params{template};
414         if ($template->query(name => 'trails') && ! $recursive) {
415                 prerender();
417                 $recursive = 1;
418                 my $inner = template("trails.tmpl", blind_cache => 1);
419                 IkiWiki::run_hooks(pagetemplate => sub {
420                                 shift->(%params, template => $inner)
421                         });
422                 $template->param(trails => $inner->output);
423                 $recursive = 0;
424         }
426         if ($template->query(name => 'trailloop')) {
427                 prerender();
429                 my @trails;
431                 # sort backlinks by page name to have a consistent order
432                 foreach my $trail (sort keys %{$member_to_trails{$page}}) {
434                         my $members = $trail_to_members{$trail};
435                         my ($prev, $next) = @{$member_to_trails{$page}{$trail}};
436                         my ($prevurl, $nexturl, $prevtitle, $nexttitle);
438                         if (defined $prev) {
439                                 $prevurl = urlto($prev, $page);
440                                 $prevtitle = title_of($prev);
441                         }
443                         if (defined $next) {
444                                 $nexturl = urlto($next, $page);
445                                 $nexttitle = title_of($next);
446                         }
448                         push @trails, {
449                                 prevpage => $prev,
450                                 prevtitle => $prevtitle,
451                                 prevurl => $prevurl,
452                                 nextpage => $next,
453                                 nexttitle => $nexttitle,
454                                 nexturl => $nexturl,
455                                 trailpage => $trail,
456                                 trailtitle => title_of($trail),
457                                 trailurl => urlto($trail, $page),
458                         };
459                 }
461                 $template->param(trailloop => \@trails);
462         }
465 1;