shithub: hugo

Download patch

ref: 10af906371bedb643f67d89ec370a1236c6504fd
parent: fbca53ac325324db104df5de86add3fa0baf3488
author: Bjørn Erik Pedersen <[email protected]>
date: Fri Apr 22 16:43:00 EDT 2016

Add ByLastmod page sort

--- a/hugolib/pageSort.go
+++ b/hugolib/pageSort.go
@@ -158,6 +158,24 @@
 	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.
--- a/hugolib/pageSort_test.go
+++ b/hugolib/pageSort_test.go
@@ -15,12 +15,13 @@
 
 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 @@
 		{(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 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 @@
 		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 {