2019-01-02 06:33:26 -05:00
|
|
|
// Copyright 2019 The Hugo Authors. All rights reserved.
|
2014-12-27 08:11:19 -05:00
|
|
|
//
|
2015-11-23 22:16:36 -05:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
2014-12-27 08:11:19 -05:00
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2015-11-23 22:16:36 -05:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2014-12-27 08:11:19 -05:00
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
package page
|
2014-12-27 08:11:19 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"math"
|
2015-03-31 16:33:24 -04:00
|
|
|
"reflect"
|
2016-07-26 13:04:10 -04:00
|
|
|
|
2017-06-13 12:42:45 -04:00
|
|
|
"github.com/gohugoio/hugo/config"
|
2017-02-04 22:20:06 -05:00
|
|
|
|
2016-07-26 13:04:10 -04:00
|
|
|
"github.com/spf13/cast"
|
2014-12-27 08:11:19 -05:00
|
|
|
)
|
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
// PaginatorProvider provides two ways to create a page paginator.
|
|
|
|
type PaginatorProvider interface {
|
|
|
|
Paginator(options ...interface{}) (*Pager, error)
|
|
|
|
Paginate(seq interface{}, options ...interface{}) (*Pager, error)
|
|
|
|
}
|
|
|
|
|
2016-03-24 09:18:47 -04:00
|
|
|
// Pager represents one of the elements in a paginator.
|
|
|
|
// The number, starting on 1, represents its place.
|
2015-04-21 15:25:42 -04:00
|
|
|
type Pager struct {
|
2014-12-27 08:11:19 -05:00
|
|
|
number int
|
2019-01-02 06:33:26 -05:00
|
|
|
*Paginator
|
2014-12-27 08:11:19 -05:00
|
|
|
}
|
|
|
|
|
2016-11-13 08:27:10 -05:00
|
|
|
func (p Pager) String() string {
|
|
|
|
return fmt.Sprintf("Pager %d", p.number)
|
|
|
|
}
|
|
|
|
|
2015-07-16 07:17:54 -04:00
|
|
|
type paginatedElement interface {
|
|
|
|
Len() int
|
|
|
|
}
|
|
|
|
|
2015-04-21 15:25:42 -04:00
|
|
|
type pagers []*Pager
|
2014-12-27 08:11:19 -05:00
|
|
|
|
2016-01-08 16:55:19 -05:00
|
|
|
var (
|
|
|
|
paginatorEmptyPages Pages
|
|
|
|
paginatorEmptyPageGroups PagesGroup
|
|
|
|
)
|
2015-03-05 18:30:51 -05:00
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
type Paginator struct {
|
2015-07-16 07:17:54 -04:00
|
|
|
paginatedElements []paginatedElement
|
2014-12-27 08:11:19 -05:00
|
|
|
pagers
|
2015-03-11 13:34:57 -04:00
|
|
|
paginationURLFactory
|
2019-01-02 06:33:26 -05:00
|
|
|
total int
|
|
|
|
size int
|
2014-12-27 08:11:19 -05:00
|
|
|
}
|
|
|
|
|
2015-03-11 13:34:57 -04:00
|
|
|
type paginationURLFactory func(int) string
|
2014-12-27 08:11:19 -05:00
|
|
|
|
|
|
|
// PageNumber returns the current page's number in the pager sequence.
|
2015-04-21 15:25:42 -04:00
|
|
|
func (p *Pager) PageNumber() int {
|
2014-12-27 08:11:19 -05:00
|
|
|
return p.number
|
|
|
|
}
|
|
|
|
|
2015-03-18 01:16:54 -04:00
|
|
|
// URL returns the URL to the current page.
|
2015-04-21 15:25:42 -04:00
|
|
|
func (p *Pager) URL() template.HTML {
|
2015-03-11 13:34:57 -04:00
|
|
|
return template.HTML(p.paginationURLFactory(p.PageNumber()))
|
2014-12-27 08:11:19 -05:00
|
|
|
}
|
|
|
|
|
2015-07-16 07:17:54 -04:00
|
|
|
// Pages returns the Pages on this page.
|
|
|
|
// Note: If this return a non-empty result, then PageGroups() will return empty.
|
2015-04-21 15:25:42 -04:00
|
|
|
func (p *Pager) Pages() Pages {
|
2015-07-16 07:17:54 -04:00
|
|
|
if len(p.paginatedElements) == 0 {
|
|
|
|
return paginatorEmptyPages
|
|
|
|
}
|
|
|
|
|
|
|
|
if pages, ok := p.element().(Pages); ok {
|
|
|
|
return pages
|
|
|
|
}
|
|
|
|
|
|
|
|
return paginatorEmptyPages
|
|
|
|
}
|
|
|
|
|
|
|
|
// PageGroups return Page groups for this page.
|
|
|
|
// Note: If this return non-empty result, then Pages() will return empty.
|
|
|
|
func (p *Pager) PageGroups() PagesGroup {
|
|
|
|
if len(p.paginatedElements) == 0 {
|
|
|
|
return paginatorEmptyPageGroups
|
|
|
|
}
|
|
|
|
|
|
|
|
if groups, ok := p.element().(PagesGroup); ok {
|
|
|
|
return groups
|
|
|
|
}
|
|
|
|
|
|
|
|
return paginatorEmptyPageGroups
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Pager) element() paginatedElement {
|
|
|
|
if len(p.paginatedElements) == 0 {
|
2015-03-05 18:30:51 -05:00
|
|
|
return paginatorEmptyPages
|
|
|
|
}
|
2015-07-16 07:17:54 -04:00
|
|
|
return p.paginatedElements[p.PageNumber()-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
// page returns the Page with the given index
|
2019-01-02 06:33:26 -05:00
|
|
|
func (p *Pager) page(index int) (Page, error) {
|
2015-07-16 07:17:54 -04:00
|
|
|
if pages, ok := p.element().(Pages); ok {
|
|
|
|
if pages != nil && len(pages) > index {
|
|
|
|
return pages[index], nil
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// must be PagesGroup
|
|
|
|
// this construction looks clumsy, but ...
|
|
|
|
// ... it is the difference between 99.5% and 100% test coverage :-)
|
|
|
|
groups := p.element().(PagesGroup)
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
for _, v := range groups {
|
|
|
|
for _, page := range v.Pages {
|
|
|
|
if i == index {
|
|
|
|
return page, nil
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, nil
|
2014-12-27 08:11:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NumberOfElements gets the number of elements on this page.
|
2015-04-21 15:25:42 -04:00
|
|
|
func (p *Pager) NumberOfElements() int {
|
2015-07-16 07:17:54 -04:00
|
|
|
return p.element().Len()
|
2014-12-27 08:11:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// HasPrev tests whether there are page(s) before the current.
|
2015-04-21 15:25:42 -04:00
|
|
|
func (p *Pager) HasPrev() bool {
|
2014-12-27 08:11:19 -05:00
|
|
|
return p.PageNumber() > 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prev returns the pager for the previous page.
|
2015-04-21 15:25:42 -04:00
|
|
|
func (p *Pager) Prev() *Pager {
|
2014-12-27 08:11:19 -05:00
|
|
|
if !p.HasPrev() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return p.pagers[p.PageNumber()-2]
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasNext tests whether there are page(s) after the current.
|
2015-04-21 15:25:42 -04:00
|
|
|
func (p *Pager) HasNext() bool {
|
2015-07-16 07:17:54 -04:00
|
|
|
return p.PageNumber() < len(p.paginatedElements)
|
2014-12-27 08:11:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Next returns the pager for the next page.
|
2015-04-21 15:25:42 -04:00
|
|
|
func (p *Pager) Next() *Pager {
|
2014-12-27 08:11:19 -05:00
|
|
|
if !p.HasNext() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return p.pagers[p.PageNumber()]
|
|
|
|
}
|
|
|
|
|
|
|
|
// First returns the pager for the first page.
|
2015-04-21 15:25:42 -04:00
|
|
|
func (p *Pager) First() *Pager {
|
2014-12-27 08:11:19 -05:00
|
|
|
return p.pagers[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Last returns the pager for the last page.
|
2015-04-21 15:25:42 -04:00
|
|
|
func (p *Pager) Last() *Pager {
|
2014-12-27 08:11:19 -05:00
|
|
|
return p.pagers[len(p.pagers)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pagers returns a list of pagers that can be used to build a pagination menu.
|
2019-01-02 06:33:26 -05:00
|
|
|
func (p *Paginator) Pagers() pagers {
|
2014-12-27 08:11:19 -05:00
|
|
|
return p.pagers
|
|
|
|
}
|
|
|
|
|
|
|
|
// PageSize returns the size of each paginator page.
|
2019-01-02 06:33:26 -05:00
|
|
|
func (p *Paginator) PageSize() int {
|
2014-12-27 08:11:19 -05:00
|
|
|
return p.size
|
|
|
|
}
|
|
|
|
|
|
|
|
// TotalPages returns the number of pages in the paginator.
|
2019-01-02 06:33:26 -05:00
|
|
|
func (p *Paginator) TotalPages() int {
|
2015-07-16 07:17:54 -04:00
|
|
|
return len(p.paginatedElements)
|
2014-12-27 08:11:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// TotalNumberOfElements returns the number of elements on all pages in this paginator.
|
2019-01-02 06:33:26 -05:00
|
|
|
func (p *Paginator) TotalNumberOfElements() int {
|
2014-12-27 08:11:19 -05:00
|
|
|
return p.total
|
|
|
|
}
|
|
|
|
|
2015-07-16 07:17:54 -04:00
|
|
|
func splitPages(pages Pages, size int) []paginatedElement {
|
|
|
|
var split []paginatedElement
|
2014-12-27 08:11:19 -05:00
|
|
|
for low, j := 0, len(pages); low < j; low += size {
|
|
|
|
high := int(math.Min(float64(low+size), float64(len(pages))))
|
|
|
|
split = append(split, pages[low:high])
|
|
|
|
}
|
|
|
|
|
|
|
|
return split
|
|
|
|
}
|
|
|
|
|
2015-07-16 07:17:54 -04:00
|
|
|
func splitPageGroups(pageGroups PagesGroup, size int) []paginatedElement {
|
|
|
|
type keyPage struct {
|
|
|
|
key interface{}
|
2019-01-02 06:33:26 -05:00
|
|
|
page Page
|
2015-07-16 07:17:54 -04:00
|
|
|
}
|
|
|
|
|
2016-01-08 16:55:19 -05:00
|
|
|
var (
|
|
|
|
split []paginatedElement
|
|
|
|
flattened []keyPage
|
|
|
|
)
|
2015-07-16 07:17:54 -04:00
|
|
|
|
|
|
|
for _, g := range pageGroups {
|
|
|
|
for _, p := range g.Pages {
|
|
|
|
flattened = append(flattened, keyPage{g.Key, p})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
numPages := len(flattened)
|
|
|
|
|
|
|
|
for low, j := 0, numPages; low < j; low += size {
|
|
|
|
high := int(math.Min(float64(low+size), float64(numPages)))
|
2016-01-08 16:55:19 -05:00
|
|
|
|
|
|
|
var (
|
|
|
|
pg PagesGroup
|
2016-03-24 09:18:47 -04:00
|
|
|
key interface{}
|
|
|
|
groupIndex = -1
|
2016-01-08 16:55:19 -05:00
|
|
|
)
|
|
|
|
|
2015-07-16 07:17:54 -04:00
|
|
|
for k := low; k < high; k++ {
|
|
|
|
kp := flattened[k]
|
|
|
|
if key == nil || key != kp.key {
|
|
|
|
key = kp.key
|
|
|
|
pg = append(pg, PageGroup{Key: key})
|
|
|
|
groupIndex++
|
|
|
|
}
|
|
|
|
pg[groupIndex].Pages = append(pg[groupIndex].Pages, kp.page)
|
|
|
|
}
|
|
|
|
split = append(split, pg)
|
|
|
|
}
|
|
|
|
|
|
|
|
return split
|
|
|
|
}
|
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
func ResolvePagerSize(cfg config.Provider, options ...interface{}) (int, error) {
|
2015-03-31 13:12:54 -04:00
|
|
|
if len(options) == 0 {
|
2017-02-04 22:20:06 -05:00
|
|
|
return cfg.GetInt("paginate"), nil
|
2015-03-31 13:12:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(options) > 1 {
|
|
|
|
return -1, errors.New("too many arguments, 'pager size' is currently the only option")
|
|
|
|
}
|
|
|
|
|
|
|
|
pas, err := cast.ToIntE(options[0])
|
|
|
|
|
|
|
|
if err != nil || pas <= 0 {
|
|
|
|
return -1, errors.New(("'pager size' must be a positive integer"))
|
|
|
|
}
|
|
|
|
|
|
|
|
return pas, nil
|
|
|
|
}
|
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
func Paginate(td TargetPathDescriptor, seq interface{}, pagerSize int) (*Paginator, error) {
|
2015-03-31 13:12:54 -04:00
|
|
|
if pagerSize <= 0 {
|
2014-12-27 08:11:19 -05:00
|
|
|
return nil, errors.New("'paginate' configuration setting must be positive to paginate")
|
|
|
|
}
|
2015-03-31 13:12:54 -04:00
|
|
|
|
2017-03-09 13:19:29 -05:00
|
|
|
urlFactory := newPaginationURLFactory(td)
|
2015-07-16 07:17:54 -04:00
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
var paginator *Paginator
|
2015-07-16 07:17:54 -04:00
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
groups, err := ToPagesGroup(seq)
|
2018-09-08 05:14:09 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if groups != nil {
|
2015-07-16 07:17:54 -04:00
|
|
|
paginator, _ = newPaginatorFromPageGroups(groups, pagerSize, urlFactory)
|
|
|
|
} else {
|
2019-01-02 06:33:26 -05:00
|
|
|
pages, err := ToPages(seq)
|
2015-07-16 07:17:54 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
paginator, _ = newPaginatorFromPages(pages, pagerSize, urlFactory)
|
|
|
|
}
|
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
return paginator, nil
|
2015-03-31 16:33:24 -04:00
|
|
|
}
|
2014-12-27 08:11:19 -05:00
|
|
|
|
2015-03-31 16:33:24 -04:00
|
|
|
// probablyEqual checks page lists for probable equality.
|
|
|
|
// It may return false positives.
|
|
|
|
// The motivation behind this is to avoid potential costly reflect.DeepEqual
|
|
|
|
// when "probably" is good enough.
|
|
|
|
func probablyEqualPageLists(a1 interface{}, a2 interface{}) bool {
|
|
|
|
if a1 == nil || a2 == nil {
|
|
|
|
return a1 == a2
|
|
|
|
}
|
|
|
|
|
2015-07-16 07:17:54 -04:00
|
|
|
t1 := reflect.TypeOf(a1)
|
|
|
|
t2 := reflect.TypeOf(a2)
|
|
|
|
|
|
|
|
if t1 != t2 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if g1, ok := a1.(PagesGroup); ok {
|
|
|
|
g2 := a2.(PagesGroup)
|
|
|
|
if len(g1) != len(g2) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if len(g1) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if g1.Len() != g2.Len() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return g1[0].Pages[0] == g2[0].Pages[0]
|
|
|
|
}
|
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
p1, err1 := ToPages(a1)
|
|
|
|
p2, err2 := ToPages(a2)
|
2015-03-31 16:33:24 -04:00
|
|
|
|
|
|
|
// probably the same wrong type
|
|
|
|
if err1 != nil && err2 != nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(p1) != len(p2) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(p1) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return p1[0] == p2[0]
|
2014-12-27 08:11:19 -05:00
|
|
|
}
|
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
func newPaginatorFromPages(pages Pages, size int, urlFactory paginationURLFactory) (*Paginator, error) {
|
2014-12-27 08:11:19 -05:00
|
|
|
if size <= 0 {
|
2015-01-26 19:27:07 -05:00
|
|
|
return nil, errors.New("Paginator size must be positive")
|
2014-12-27 08:11:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
split := splitPages(pages, size)
|
|
|
|
|
2015-07-16 07:17:54 -04:00
|
|
|
return newPaginator(split, len(pages), size, urlFactory)
|
|
|
|
}
|
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
func newPaginatorFromPageGroups(pageGroups PagesGroup, size int, urlFactory paginationURLFactory) (*Paginator, error) {
|
2015-07-16 07:17:54 -04:00
|
|
|
if size <= 0 {
|
|
|
|
return nil, errors.New("Paginator size must be positive")
|
|
|
|
}
|
|
|
|
|
|
|
|
split := splitPageGroups(pageGroups, size)
|
|
|
|
|
|
|
|
return newPaginator(split, pageGroups.Len(), size, urlFactory)
|
|
|
|
}
|
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
func newPaginator(elements []paginatedElement, total, size int, urlFactory paginationURLFactory) (*Paginator, error) {
|
|
|
|
p := &Paginator{total: total, paginatedElements: elements, size: size, paginationURLFactory: urlFactory}
|
2014-12-27 08:11:19 -05:00
|
|
|
|
2015-03-05 18:30:51 -05:00
|
|
|
var ps pagers
|
|
|
|
|
2015-07-16 07:17:54 -04:00
|
|
|
if len(elements) > 0 {
|
|
|
|
ps = make(pagers, len(elements))
|
|
|
|
for i := range p.paginatedElements {
|
2019-01-02 06:33:26 -05:00
|
|
|
ps[i] = &Pager{number: (i + 1), Paginator: p}
|
2015-03-05 18:30:51 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ps = make(pagers, 1)
|
2019-01-02 06:33:26 -05:00
|
|
|
ps[0] = &Pager{number: 1, Paginator: p}
|
2014-12-27 08:11:19 -05:00
|
|
|
}
|
|
|
|
|
2015-03-05 18:30:51 -05:00
|
|
|
p.pagers = ps
|
2014-12-27 08:11:19 -05:00
|
|
|
|
2015-01-26 19:27:07 -05:00
|
|
|
return p, nil
|
2014-12-27 08:11:19 -05:00
|
|
|
}
|
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
func newPaginationURLFactory(d TargetPathDescriptor) paginationURLFactory {
|
|
|
|
return func(pageNumber int) string {
|
2017-03-09 13:19:29 -05:00
|
|
|
pathDescriptor := d
|
2014-12-27 08:11:19 -05:00
|
|
|
var rel string
|
2019-01-02 06:33:26 -05:00
|
|
|
if pageNumber > 1 {
|
|
|
|
rel = fmt.Sprintf("/%s/%d/", d.PathSpec.PaginatePath, pageNumber)
|
2017-03-09 13:19:29 -05:00
|
|
|
pathDescriptor.Addends = rel
|
2014-12-27 08:11:19 -05:00
|
|
|
}
|
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
return CreateTargetPaths(pathDescriptor).RelPermalink(d.PathSpec)
|
2014-12-27 08:11:19 -05:00
|
|
|
}
|
|
|
|
}
|