hugolib: Fix page sorting when weight is zero

Fixes #2673
This commit is contained in:
Bjørn Erik Pedersen 2016-11-06 15:38:52 +01:00
parent 52e2fd27f8
commit 4b4ab47553
4 changed files with 50 additions and 12 deletions

View file

@ -1023,6 +1023,7 @@ func createMultiTestSitesForConfig(t *testing.T, siteConfig testSiteConfig, conf
sources := []source.ByteSource{ sources := []source.ByteSource{
{Name: filepath.FromSlash("sect/doc1.en.md"), Content: []byte(`--- {Name: filepath.FromSlash("sect/doc1.en.md"), Content: []byte(`---
title: doc1 title: doc1
weight: 1
slug: doc1-slug slug: doc1-slug
tags: tags:
- tag1 - tag1
@ -1037,6 +1038,7 @@ NOTE: slug should be used as URL
`)}, `)},
{Name: filepath.FromSlash("sect/doc1.fr.md"), Content: []byte(`--- {Name: filepath.FromSlash("sect/doc1.fr.md"), Content: []byte(`---
title: doc1 title: doc1
weight: 1
plaques: plaques:
- frtag1 - frtag1
- frtag2 - frtag2
@ -1052,6 +1054,7 @@ NOTE: date is after "doc3"
`)}, `)},
{Name: filepath.FromSlash("sect/doc2.en.md"), Content: []byte(`--- {Name: filepath.FromSlash("sect/doc2.en.md"), Content: []byte(`---
title: doc2 title: doc2
weight: 2
publishdate: "2000-01-02" publishdate: "2000-01-02"
--- ---
# doc2 # 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(`--- {Name: filepath.FromSlash("sect/doc3.en.md"), Content: []byte(`---
title: doc3 title: doc3
weight: 3
publishdate: "2000-01-03" publishdate: "2000-01-03"
tags: tags:
- tag2 - tag2
@ -1072,6 +1076,7 @@ NOTE: third 'en' doc, should trigger pagination on home page.
`)}, `)},
{Name: filepath.FromSlash("sect/doc4.md"), Content: []byte(`--- {Name: filepath.FromSlash("sect/doc4.md"), Content: []byte(`---
title: doc4 title: doc4
weight: 4
plaques: plaques:
- frtag1 - frtag1
publishdate: "2000-01-05" 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(`--- {Name: filepath.FromSlash("other/doc5.fr.md"), Content: []byte(`---
title: doc5 title: doc5
weight: 5
publishdate: "2000-01-06" publishdate: "2000-01-06"
--- ---
# doc5 # doc5
@ -1099,12 +1105,14 @@ expiryDate: "2001-01-06"
`)}, `)},
{Name: filepath.FromSlash("stats/future.fr.md"), Content: []byte(`--- {Name: filepath.FromSlash("stats/future.fr.md"), Content: []byte(`---
title: future title: future
weight: 6
publishdate: "2100-01-06" publishdate: "2100-01-06"
--- ---
# Future # Future
`)}, `)},
{Name: filepath.FromSlash("stats/expired.en.md"), Content: []byte(`--- {Name: filepath.FromSlash("stats/expired.en.md"), Content: []byte(`---
title: expired title: expired
weight: 7
publishdate: "2000-01-06" publishdate: "2000-01-06"
expiryDate: "2001-01-06" expiryDate: "2001-01-06"
--- ---
@ -1112,6 +1120,7 @@ expiryDate: "2001-01-06"
`)}, `)},
{Name: filepath.FromSlash("stats/future.en.md"), Content: []byte(`--- {Name: filepath.FromSlash("stats/future.en.md"), Content: []byte(`---
title: future title: future
weight: 6
publishdate: "2100-01-06" publishdate: "2100-01-06"
--- ---
# Future # Future
@ -1125,6 +1134,7 @@ draft: true
`)}, `)},
{Name: filepath.FromSlash("stats/tax.nn.md"), Content: []byte(`--- {Name: filepath.FromSlash("stats/tax.nn.md"), Content: []byte(`---
title: Tax NN title: Tax NN
weight: 8
publishdate: "2000-01-06" publishdate: "2000-01-06"
weight: 1001 weight: 1001
lag: lag:
@ -1134,6 +1144,7 @@ lag:
`)}, `)},
{Name: filepath.FromSlash("stats/tax.nb.md"), Content: []byte(`--- {Name: filepath.FromSlash("stats/tax.nb.md"), Content: []byte(`---
title: Tax NB title: Tax NB
weight: 8
publishdate: "2000-01-06" publishdate: "2000-01-06"
weight: 1002 weight: 1002
lag: lag:

View file

@ -87,9 +87,9 @@ const (
var menuPage1 = []byte(`+++ var menuPage1 = []byte(`+++
title = "One" title = "One"
weight = 1
[menu] [menu]
[menu.p_one] [menu.p_one]
weight = 1
+++ +++
Front Matter with Menu Pages`) Front Matter with Menu Pages`)

View file

@ -53,6 +53,15 @@ var defaultPageSort = func(p1, p2 *Page) bool {
} }
return p1.Date.Unix() > p2.Date.Unix() return p1.Date.Unix() > p2.Date.Unix()
} }
if p2.Weight == 0 {
return true
}
if p1.Weight == 0 {
return false
}
return p1.Weight < p2.Weight return p1.Weight < p2.Weight
} }
@ -66,6 +75,15 @@ var languagePageSort = func(p1, p2 *Page) bool {
} }
return p1.Date.Unix() > p2.Date.Unix() 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 return p1.language.Weight < p2.language.Weight
} }

View file

@ -30,22 +30,29 @@ func TestDefaultSort(t *testing.T) {
d1 := time.Now() d1 := time.Now()
d2 := d1.Add(-1 * time.Hour) d2 := d1.Add(-1 * time.Hour)
d3 := d1.Add(-2 * time.Hour) d3 := d1.Add(-2 * time.Hour)
d4 := d1.Add(-3 * time.Hour)
p := createSortTestPages(3) p := createSortTestPages(4)
// first by weight // 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() p.Sort()
assert.Equal(t, 1, p[0].Weight) assert.Equal(t, 1, p[0].Weight)
// next by date // 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() p.Sort()
assert.Equal(t, d1, p[0].Date) assert.Equal(t, d1, p[0].Date)
// finally by link title // 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() p.Sort()
assert.Equal(t, "al", p[0].LinkTitle()) assert.Equal(t, "al", p[0].LinkTitle())
assert.Equal(t, "bl", p[1].LinkTitle()) assert.Equal(t, "bl", p[1].LinkTitle())
@ -57,8 +64,9 @@ func TestSortByN(t *testing.T) {
d1 := time.Now() d1 := time.Now()
d2 := d1.Add(-2 * time.Hour) d2 := d1.Add(-2 * time.Hour)
d3 := d1.Add(-10 * 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 { for i, this := range []struct {
sortFunc func(p Pages) Pages 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).ByWeight, func(p Pages) bool { return p[0].Weight == 1 }},
{(Pages).ByTitle, func(p Pages) bool { return p[0].Title == "ab" }}, {(Pages).ByTitle, func(p Pages) bool { return p[0].Title == "ab" }},
{(Pages).ByLinkTitle, func(p Pages) bool { return p[0].LinkTitle() == "abl" }}, {(Pages).ByLinkTitle, func(p Pages) bool { return p[0].LinkTitle() == "abl" }},
{(Pages).ByDate, func(p Pages) bool { return p[0].Date == d3 }}, {(Pages).ByDate, func(p Pages) bool { return p[0].Date == d4 }},
{(Pages).ByPublishDate, func(p Pages) bool { return p[0].PublishDate == d3 }}, {(Pages).ByPublishDate, func(p Pages) bool { return p[0].PublishDate == d4 }},
{(Pages).ByExpiryDate, func(p Pages) bool { return p[0].ExpiryDate == d3 }}, {(Pages).ByExpiryDate, func(p Pages) bool { return p[0].ExpiryDate == d4 }},
{(Pages).ByLastmod, func(p Pages) bool { return p[1].Lastmod == d2 }}, {(Pages).ByLastmod, func(p Pages) bool { return p[1].Lastmod == d3 }},
{(Pages).ByLength, func(p Pages) bool { return p[0].Content == "b_content" }}, {(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) sorted := this.sortFunc(p)
if !this.assertFunc(sorted) { 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 { for i := range dates {
pages[i].Date = dates[i] pages[i].Date = dates[i]
pages[i].Lastmod = 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)))}, Source: Source{File: *source.NewFile(filepath.FromSlash(fmt.Sprintf("/x/y/p%d.md", i)))},
} }
w := 5 w := 5
if i%2 == 0 { if i%2 == 0 {
w = 10 w = 10
} }