X-Git-Url: http://git.vanrenterghem.biz/git.ikiwiki.info.git/blobdiff_plain/24168b993e65994d2996d02bfaa36fb80bb65ca7..2bb5085785d05d5f3fa81b7adcfb8678c7c67ae8:/IkiWiki/Plugin/trail.pm?ds=sidebyside diff --git a/IkiWiki/Plugin/trail.pm b/IkiWiki/Plugin/trail.pm index 29830175e..d5fb2b5d6 100644 --- a/IkiWiki/Plugin/trail.pm +++ b/IkiWiki/Plugin/trail.pm @@ -20,60 +20,39 @@ sub import { hook(type => "build_affected", id => "trail", call => \&build_affected); } -=head1 Page state - -If a page C<$T> is a trail, then it can have - -=over - -=item * C<$pagestate{$T}{trail}{contents}> - -Reference to an array of lists each containing either: - -=over - -=item * C<[link, "link"]> - -A link specification, pointing to the same page that C<[[link]]> would select - -=item * C<[pagespec, "posts/*", "age", 0]> - -A match by pagespec; the third array element is the sort order and the fourth -is whether to reverse sorting - -=back - -=item * C<$pagestate{$T}{trail}{sort}> - -A [[ikiwiki/pagespec/sorting]] order; if absent or undef, the trail is in -the order given by the links that form it - -=item * C<$pagestate{$T}{trail}{circular}> - -True if this trail is circular (i.e. going "next" from the last item is -allowed, and takes you back to the first) - -=item * C<$pagestate{$T}{trail}{reverse}> - -True if C is to be reversed. - -=back - -If a page C<$M> is a member of a trail C<$T>, then it has - -=over - -=item * C<$pagestate{$M}{trail}{item}{$T}[0]> - -The page before this one in C<$T> at the last rebuild, or undef. - -=item * C<$pagestate{$M}{trail}{item}{$T}[1]> - -The page after this one in C<$T> at the last refresh, or undef. - -=back - -=cut +# Page state +# +# If a page $T is a trail, then it can have +# +# * $pagestate{$T}{trail}{contents} +# Reference to an array of lists each containing either: +# - [pagenames => "page1", "page2"] +# Those literal pages +# - [link => "link"] +# A link specification, pointing to the same page that [[link]] +# would select +# - [pagespec => "posts/*", "age", 0] +# A match by pagespec; the third array element is the sort order +# and the fourth is whether to reverse sorting +# +# * $pagestate{$T}{trail}{sort} +# A sorting order; if absent or undef, the trail is in the order given +# by the links that form it +# +# * $pagestate{$T}{trail}{circular} +# True if this trail is circular (i.e. going "next" from the last item is +# allowed, and takes you back to the first) +# +# * $pagestate{$T}{trail}{reverse} +# True if C is to be reversed. +# +# If a page $M is a member of a trail $T, then it has +# +# * $pagestate{$M}{trail}{item}{$T}[0] +# The page before this one in C<$T> at the last rebuild, or undef. +# +# * $pagestate{$M}{trail}{item}{$T}[1] +# The page after this one in C<$T> at the last refresh, or undef. sub getsetup () { return @@ -83,12 +62,20 @@ sub getsetup () { }, } +# Cache of pages' old titles, so we can tell whether they changed +my %old_trail_titles; + sub needsbuild (@) { my $needsbuild=shift; + foreach my $page (keys %pagestate) { if (exists $pagestate{$page}{trail}) { if (exists $pagesources{$page} && grep { $_ eq $pagesources{$page} } @$needsbuild) { + # Remember its title, so we can know whether + # it changed. + $old_trail_titles{$page} = title_of($page); + # Remove state, it will be re-added # if the preprocessor directive is still # there during the rebuild. {item} is the @@ -99,6 +86,7 @@ sub needsbuild (@) { } } } + return $needsbuild; } @@ -171,8 +159,8 @@ sub preprocess_trailitems (@) { } if (exists $params{pagenames}) { - my @list = map { [link => $_] } split ' ', $params{pagenames}; - push @{$pagestate{$params{page}}{trail}{contents}}, @list; + push @{$pagestate{$params{page}}{trail}{contents}}, + [pagenames => (split ' ', $params{pagenames})]; } return ""; @@ -251,6 +239,12 @@ sub trails_differ { if (! exists $new->{$trail}) { return 1; } + + if (exists $old_trail_titles{$trail} && + title_of($trail) ne $old_trail_titles{$trail}) { + return 1; + } + my ($old_p, $old_n) = @{$old->{$trail}}; my ($new_p, $new_n) = @{$new->{$trail}}; $old_p = "" unless defined $old_p; @@ -260,9 +254,20 @@ sub trails_differ { if ($old_p ne $new_p) { return 1; } + + if (exists $old_trail_titles{$old_p} && + title_of($old_p) ne $old_trail_titles{$old_p}) { + return 1; + } + if ($old_n ne $new_n) { return 1; } + + if (exists $old_trail_titles{$old_n} && + title_of($old_n) ne $old_trail_titles{$old_n}) { + return 1; + } } foreach my $trail (keys %$new) { @@ -294,6 +299,19 @@ sub prerender { $c->[1], sort => $c->[2], reverse => $c->[3]); } + elsif ($c->[0] eq 'pagenames') { + my @pagenames = @$c; + shift @pagenames; + foreach my $page (@pagenames) { + if (exists $pagesources{$page}) { + push @$members, $page; + } + else { + # rebuild trail if it turns up + add_depends($trail, $page, deptype("presence")); + } + } + } elsif ($c->[0] eq 'link') { my $best = bestlink($trail, $c->[1]); push @$members, $best if length $best; @@ -326,8 +344,6 @@ sub prerender { $prev = $members->[$i - 1] if $i > 0; my $next = $members->[$i + 1]; - add_depends($member, $trail); - $member_to_trails{$member}{$trail} = [$prev, $next]; } @@ -343,7 +359,7 @@ sub prerender { if (exists $pagestate{$member}{trail}{item} && ! exists $member_to_trails{$member}) { $rebuild_trail_members{$member} = 1; - delete $pagestate{$member}{trailitem}; + delete $pagestate{$member}{trail}{item}; } } @@ -367,6 +383,12 @@ sub prerender { sub build_affected { my %affected; + # In principle we might not have done this yet, although in practice + # at least the trail itself has probably changed, and its template + # almost certainly contains TRAILS or TRAILLOOP, triggering our + # prerender as a side-effect. + prerender(); + foreach my $member (keys %rebuild_trail_members) { $affected{$member} = sprintf(gettext("building %s, its previous or next page has changed"), $member); } @@ -389,6 +411,8 @@ sub pagetemplate (@) { my $page = $params{page}; my $template = $params{template}; + return unless length $page; + if ($template->query(name => 'trails') && ! $recursive) { prerender(); @@ -414,13 +438,11 @@ sub pagetemplate (@) { my ($prevurl, $nexturl, $prevtitle, $nexttitle); if (defined $prev) { - add_depends($params{destpage}, $prev); $prevurl = urlto($prev, $page); $prevtitle = title_of($prev); } if (defined $next) { - add_depends($params{destpage}, $next); $nexturl = urlto($next, $page); $nexttitle = title_of($next); }