]> sipb.mit.edu Git - ikiwiki.git/blobdiff - IkiWiki/Plugin/trail.pm
trail: improve and test sorting
[ikiwiki.git] / IkiWiki / Plugin / trail.pm
index e9b4d9cd4fc589ff2b35f2256f6d7d76bd697770..87d99dd3dbafcd47bdc3e599d79697552ffc9874 100644 (file)
@@ -88,6 +88,8 @@ sub needsbuild (@) {
        return $needsbuild;
 }
 
+my $scanned = 0;
+
 =for wiki
 
 The `trail` directive is supplied by the [[plugins/contrib/trail]]
@@ -112,6 +114,20 @@ The available options are:
 sub preprocess_trail (@) {
        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});
@@ -126,11 +142,13 @@ sub preprocess_trail (@) {
        }
 
        if (exists $params{pages}) {
-               push @{$pagestate{$params{page}}{trail}{contents}}, "pagespec $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};
+               my @list = map { [link =>  $_] } split ' ', $params{pagenames};
                push @{$pagestate{$params{page}}{trail}{contents}}, @list;
        }
 
@@ -166,14 +184,42 @@ to the trail.
 =cut
 
 sub preprocess_trailinline (@) {
-       preprocess_trail(@_);
-       return unless defined wantarray;
+       my %params = @_;
 
-       if (IkiWiki->can("preprocess_inline")) {
-               return IkiWiki::preprocess_inline(@_);
+       if (exists $params{sort}) {
+               $params{sortthese} = $params{sort};
+               delete $params{sort};
        }
        else {
-               error("trailinline directive requires the inline plugin");
+               # sort in the same order as [[plugins/inline]]'s default
+               $params{sortthese} = 'age';
+       }
+
+       if (exists $params{reverse}) {
+               $params{reversethese} = $params{reverse};
+               delete $params{reverse};
+       }
+
+       if (exists $params{trailsort}) {
+               $params{sort} = $params{trailsort};
+       }
+
+       if (exists $params{trailreverse}) {
+               $params{reverse} = $params{trailreverse};
+       }
+
+       if (defined wantarray) {
+               scalar preprocess_trail(%params);
+
+               if (IkiWiki->can("preprocess_inline")) {
+                       return IkiWiki::preprocess_inline(@_);
+               }
+               else {
+                       error("trailinline directive requires the inline plugin");
+               }
+       }
+       else {
+               preprocess_trail(%params);
        }
 }
 
@@ -193,13 +239,22 @@ sub preprocess_trailitem (@) {
        my $link = shift;
        shift;
 
+       # avoid collecting everything in the preprocess stage if we already
+       # did in the scan stage
+       if (defined wantarray) {
+               return "" if $scanned;
+       }
+       else {
+               $scanned = 1;
+       }
+
        my %params = @_;
        my $trail = $params{page};
 
        $link = linkpage($link);
 
        add_link($params{page}, $link, 'trail');
-       push @{$pagestate{$params{page}}{trail}{contents}}, "link $link";
+       push @{$pagestate{$params{page}}{trail}{contents}}, [link => $link];
 
        return "";
 }
@@ -242,7 +297,18 @@ sub preprocess_traillink (@) {
        $link = linkpage($2);
 
        add_link($params{page}, $link, 'trail');
-       push @{$pagestate{$params{page}}{trail}{contents}}, "link $link";
+
+       # avoid collecting everything in the preprocess stage if we already
+       # did in the scan stage
+       my $already;
+       if (defined wantarray) {
+               $already = $scanned;
+       }
+       else {
+               $scanned = 1;
+       }
+
+       push @{$pagestate{$params{page}}{trail}{contents}}, [link => $link] unless $already;
 
        if (defined $linktext) {
                $linktext = pagetitle($linktext);
@@ -326,13 +392,14 @@ sub prerender {
                my $members = [];
                my @contents = @{$pagestate{$trail}{trail}{contents}};
 
-
                foreach my $c (@contents) {
-                       if ($c =~ m/^pagespec (.*)$/) {
-                               push @$members, pagespec_match_list($trail, $1);
+                       if ($c->[0] eq 'pagespec') {
+                               push @$members, pagespec_match_list($trail,
+                                       $c->[1], sort => $c->[2],
+                                       reverse => $c->[3]);
                        }
-                       elsif ($c =~ m/^link (.*)$/) {
-                               my $best = bestlink($trail, $1);
+                       elsif ($c->[0] eq 'link') {
+                               my $best = bestlink($trail, $c->[1]);
                                push @$members, $best if length $best;
                        }
                }