Split trail directive into trailitems, trailoptions
authorSimon McVittie <smcv@debian.org>
Sat, 12 Nov 2011 21:37:28 +0000 (21:37 +0000)
committerSimon McVittie <smcv@debian.org>
Sat, 12 Nov 2011 21:37:28 +0000 (21:37 +0000)
IkiWiki/Plugin/trail.pm
t/trail.t

index 098b98607d47c43baf61eebe17efcf9cf9a5e3c8..5ee152155187d718d314bb70776ff2ecb8503c6b 100644 (file)
@@ -12,9 +12,10 @@ 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 => "trailoptions", call => \&preprocess_trailoptions, scan => 1);
        hook(type => "preprocess", id => "trailinline", call => \&preprocess_trailinline, 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);
 }
@@ -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,47 +120,19 @@ 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 = @_;
 
-       if (exists $params{sort}) {
-               $params{sortthese} = $params{sort};
-               delete $params{sort};
-       }
-       else {
+       if (! exists $params{sort}) {
                # 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};
+               $params{sort} = 'age';
        }
 
        if (defined wantarray) {
-               scalar preprocess_trail(%params);
+               scalar preprocess_trailitems(%params);
 
                if (IkiWiki->can("preprocess_inline")) {
                        return IkiWiki::preprocess_inline(@_);
@@ -183,7 +142,7 @@ sub preprocess_trailinline (@) {
                }
        }
        else {
-               preprocess_trail(%params);
+               preprocess_trailitems(%params);
        }
 }
 
@@ -211,6 +170,37 @@ sub preprocess_trailitem (@) {
        return "";
 }
 
+sub preprocess_trailitems (@) {
+       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{sort} = 'path' unless exists $params{sort};
+       $params{reverse} = 'no' unless exists $params{reverse};
+
+       if (exists $params{pages}) {
+               push @{$pagestate{$params{page}}{trail}{contents}},
+                       ["pagespec" => $params{pages}, $params{sort},
+                               IkiWiki::yesno($params{reverse})];
+       }
+
+       if (exists $params{pagenames}) {
+               my @list = map { [link =>  $_] } split ' ', $params{pagenames};
+               push @{$pagestate{$params{page}}{trail}{contents}}, @list;
+       }
+
+       return "";
+}
+
 sub preprocess_traillink (@) {
        my $link = shift;
        shift;
index 28aa1c09de9150ab431374449b16c7dfaa372b11..0cf50ddc1cdb8aead5d655da59a4e70b077c9894 100755 (executable)
--- a/t/trail.t
+++ b/t/trail.t
@@ -35,16 +35,16 @@ writefile("mushroom.mdwn", "t/tmp/in", "content of mushroom");
 writefile("snake.mdwn", "t/tmp/in", "content of snake");
 writefile("ratty.mdwn", "t/tmp/in", "content of ratty");
 writefile("mr_toad.mdwn", "t/tmp/in", "content of mr toad");
-writefile("add.mdwn", "t/tmp/in", '[[!trail pagenames="add/a add/b add/c add/d add/e"]]');
+writefile("add.mdwn", "t/tmp/in", '[[!trailitems pagenames="add/a add/b add/c add/d add/e"]]');
 writefile("add/b.mdwn", "t/tmp/in", "b");
 writefile("add/d.mdwn", "t/tmp/in", "d");
-writefile("del.mdwn", "t/tmp/in", '[[!trail pages="del/*" sort=title]]');
+writefile("del.mdwn", "t/tmp/in", '[[!trailitems pages="del/*" sort=title]]');
 writefile("del/a.mdwn", "t/tmp/in", "a");
 writefile("del/b.mdwn", "t/tmp/in", "b");
 writefile("del/c.mdwn", "t/tmp/in", "c");
 writefile("del/d.mdwn", "t/tmp/in", "d");
 writefile("del/e.mdwn", "t/tmp/in", "e");
-writefile("self_referential.mdwn", "t/tmp/in", '[[!trail pagenames="self_referential" circular=yes]]');
+writefile("self_referential.mdwn", "t/tmp/in", '[[!trailitems pagenames="self_referential" circular=yes]]');
 writefile("sorting/linked.mdwn", "t/tmp/in", "linked");
 writefile("sorting/a/b.mdwn", "t/tmp/in", "a/b");
 writefile("sorting/a/c.mdwn", "t/tmp/in", "a/c");
@@ -64,8 +64,8 @@ writefile("sorting/linked2.mdwn", "t/tmp/in", "linked2");
 # for trail. We change it later.
 writefile("sorting.mdwn", "t/tmp/in",
        '[[!traillink linked]] ' .
-       '[[!trail pages="sorting/z/a or sorting/a/b or sorting/a/c"]] ' .
-       '[[!trail pagenames="beginning middle end"]] ' .
+       '[[!trailitems pages="sorting/z/a or sorting/a/b or sorting/a/c"]] ' .
+       '[[!trailitems pagenames="beginning middle end"]] ' .
        '[[!trailinline pages="sorting/old or sorting/ancient or sorting/new"]] ' .
        '[[!traillink linked2]]');
 
@@ -83,7 +83,8 @@ EOF
 );
 
 writefile("wind_in_the_willows.mdwn", "t/tmp/in", <<EOF
-[[!trail circular=yes sort=title pages="ratty or badger or mr_toad"]]
+[[!trailoptions circular=yes sort=title]]
+[[!trailitems pages="ratty or badger or mr_toad"]]
 [[!trailitem moley]]
 EOF
 );
@@ -183,7 +184,7 @@ ok(unlink("t/tmp/in/del/e.mdwn"));
 
 writefile("sorting.mdwn", "t/tmp/in",
        readfile("t/tmp/in/sorting.mdwn") .
-       '[[!trailinline pages="doesnt_exist" trailsort="title" trailreverse="yes"]]'); 
+       '[[!trailoptions sort="title" reverse="yes"]]'); 
 
 ok(! system("$command -refresh"));