diff options
author | Bjørn Erik Pedersen <[email protected]> | 2016-04-22 20:43:00 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2016-04-22 20:43:18 +0200 |
commit | 10af906371bedb643f67d89ec370a1236c6504fd (patch) | |
tree | d01fdfde292db991ba1e6df57c1d144e0d26a739 | |
parent | fbca53ac325324db104df5de86add3fa0baf3488 (diff) | |
download | hugo-10af906371bedb643f67d89ec370a1236c6504fd.tar.gz hugo-10af906371bedb643f67d89ec370a1236c6504fd.zip |
Add ByLastmod page sort
-rw-r--r-- | hugolib/pageSort.go | 18 | ||||
-rw-r--r-- | hugolib/pageSort_test.go | 11 |
2 files changed, 26 insertions, 3 deletions
diff --git a/hugolib/pageSort.go b/hugolib/pageSort.go index 7381cf9dd..40c18e4c6 100644 --- a/hugolib/pageSort.go +++ b/hugolib/pageSort.go @@ -158,6 +158,24 @@ func (p Pages) ByPublishDate() Pages { return pages } +// ByLastmod sorts the Pages by the last modification date and returns a copy. +// +// Adjacent invocactions on the same receiver will return a cached result. +// +// This may safely be executed in parallel. +func (p Pages) ByLastmod() Pages { + + key := "pageSort.ByLastmod" + + date := func(p1, p2 *Page) bool { + return p1.Lastmod.Unix() < p2.Lastmod.Unix() + } + + pages, _ := spc.get(key, p, pageBy(date).Sort) + + return pages +} + // ByLength sorts the Pages by length and returns a copy. // // Adjacent invocactions on the same receiver will return a cached result. diff --git a/hugolib/pageSort_test.go b/hugolib/pageSort_test.go index 0d1552f1c..f3fecb0be 100644 --- a/hugolib/pageSort_test.go +++ b/hugolib/pageSort_test.go @@ -15,12 +15,13 @@ package hugolib import ( "fmt" - "github.com/spf13/hugo/source" - "github.com/stretchr/testify/assert" "html/template" "path/filepath" "testing" "time" + + "github.com/spf13/hugo/source" + "github.com/stretchr/testify/assert" ) func TestDefaultSort(t *testing.T) { @@ -67,6 +68,7 @@ func TestSortByN(t *testing.T) { {(Pages).ByLinkTitle, func(p Pages) bool { return p[0].LinkTitle() == "abl" }}, {(Pages).ByDate, func(p Pages) bool { return p[0].Date == d3 }}, {(Pages).ByPublishDate, func(p Pages) bool { return p[0].PublishDate == d3 }}, + {(Pages).ByLastmod, func(p Pages) bool { return p[1].Lastmod == d2 }}, {(Pages).ByLength, func(p Pages) bool { return p[0].Content == "b_content" }}, } { setSortVals([3]time.Time{d1, d2, d3}, [3]string{"b", "ab", "cde"}, [3]int{3, 2, 1}, p) @@ -114,6 +116,7 @@ func BenchmarkSortByWeightAndReverse(b *testing.B) { func setSortVals(dates [3]time.Time, titles [3]string, weights [3]int, pages Pages) { for i := range dates { pages[i].Date = dates[i] + pages[i].Lastmod = dates[i] pages[i].Weight = weights[i] pages[i].Title = titles[i] // make sure we compare apples and ... apples ... @@ -121,7 +124,9 @@ func setSortVals(dates [3]time.Time, titles [3]string, weights [3]int, pages Pag pages[len(dates)-1-i].PublishDate = dates[i] pages[len(dates)-1-i].Content = template.HTML(titles[i] + "_content") } - + lastLastMod := pages[2].Lastmod + pages[2].Lastmod = pages[1].Lastmod + pages[1].Lastmod = lastLastMod } func createSortTestPages(num int) Pages { |