hugolib: Fix page sorting when weight is zero
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sun, 6 Nov 2016 14:38:52 +0000 (15:38 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sun, 6 Nov 2016 14:39:33 +0000 (15:39 +0100)
Fixes #2673

hugolib/hugo_sites_test.go
hugolib/menu_test.go
hugolib/pageSort.go
hugolib/pageSort_test.go

index e14a04cb6aa789c5541381a08073ccb3f5575d5f..5a44fb5f9cd5a4df8882d98c9900ed73038072a7 100644 (file)
@@ -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:
index 38bcd1c05624a2ba7811ab2764ce93e1f95691a4..53ffc720c733419be92e631fb00e9f837367c726 100644 (file)
@@ -87,9 +87,9 @@ const (
 
 var menuPage1 = []byte(`+++
 title = "One"
+weight = 1
 [menu]
        [menu.p_one]
-weight = 1
 +++
 Front Matter with Menu Pages`)
 
index ec5e2c36da81092c9b0f2f3ded553f7b8ad5ec04..1288ce719128c49ce27462220cb3909508be07bd 100644 (file)
@@ -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
 }
 
index 738fcc80e1a73dbfc3ed3d0a30abd6b85d6f7259..d98018f38624a444f78baa65c7586202c153ae90 100644 (file)
@@ -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
                }