From 4b4ab4755339a8c1d034be1f75b323d585dac1df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Sun, 6 Nov 2016 15:38:52 +0100 Subject: [PATCH] hugolib: Fix page sorting when weight is zero Fixes #2673 --- hugolib/hugo_sites_test.go | 11 +++++++++++ hugolib/menu_test.go | 2 +- hugolib/pageSort.go | 18 ++++++++++++++++++ hugolib/pageSort_test.go | 31 ++++++++++++++++++++----------- 4 files changed, 50 insertions(+), 12 deletions(-) diff --git a/hugolib/hugo_sites_test.go b/hugolib/hugo_sites_test.go index e14a04cb6..5a44fb5f9 100644 --- a/hugolib/hugo_sites_test.go +++ b/hugolib/hugo_sites_test.go @@ -1023,6 +1023,7 @@ func createMultiTestSitesForConfig(t *testing.T, siteConfig testSiteConfig, conf 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 @@ NOTE: slug should be used as URL `)}, {Name: filepath.FromSlash("sect/doc1.fr.md"), Content: []byte(`--- title: doc1 +weight: 1 plaques: - frtag1 - frtag2 @@ -1052,6 +1054,7 @@ NOTE: date is after "doc3" `)}, {Name: filepath.FromSlash("sect/doc2.en.md"), Content: []byte(`--- title: doc2 +weight: 2 publishdate: "2000-01-02" --- # doc2 @@ -1060,6 +1063,7 @@ NOTE: without slug, "doc2" should be used, without ".en" as URL `)}, {Name: filepath.FromSlash("sect/doc3.en.md"), Content: []byte(`--- title: doc3 +weight: 3 publishdate: "2000-01-03" tags: - tag2 @@ -1072,6 +1076,7 @@ NOTE: third 'en' doc, should trigger pagination on home page. `)}, {Name: filepath.FromSlash("sect/doc4.md"), Content: []byte(`--- title: doc4 +weight: 4 plaques: - frtag1 publishdate: "2000-01-05" @@ -1083,6 +1088,7 @@ NOTE: doesn't have any corresponding translation in 'en' `)}, {Name: filepath.FromSlash("other/doc5.fr.md"), Content: []byte(`--- title: doc5 +weight: 5 publishdate: "2000-01-06" --- # doc5 @@ -1099,12 +1105,14 @@ expiryDate: "2001-01-06" `)}, {Name: filepath.FromSlash("stats/future.fr.md"), Content: []byte(`--- title: future +weight: 6 publishdate: "2100-01-06" --- # Future `)}, {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 @@ expiryDate: "2001-01-06" `)}, {Name: filepath.FromSlash("stats/future.en.md"), Content: []byte(`--- title: future +weight: 6 publishdate: "2100-01-06" --- # Future @@ -1125,6 +1134,7 @@ draft: true `)}, {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 @@ lag: `)}, {Name: filepath.FromSlash("stats/tax.nb.md"), Content: []byte(`--- title: Tax NB +weight: 8 publishdate: "2000-01-06" weight: 1002 lag: diff --git a/hugolib/menu_test.go b/hugolib/menu_test.go index 38bcd1c05..53ffc720c 100644 --- a/hugolib/menu_test.go +++ b/hugolib/menu_test.go @@ -87,9 +87,9 @@ const ( var menuPage1 = []byte(`+++ title = "One" +weight = 1 [menu] [menu.p_one] -weight = 1 +++ Front Matter with Menu Pages`) diff --git a/hugolib/pageSort.go b/hugolib/pageSort.go index ec5e2c36d..1288ce719 100644 --- a/hugolib/pageSort.go +++ b/hugolib/pageSort.go @@ -53,6 +53,15 @@ var defaultPageSort = func(p1, p2 *Page) bool { } 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 @@ var languagePageSort = func(p1, p2 *Page) bool { } 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 } diff --git a/hugolib/pageSort_test.go b/hugolib/pageSort_test.go index 738fcc80e..d98018f38 100644 --- a/hugolib/pageSort_test.go +++ b/hugolib/pageSort_test.go @@ -30,22 +30,29 @@ func TestDefaultSort(t *testing.T) { 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 @@ func TestSortByN(t *testing.T) { 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 @@ func TestSortByN(t *testing.T) { {(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 BenchmarkSortByWeightAndReverse(b *testing.B) { } } -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 @@ func createSortTestPages(num int) Pages { Source: Source{File: *source.NewFile(filepath.FromSlash(fmt.Sprintf("/x/y/p%d.md", i)))}, } w := 5 + if i%2 == 0 { w = 10 }