]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blobdiff - IkiWiki/Plugin/trail.pm
Add a build_affected hook so trail doesn't have to inject
[git.ikiwiki.info.git] / IkiWiki / Plugin / trail.pm
index 098b98607d47c43baf61eebe17efcf9cf9a5e3c8..29830175e31c315800e236d8677729d8d41405f1 100644 (file)
@@ -1,6 +1,6 @@
 #!/usr/bin/perl
 # Copyright © 2008-2011 Joey Hess
-# Copyright © 2009-2011 Simon McVittie <http://smcv.pseudorandom.co.uk/>
+# Copyright © 2009-2012 Simon McVittie <http://smcv.pseudorandom.co.uk/>
 # Licensed under the GNU GPL, version 2, or any later version published by the
 # Free Software Foundation
 package IkiWiki::Plugin::trail;
@@ -12,11 +12,12 @@ use IkiWiki 3.00;
 sub import {
        hook(type => "getsetup", id => "trail", call => \&getsetup);
        hook(type => "needsbuild", id => "trail", call => \&needsbuild);
-       hook(type => "preprocess", id => "trail", call => \&preprocess_trail, scan => 1);
-       hook(type => "preprocess", id => "trailinline", call => \&preprocess_trailinline, scan => 1);
+       hook(type => "preprocess", id => "trailoptions", call => \&preprocess_trailoptions, scan => 1);
        hook(type => "preprocess", id => "trailitem", call => \&preprocess_trailitem, scan => 1);
+       hook(type => "preprocess", id => "trailitems", call => \&preprocess_trailitems, scan => 1);
        hook(type => "preprocess", id => "traillink", call => \&preprocess_traillink, scan => 1);
        hook(type => "pagetemplate", id => "trail", call => \&pagetemplate);
+       hook(type => "build_affected", id => "trail", call => \&build_affected);
 }
 
 =head1 Page state
@@ -103,23 +104,9 @@ sub needsbuild (@) {
 
 my $scanned = 0;
 
-sub preprocess_trail (@) {
+sub preprocess_trailoptions (@) {
        my %params = @_;
 
-       # avoid collecting everything in the preprocess stage if we already
-       # did in the scan stage
-       if (defined wantarray) {
-               return "" if $scanned;
-       }
-       else {
-               $scanned = 1;
-       }
-
-       # trail members from a pagespec ought to be in some sort of order,
-       # and path is a nice obvious default
-       $params{sortthese} = 'path' unless exists $params{sortthese};
-       $params{reversethese} = 'no' unless exists $params{reversethese};
-
        if (exists $params{circular}) {
                $pagestate{$params{page}}{trail}{circular} =
                        IkiWiki::yesno($params{circular});
@@ -133,63 +120,35 @@ sub preprocess_trail (@) {
                $pagestate{$params{page}}{trail}{reverse} = $params{reverse};
        }
 
-       if (exists $params{pages}) {
-               push @{$pagestate{$params{page}}{trail}{contents}},
-                       ["pagespec" => $params{pages}, $params{sortthese},
-                               IkiWiki::yesno($params{reversethese})];
-       }
-
-       if (exists $params{pagenames}) {
-               my @list = map { [link =>  $_] } split ' ', $params{pagenames};
-               push @{$pagestate{$params{page}}{trail}{contents}}, @list;
-       }
-
        return "";
 }
 
-sub preprocess_trailinline (@) {
-       my %params = @_;
+sub preprocess_trailitem (@) {
+       my $link = shift;
+       shift;
 
-       if (exists $params{sort}) {
-               $params{sortthese} = $params{sort};
-               delete $params{sort};
+       # avoid collecting everything in the preprocess stage if we already
+       # did in the scan stage
+       if (defined wantarray) {
+               return "" if $scanned;
        }
        else {
-               # sort in the same order as [[plugins/inline]]'s default
-               $params{sortthese} = 'age';
-       }
-
-       if (exists $params{reverse}) {
-               $params{reversethese} = $params{reverse};
-               delete $params{reverse};
+               $scanned = 1;
        }
 
-       if (exists $params{trailsort}) {
-               $params{sort} = $params{trailsort};
-       }
+       my %params = @_;
+       my $trail = $params{page};
 
-       if (exists $params{trailreverse}) {
-               $params{reverse} = $params{trailreverse};
-       }
+       $link = linkpage($link);
 
-       if (defined wantarray) {
-               scalar preprocess_trail(%params);
+       add_link($params{page}, $link, 'trail');
+       push @{$pagestate{$params{page}}{trail}{contents}}, [link => $link];
 
-               if (IkiWiki->can("preprocess_inline")) {
-                       return IkiWiki::preprocess_inline(@_);
-               }
-               else {
-                       error("trailinline directive requires the inline plugin");
-               }
-       }
-       else {
-               preprocess_trail(%params);
-       }
+       return "";
 }
 
-sub preprocess_trailitem (@) {
-       my $link = shift;
-       shift;
+sub preprocess_trailitems (@) {
+       my %params = @_;
 
        # avoid collecting everything in the preprocess stage if we already
        # did in the scan stage
@@ -200,13 +159,21 @@ sub preprocess_trailitem (@) {
                $scanned = 1;
        }
 
-       my %params = @_;
-       my $trail = $params{page};
+       # trail members from a pagespec ought to be in some sort of order,
+       # and path is a nice obvious default
+       $params{sort} = 'path' unless exists $params{sort};
+       $params{reverse} = 'no' unless exists $params{reverse};
 
-       $link = linkpage($link);
+       if (exists $params{pages}) {
+               push @{$pagestate{$params{page}}{trail}{contents}},
+                       ["pagespec" => $params{pages}, $params{sort},
+                               IkiWiki::yesno($params{reverse})];
+       }
 
-       add_link($params{page}, $link, 'trail');
-       push @{$pagestate{$params{page}}{trail}{contents}}, [link => $link];
+       if (exists $params{pagenames}) {
+               my @list = map { [link =>  $_] } split ' ', $params{pagenames};
+               push @{$pagestate{$params{page}}{trail}{contents}}, @list;
+       }
 
        return "";
 }
@@ -309,14 +276,9 @@ sub trails_differ {
 
 my $done_prerender = 0;
 
-my %origsubs;
-
 sub prerender {
        return if $done_prerender;
 
-       $origsubs{render_backlinks} = \&IkiWiki::render_backlinks;
-       inject(name => "IkiWiki::render_backlinks", call => \&render_backlinks);
-
        %trail_to_members = ();
        %member_to_trails = ();
 
@@ -402,18 +364,14 @@ sub prerender {
        $done_prerender = 1;
 }
 
-# This is called at about the right time that we can hijack it to render
-# extra pages.
-sub render_backlinks ($) {
-       my $blc = shift;
+sub build_affected {
+       my %affected;
 
        foreach my $member (keys %rebuild_trail_members) {
-               next unless exists $pagesources{$member};
-
-               IkiWiki::render($pagesources{$member}, sprintf(gettext("building %s, its previous or next page has changed"), $member));
+               $affected{$member} = sprintf(gettext("building %s, its previous or next page has changed"), $member);
        }
 
-       $origsubs{render_backlinks}($blc);
+       return %affected;
 }
 
 sub title_of ($) {