1
0
mirror of https://github.com/gohugoio/hugo.git synced 2024-05-11 05:54:58 +00:00

hugolib: Avoid double-encoding of paginator URLs

Fixes #2177
This commit is contained in:
Bjørn Erik Pedersen
2017-01-08 16:54:05 +01:00
parent bc06135c96
commit 4ea4359ac1
3 changed files with 56 additions and 22 deletions

View File

@@ -20,7 +20,6 @@ import (
"math"
"path"
"reflect"
"strings"
"github.com/spf13/cast"
"github.com/spf13/hugo/helpers"
@@ -280,7 +279,7 @@ func (p *Page) Paginator(options ...interface{}) (*Pager, error) {
return
}
pagers, err := paginatePages(p.Data["Pages"], pagerSize, p.URL())
pagers, err := paginatePages(p.Data["Pages"], pagerSize, p.sections...)
if err != nil {
initError = err
@@ -306,9 +305,9 @@ func (p *Page) Paginator(options ...interface{}) (*Pager, error) {
// Paginate gets this Node's paginator if it's already created.
// If it's not, one will be created with the qiven sequence.
// Note that repeated calls will return the same result, even if the sequence is different.
func (n *Page) Paginate(seq interface{}, options ...interface{}) (*Pager, error) {
if !n.IsNode() {
return nil, fmt.Errorf("Paginators not supported for pages of type %q (%q)", n.Kind, n.Title)
func (p *Page) Paginate(seq interface{}, options ...interface{}) (*Pager, error) {
if !p.IsNode() {
return nil, fmt.Errorf("Paginators not supported for pages of type %q (%q)", p.Kind, p.Title)
}
pagerSize, err := resolvePagerSize(options...)
@@ -319,11 +318,11 @@ func (n *Page) Paginate(seq interface{}, options ...interface{}) (*Pager, error)
var initError error
n.paginatorInit.Do(func() {
if n.paginator != nil {
p.paginatorInit.Do(func() {
if p.paginator != nil {
return
}
pagers, err := paginatePages(seq, pagerSize, n.URL())
pagers, err := paginatePages(seq, pagerSize, p.sections...)
if err != nil {
initError = err
@@ -331,10 +330,10 @@ func (n *Page) Paginate(seq interface{}, options ...interface{}) (*Pager, error)
if len(pagers) > 0 {
// the rest of the nodes will be created later
n.paginator = pagers[0]
n.paginator.source = seq
n.paginator.options = options
n.Site.addToPaginationPageCount(uint64(n.paginator.TotalPages()))
p.paginator = pagers[0]
p.paginator.source = seq
p.paginator.options = options
p.Site.addToPaginationPageCount(uint64(p.paginator.TotalPages()))
}
})
@@ -343,15 +342,15 @@ func (n *Page) Paginate(seq interface{}, options ...interface{}) (*Pager, error)
return nil, initError
}
if n.paginator.source == "paginator" {
if p.paginator.source == "paginator" {
return nil, errors.New("a Paginator was previously built for this Node without filters; look for earlier .Paginator usage")
}
if !reflect.DeepEqual(options, n.paginator.options) || !probablyEqualPageLists(n.paginator.source, seq) {
if !reflect.DeepEqual(options, p.paginator.options) || !probablyEqualPageLists(p.paginator.source, seq) {
return nil, errors.New("invoked multiple times with different arguments")
}
return n.paginator, nil
return p.paginator, nil
}
func resolvePagerSize(options ...interface{}) (int, error) {
@@ -372,14 +371,13 @@ func resolvePagerSize(options ...interface{}) (int, error) {
return pas, nil
}
func paginatePages(seq interface{}, pagerSize int, section string) (pagers, error) {
func paginatePages(seq interface{}, pagerSize int, sections ...string) (pagers, error) {
if pagerSize <= 0 {
return nil, errors.New("'paginate' configuration setting must be positive to paginate")
}
section = strings.TrimSuffix(section, ".html")
urlFactory := newPaginationURLFactory(section)
urlFactory := newPaginationURLFactory(sections...)
var paginator *paginator
@@ -509,12 +507,14 @@ func newPaginator(elements []paginatedElement, total, size int, urlFactory pagin
func newPaginationURLFactory(pathElements ...string) paginationURLFactory {
pathSpec := helpers.CurrentPathSpec()
basePath := path.Join(pathElements...)
return func(page int) string {
var rel string
if page == 1 {
rel = fmt.Sprintf("/%s/", path.Join(pathElements...))
rel = fmt.Sprintf("/%s/", basePath)
} else {
rel = fmt.Sprintf("/%s/%s/%d/", path.Join(pathElements...), pathSpec.PaginatePath(), page)
rel = fmt.Sprintf("/%s/%s/%d/", basePath, pathSpec.PaginatePath(), page)
}
return pathSpec.URLizeAndPrep(rel)