shithub: hugo

Download patch

ref: 4b4ab4755339a8c1d034be1f75b323d585dac1df
parent: 52e2fd27f81bb0f111addfb24fc63820e0ac1f04
author: Bjørn Erik Pedersen <[email protected]>
date: Sun Nov 6 10:38:52 EST 2016

hugolib: Fix page sorting when weight is zero

Fixes #2673

--- a/hugolib/hugo_sites_test.go
+++ b/hugolib/hugo_sites_test.go
@@ -1023,6 +1023,7 @@
 	sources := []source.ByteSource{
 		{Name: filepath.FromSlash("sect/doc1.en.md"), Content: []byte(`---
 title: doc1
+weight: 1
 slug: doc1-slug
 tags:
  - tag1
@@ -1037,6 +1038,7 @@
 `)},
 		{Name: filepath.FromSlash("sect/doc1.fr.md"), Content: []byte(`---
 title: doc1
+weight: 1
 plaques:
  - frtag1
  - frtag2
@@ -1052,6 +1054,7 @@
 `)},
 		{Name: filepath.FromSlash("sect/doc2.en.md"), Content: []byte(`---
 title: doc2
+weight: 2
 publishdate: "2000-01-02"
 ---
 # doc2
@@ -1060,6 +1063,7 @@
 `)},
 		{Name: filepath.FromSlash("sect/doc3.en.md"), Content: []byte(`---
 title: doc3
+weight: 3
 publishdate: "2000-01-03"
 tags:
  - tag2
@@ -1072,6 +1076,7 @@
 `)},
 		{Name: filepath.FromSlash("sect/doc4.md"), Content: []byte(`---
 title: doc4
+weight: 4
 plaques:
  - frtag1
 publishdate: "2000-01-05"
@@ -1083,6 +1088,7 @@
 `)},
 		{Name: filepath.FromSlash("other/doc5.fr.md"), Content: []byte(`---
 title: doc5
+weight: 5
 publishdate: "2000-01-06"
 ---
 # doc5
@@ -1099,6 +1105,7 @@
 `)},
 		{Name: filepath.FromSlash("stats/future.fr.md"), Content: []byte(`---
 title: future
+weight: 6
 publishdate: "2100-01-06"
 ---
 # Future
@@ -1105,6 +1112,7 @@
 `)},
 		{Name: filepath.FromSlash("stats/expired.en.md"), Content: []byte(`---
 title: expired
+weight: 7
 publishdate: "2000-01-06"
 expiryDate: "2001-01-06"
 ---
@@ -1112,6 +1120,7 @@
 `)},
 		{Name: filepath.FromSlash("stats/future.en.md"), Content: []byte(`---
 title: future
+weight: 6
 publishdate: "2100-01-06"
 ---
 # Future
@@ -1125,6 +1134,7 @@
 `)},
 		{Name: filepath.FromSlash("stats/tax.nn.md"), Content: []byte(`---
 title: Tax NN
+weight: 8
 publishdate: "2000-01-06"
 weight: 1001
 lag:
@@ -1134,6 +1144,7 @@
 `)},
 		{Name: filepath.FromSlash("stats/tax.nb.md"), Content: []byte(`---
 title: Tax NB
+weight: 8
 publishdate: "2000-01-06"
 weight: 1002
 lag:
--- a/hugolib/menu_test.go
+++ b/hugolib/menu_test.go
@@ -87,9 +87,9 @@
 
 var menuPage1 = []byte(`+++
 title = "One"
+weight = 1
 [menu]
 	[menu.p_one]
-weight = 1
 +++
 Front Matter with Menu Pages`)
 
--- a/hugolib/pageSort.go
+++ b/hugolib/pageSort.go
@@ -53,6 +53,15 @@
 		}
 		return p1.Date.Unix() > p2.Date.Unix()
 	}
+
+	if p2.Weight == 0 {
+		return true
+	}
+
+	if p1.Weight == 0 {
+		return false
+	}
+
 	return p1.Weight < p2.Weight
 }
 
@@ -66,6 +75,15 @@
 		}
 		return p1.Date.Unix() > p2.Date.Unix()
 	}
+
+	if p2.language.Weight == 0 {
+		return true
+	}
+
+	if p1.language.Weight == 0 {
+		return false
+	}
+
 	return p1.language.Weight < p2.language.Weight
 }
 
--- a/hugolib/pageSort_test.go
+++ b/hugolib/pageSort_test.go
@@ -30,22 +30,29 @@
 	d1 := time.Now()
 	d2 := d1.Add(-1 * time.Hour)
 	d3 := d1.Add(-2 * time.Hour)
+	d4 := d1.Add(-3 * time.Hour)
 
-	p := createSortTestPages(3)
+	p := createSortTestPages(4)
 
 	// first by weight
-	setSortVals([3]time.Time{d1, d2, d3}, [3]string{"b", "a", "c"}, [3]int{3, 2, 1}, p)
+	setSortVals([4]time.Time{d1, d2, d3, d4}, [4]string{"b", "a", "c", "d"}, [4]int{4, 3, 2, 1}, p)
 	p.Sort()
 
 	assert.Equal(t, 1, p[0].Weight)
 
+	// Consider zero weight, issue #2673
+	setSortVals([4]time.Time{d1, d2, d3, d4}, [4]string{"b", "a", "d", "c"}, [4]int{0, 0, 0, 1}, p)
+	p.Sort()
+
+	assert.Equal(t, 1, p[0].Weight)
+
 	// next by date
-	setSortVals([3]time.Time{d3, d1, d2}, [3]string{"a", "b", "c"}, [3]int{1, 1, 1}, p)
+	setSortVals([4]time.Time{d3, d4, d1, d2}, [4]string{"a", "b", "c", "d"}, [4]int{1, 1, 1, 1}, p)
 	p.Sort()
 	assert.Equal(t, d1, p[0].Date)
 
 	// finally by link title
-	setSortVals([3]time.Time{d3, d3, d3}, [3]string{"b", "c", "a"}, [3]int{1, 1, 1}, p)
+	setSortVals([4]time.Time{d3, d3, d3, d3}, [4]string{"b", "c", "a", "d"}, [4]int{1, 1, 1, 1}, p)
 	p.Sort()
 	assert.Equal(t, "al", p[0].LinkTitle())
 	assert.Equal(t, "bl", p[1].LinkTitle())
@@ -57,8 +64,9 @@
 	d1 := time.Now()
 	d2 := d1.Add(-2 * time.Hour)
 	d3 := d1.Add(-10 * time.Hour)
+	d4 := d1.Add(-20 * time.Hour)
 
-	p := createSortTestPages(3)
+	p := createSortTestPages(4)
 
 	for i, this := range []struct {
 		sortFunc   func(p Pages) Pages
@@ -67,13 +75,13 @@
 		{(Pages).ByWeight, func(p Pages) bool { return p[0].Weight == 1 }},
 		{(Pages).ByTitle, func(p Pages) bool { return p[0].Title == "ab" }},
 		{(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).ByExpiryDate, func(p Pages) bool { return p[0].ExpiryDate == d3 }},
-		{(Pages).ByLastmod, func(p Pages) bool { return p[1].Lastmod == d2 }},
+		{(Pages).ByDate, func(p Pages) bool { return p[0].Date == d4 }},
+		{(Pages).ByPublishDate, func(p Pages) bool { return p[0].PublishDate == d4 }},
+		{(Pages).ByExpiryDate, func(p Pages) bool { return p[0].ExpiryDate == d4 }},
+		{(Pages).ByLastmod, func(p Pages) bool { return p[1].Lastmod == d3 }},
 		{(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)
+		setSortVals([4]time.Time{d1, d2, d3, d4}, [4]string{"b", "ab", "cde", "fg"}, [4]int{0, 3, 2, 1}, p)
 
 		sorted := this.sortFunc(p)
 		if !this.assertFunc(sorted) {
@@ -115,7 +123,7 @@
 	}
 }
 
-func setSortVals(dates [3]time.Time, titles [3]string, weights [3]int, pages Pages) {
+func setSortVals(dates [4]time.Time, titles [4]string, weights [4]int, pages Pages) {
 	for i := range dates {
 		pages[i].Date = dates[i]
 		pages[i].Lastmod = dates[i]
@@ -149,6 +157,7 @@
 			Source: Source{File: *source.NewFile(filepath.FromSlash(fmt.Sprintf("/x/y/p%d.md", i)))},
 		}
 		w := 5
+
 		if i%2 == 0 {
 			w = 10
 		}