trail: improve and test sorting
authorSimon McVittie <smcv@debian.org>
Sat, 12 Nov 2011 16:57:54 +0000 (16:57 +0000)
committerSimon McVittie <smcv@debian.org>
Sat, 12 Nov 2011 16:57:54 +0000 (16:57 +0000)
IkiWiki/Plugin/trail.pm
t/trail.t

index e6e55bbdbe104364a407466bd16daf0d099246ba..87d99dd3dbafcd47bdc3e599d79697552ffc9874 100644 (file)
@@ -123,6 +123,11 @@ sub preprocess_trail (@) {
                $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});
@@ -137,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;
        }
 
@@ -179,6 +186,28 @@ to the trail.
 sub preprocess_trailinline (@) {
        my %params = @_;
 
+       if (exists $params{sort}) {
+               $params{sortthese} = $params{sort};
+               delete $params{sort};
+       }
+       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};
+       }
+
+       if (exists $params{trailsort}) {
+               $params{sort} = $params{trailsort};
+       }
+
+       if (exists $params{trailreverse}) {
+               $params{reverse} = $params{trailreverse};
+       }
+
        if (defined wantarray) {
                scalar preprocess_trail(%params);
 
@@ -225,7 +254,7 @@ sub preprocess_trailitem (@) {
        $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 "";
 }
@@ -363,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;
                        }
                }
index ce7d92048fcc3d43cfb1e2cbc7dafce972d04442..28aa1c09de9150ab431374449b16c7dfaa372b11 100755 (executable)
--- a/t/trail.t
+++ b/t/trail.t
@@ -45,6 +45,29 @@ 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("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");
+writefile("sorting/z/a.mdwn", "t/tmp/in", "z/a");
+writefile("sorting/beginning.mdwn", "t/tmp/in", "beginning");
+writefile("sorting/middle.mdwn", "t/tmp/in", "middle");
+writefile("sorting/end.mdwn", "t/tmp/in", "end");
+writefile("sorting/new.mdwn", "t/tmp/in", "new");
+writefile("sorting/old.mdwn", "t/tmp/in", "old");
+writefile("sorting/ancient.mdwn", "t/tmp/in", "ancient");
+# These three need to be in the appropriate age order
+ok(utime(333333333, 333333333, "t/tmp/in/sorting/new.mdwn"));
+ok(utime(222222222, 222222222, "t/tmp/in/sorting/old.mdwn"));
+ok(utime(111111111, 111111111, "t/tmp/in/sorting/ancient.mdwn"));
+writefile("sorting/linked2.mdwn", "t/tmp/in", "linked2");
+# This initially uses the default sort order: age for trailinline, and path
+# 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"]] ' .
+       '[[!trailinline pages="sorting/old or sorting/ancient or sorting/new"]] ' .
+       '[[!traillink linked2]]');
 
 writefile("meme.mdwn", "t/tmp/in", <<EOF
 [[!trail]]
@@ -126,6 +149,29 @@ ok($blob =~ /^trail=del n=del\/e p=del\/c$/m);
 $blob = readfile("t/tmp/out/del/e.html");
 ok($blob =~ /^trail=del n= p=del\/d$/m);
 
+$blob = readfile("t/tmp/out/sorting/linked.html");
+ok($blob =~ m{^trail=sorting n=sorting/a/b p=$}m);
+$blob = readfile("t/tmp/out/sorting/a/b.html");
+ok($blob =~ m{^trail=sorting n=sorting/a/c p=sorting/linked$}m);
+$blob = readfile("t/tmp/out/sorting/a/c.html");
+ok($blob =~ m{^trail=sorting n=sorting/z/a p=sorting/a/b$}m);
+$blob = readfile("t/tmp/out/sorting/z/a.html");
+ok($blob =~ m{^trail=sorting n=sorting/beginning p=sorting/a/c$}m);
+$blob = readfile("t/tmp/out/sorting/beginning.html");
+ok($blob =~ m{^trail=sorting n=sorting/middle p=sorting/z/a$}m);
+$blob = readfile("t/tmp/out/sorting/middle.html");
+ok($blob =~ m{^trail=sorting n=sorting/end p=sorting/beginning$}m);
+$blob = readfile("t/tmp/out/sorting/end.html");
+ok($blob =~ m{^trail=sorting n=sorting/new p=sorting/middle$}m);
+$blob = readfile("t/tmp/out/sorting/new.html");
+ok($blob =~ m{^trail=sorting n=sorting/old p=sorting/end$}m);
+$blob = readfile("t/tmp/out/sorting/old.html");
+ok($blob =~ m{^trail=sorting n=sorting/ancient p=sorting/new$}m);
+$blob = readfile("t/tmp/out/sorting/ancient.html");
+ok($blob =~ m{^trail=sorting n=sorting/linked2 p=sorting/old$}m);
+$blob = readfile("t/tmp/out/sorting/linked2.html");
+ok($blob =~ m{^trail=sorting n= p=sorting/ancient$}m);
+
 # Make some changes and refresh
 
 writefile("add/a.mdwn", "t/tmp/in", "a");
@@ -135,6 +181,10 @@ ok(unlink("t/tmp/in/del/a.mdwn"));
 ok(unlink("t/tmp/in/del/c.mdwn"));
 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"]]'); 
+
 ok(! system("$command -refresh"));
 
 $blob = readfile("t/tmp/out/add/a.html");
@@ -156,4 +206,27 @@ ok(! -f "t/tmp/out/del/a.html");
 ok(! -f "t/tmp/out/del/c.html");
 ok(! -f "t/tmp/out/del/e.html");
 
-#ok(! system("rm -rf t/tmp"));
+$blob = readfile("t/tmp/out/sorting/old.html");
+ok($blob =~ m{^trail=sorting n=sorting/new p=$}m);
+$blob = readfile("t/tmp/out/sorting/new.html");
+ok($blob =~ m{^trail=sorting n=sorting/middle p=sorting/old$}m);
+$blob = readfile("t/tmp/out/sorting/middle.html");
+ok($blob =~ m{^trail=sorting n=sorting/linked2 p=sorting/new$}m);
+$blob = readfile("t/tmp/out/sorting/linked2.html");
+ok($blob =~ m{^trail=sorting n=sorting/linked p=sorting/middle$}m);
+$blob = readfile("t/tmp/out/sorting/linked.html");
+ok($blob =~ m{^trail=sorting n=sorting/end p=sorting/linked2$}m);
+$blob = readfile("t/tmp/out/sorting/end.html");
+ok($blob =~ m{^trail=sorting n=sorting/a/c p=sorting/linked$}m);
+$blob = readfile("t/tmp/out/sorting/a/c.html");
+ok($blob =~ m{^trail=sorting n=sorting/beginning p=sorting/end$}m);
+$blob = readfile("t/tmp/out/sorting/beginning.html");
+ok($blob =~ m{^trail=sorting n=sorting/a/b p=sorting/a/c$}m);
+$blob = readfile("t/tmp/out/sorting/a/b.html");
+ok($blob =~ m{^trail=sorting n=sorting/ancient p=sorting/beginning$}m);
+$blob = readfile("t/tmp/out/sorting/ancient.html");
+ok($blob =~ m{^trail=sorting n=sorting/z/a p=sorting/a/b$}m);
+$blob = readfile("t/tmp/out/sorting/z/a.html");
+ok($blob =~ m{^trail=sorting n= p=sorting/ancient$}m);
+
+ok(! system("rm -rf t/tmp"));