hugolib: Deprecate Pages.Sort

In favour of ByWeight.
This commit is contained in:
Bjørn Erik Pedersen 2018-09-21 14:23:00 +02:00
parent 2eed35c826
commit 2e2e34a935
No known key found for this signature in database
GPG key ID: 330E6E2BD4859D8F
6 changed files with 20 additions and 12 deletions

View file

@ -525,10 +525,10 @@ func (h *HugoSites) createMissingPages() error {
first.AllPages = append(first.AllPages, newPages...)
first.AllPages.Sort()
first.AllPages.sort()
for _, s := range h.Sites {
s.Pages.Sort()
s.Pages.sort()
}
for i := 1; i < len(h.Sites); i++ {
@ -574,7 +574,7 @@ func (h *HugoSites) setupTranslations() {
allPages = append(allPages, s.Pages...)
}
allPages.Sort()
allPages.sort()
for _, s := range h.Sites {
s.AllPages = allPages

View file

@ -1,4 +1,4 @@
// Copyright 2015 The Hugo Authors. All rights reserved.
// Copyright 2018 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@ -14,6 +14,8 @@
package hugolib
import (
"github.com/gohugoio/hugo/helpers"
"sort"
"github.com/spf13/cast"
@ -98,6 +100,12 @@ func (ps *pageSorter) Less(i, j int) bool { return ps.by(ps.pages[i], ps.pages[j
// Sort sorts the pages by the default sort order defined:
// Order by Weight, Date, LinkTitle and then full file path.
func (p Pages) Sort() {
// Remove in Hugo 0.51
helpers.Deprecated("Pages", "Sort", "Use .ByWeight", false)
p.sort()
}
func (p Pages) sort() {
pageBy(defaultPageSort).Sort(p)
}

View file

@ -35,24 +35,24 @@ func TestDefaultSort(t *testing.T) {
// first by weight
setSortVals([4]time.Time{d1, d2, d3, d4}, [4]string{"b", "a", "c", "d"}, [4]int{4, 3, 2, 1}, p)
p.Sort()
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)
// next by date
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)
// finally by link title
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, "bl", p[1].LinkTitle())
assert.Equal(t, "cl", p[2].LinkTitle())

View file

@ -192,7 +192,7 @@ func (s *siteContentProcessor) process(ctx context.Context) error {
return err
}
s.site.rawAllPages.Sort()
s.site.rawAllPages.sort()
return nil

View file

@ -42,7 +42,7 @@ func (p1 Pages) MergeByLanguage(p2 Pages) Pages {
}
}
pages.Sort()
pages.sort()
}
out, _ := spc.getP("pages.MergeByLanguage", merge, p1, p2)

View file

@ -326,7 +326,7 @@ func (s *Site) assembleSections() Pages {
for _, sect := range sectionPages {
if sect.parent != nil {
sect.parent.subSections.Sort()
sect.parent.subSections.sort()
}
for i, p := range sect.Pages {
@ -356,7 +356,7 @@ func (s *Site) assembleSections() Pages {
}
func (p *Page) setPagePages(pages Pages) {
pages.Sort()
pages.sort()
p.Pages = pages
p.data = make(map[string]interface{})
p.data["Pages"] = pages