]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/trail.pm
(no commit message)
[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                         @$members = IkiWiki::sort_pages(
323                                 $pagestate{$trail}{trail}{sort},
324                                 $members);
325                 }
327                 if (IkiWiki::yesno $pagestate{$trail}{trail}{reverse}) {
328                         @$members = reverse @$members;
329                 }
331                 # uniquify
332                 my %seen;
333                 my @tmp;
334                 foreach my $member (@$members) {
335                         push @tmp, $member unless $seen{$member};
336                         $seen{$member} = 1;
337                 }
338                 $members = [@tmp];
340                 for (my $i = 0; $i <= $#$members; $i++) {
341                         my $member = $members->[$i];
342                         my $prev;
343                         $prev = $members->[$i - 1] if $i > 0;
344                         my $next = $members->[$i + 1];
346                         $member_to_trails{$member}{$trail} = [$prev, $next];
347                 }
349                 if ((scalar @$members) > 1 && $pagestate{$trail}{trail}{circular}) {
350                         $member_to_trails{$members->[0]}{$trail}[0] = $members->[$#$members];
351                         $member_to_trails{$members->[$#$members]}{$trail}[1] = $members->[0];
352                 }
354                 $trail_to_members{$trail} = $members;
355         }
357         foreach my $member (keys %pagestate) {
358                 if (exists $pagestate{$member}{trail}{item} &&
359                         ! exists $member_to_trails{$member}) {
360                         $rebuild_trail_members{$member} = 1;
361                         delete $pagestate{$member}{trail}{item};
362                 }
363         }
365         foreach my $member (keys %member_to_trails) {
366                 if (! exists $pagestate{$member}{trail}{item}) {
367                         $rebuild_trail_members{$member} = 1;
368                 }
369                 else {
370                         if (trails_differ($pagestate{$member}{trail}{item},
371                                         $member_to_trails{$member})) {
372                                 $rebuild_trail_members{$member} = 1;
373                         }
374                 }
376                 $pagestate{$member}{trail}{item} = $member_to_trails{$member};
377         }
379         $done_prerender = 1;
382 sub build_affected {
383         my %affected;
385         # In principle we might not have done this yet, although in practice
386         # at least the trail itself has probably changed, and its template
387         # almost certainly contains TRAILS or TRAILLOOP, triggering our
388         # prerender as a side-effect.
389         prerender();
391         foreach my $member (keys %rebuild_trail_members) {
392                 $affected{$member} = sprintf(gettext("building %s, its previous or next page has changed"), $member);
393         }
395         return %affected;
398 sub title_of ($) {
399         my $page = shift;
400         if (defined ($pagestate{$page}{meta}{title})) {
401                 return $pagestate{$page}{meta}{title};
402         }
403         return pagetitle(IkiWiki::basename($page));
406 my $recursive = 0;
408 sub pagetemplate (@) {
409         my %params = @_;
410         my $page = $params{page};
411         my $template = $params{template};
413         return unless length $page;
415         if ($template->query(name => 'trails') && ! $recursive) {
416                 prerender();
418                 $recursive = 1;
419                 my $inner = template("trails.tmpl", blind_cache => 1);
420                 IkiWiki::run_hooks(pagetemplate => sub {
421                                 shift->(%params, template => $inner)
422                         });
423                 $template->param(trails => $inner->output);
424                 $recursive = 0;
425         }
427         if ($template->query(name => 'trailloop')) {
428                 prerender();
430                 my @trails;
432                 # sort backlinks by page name to have a consistent order
433                 foreach my $trail (sort keys %{$member_to_trails{$page}}) {
435                         my $members = $trail_to_members{$trail};
436                         my ($prev, $next) = @{$member_to_trails{$page}{$trail}};
437                         my ($prevurl, $nexturl, $prevtitle, $nexttitle);
439                         if (defined $prev) {
440                                 $prevurl = urlto($prev, $page);
441                                 $prevtitle = title_of($prev);
442                         }
444                         if (defined $next) {
445                                 $nexturl = urlto($next, $page);
446                                 $nexttitle = title_of($next);
447                         }
449                         push @trails, {
450                                 prevpage => $prev,
451                                 prevtitle => $prevtitle,
452                                 prevurl => $prevurl,
453                                 nextpage => $next,
454                                 nexttitle => $nexttitle,
455                                 nexturl => $nexturl,
456                                 trailpage => $trail,
457                                 trailtitle => title_of($trail),
458                                 trailurl => urlto($trail, $page),
459                         };
460                 }
462                 $template->param(trailloop => \@trails);
463         }
466 1;