From e0bfd0cafda6a44a826cdfe07b99299cc96dfdf3 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Sat, 12 Nov 2011 16:57:54 +0000 Subject: [PATCH] trail: improve and test sorting --- IkiWiki/Plugin/trail.pm | 46 ++++++++++++++++++++----- t/trail.t | 75 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 112 insertions(+), 9 deletions(-) diff --git a/IkiWiki/Plugin/trail.pm b/IkiWiki/Plugin/trail.pm index e6e55bbdb..87d99dd3d 100644 --- a/IkiWiki/Plugin/trail.pm +++ b/IkiWiki/Plugin/trail.pm @@ -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; } } diff --git a/t/trail.t b/t/trail.t index ce7d92048..28aa1c09d 100755 --- 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", <