2019-01-02 06:33:26 -05:00
|
|
|
// Copyright 2019 The Hugo Authors. All rights reserved.
|
2014-08-20 09:12:17 -04:00
|
|
|
//
|
2015-11-23 22:16:36 -05:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
2014-08-20 09:12:17 -04: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-08-20 09:12:17 -04: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-08-20 09:12:17 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2019-01-02 06:33:26 -05:00
|
|
|
"fmt"
|
2014-08-20 09:12:17 -04:00
|
|
|
"reflect"
|
|
|
|
"sort"
|
2014-08-29 23:50:25 -04:00
|
|
|
"strings"
|
2014-10-17 11:10:19 -04:00
|
|
|
"time"
|
2019-01-02 06:33:26 -05:00
|
|
|
|
2020-06-19 04:13:35 -04:00
|
|
|
"github.com/spf13/cast"
|
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
"github.com/gohugoio/hugo/common/collections"
|
2022-03-08 04:06:12 -05:00
|
|
|
"github.com/gohugoio/hugo/common/hreflect"
|
2019-04-02 04:52:43 -04:00
|
|
|
"github.com/gohugoio/hugo/compare"
|
2022-04-05 03:57:58 -04:00
|
|
|
"github.com/gohugoio/hugo/langs"
|
2019-01-02 06:33:26 -05:00
|
|
|
|
|
|
|
"github.com/gohugoio/hugo/resources/resource"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-04-02 04:52:43 -04:00
|
|
|
_ collections.Slicer = PageGroup{}
|
|
|
|
_ compare.ProbablyEqer = PageGroup{}
|
|
|
|
_ compare.ProbablyEqer = PagesGroup{}
|
2014-08-20 09:12:17 -04:00
|
|
|
)
|
|
|
|
|
2016-04-07 09:21:41 -04:00
|
|
|
// PageGroup represents a group of pages, grouped by the key.
|
|
|
|
// The key is typically a year or similar.
|
2014-08-20 09:12:17 -04:00
|
|
|
type PageGroup struct {
|
2022-04-21 04:59:13 -04:00
|
|
|
// The key, typically a year or similar.
|
2022-03-17 17:03:27 -04:00
|
|
|
Key any
|
2022-04-21 04:59:13 -04:00
|
|
|
|
|
|
|
// The Pages in this group.
|
2017-08-19 07:16:00 -04:00
|
|
|
Pages
|
2014-08-20 09:12:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type mapKeyValues []reflect.Value
|
|
|
|
|
|
|
|
func (v mapKeyValues) Len() int { return len(v) }
|
|
|
|
func (v mapKeyValues) Swap(i, j int) { v[i], v[j] = v[j], v[i] }
|
|
|
|
|
|
|
|
type mapKeyByInt struct{ mapKeyValues }
|
|
|
|
|
|
|
|
func (s mapKeyByInt) Less(i, j int) bool { return s.mapKeyValues[i].Int() < s.mapKeyValues[j].Int() }
|
|
|
|
|
2022-04-10 14:30:52 -04:00
|
|
|
type mapKeyByStr struct {
|
|
|
|
less func(a, b string) bool
|
|
|
|
mapKeyValues
|
|
|
|
}
|
2014-08-20 09:12:17 -04:00
|
|
|
|
|
|
|
func (s mapKeyByStr) Less(i, j int) bool {
|
2022-04-10 14:30:52 -04:00
|
|
|
return s.less(s.mapKeyValues[i].String(), s.mapKeyValues[j].String())
|
2014-08-20 09:12:17 -04:00
|
|
|
}
|
|
|
|
|
2022-04-10 14:30:52 -04:00
|
|
|
func sortKeys(examplePage Page, v []reflect.Value, order string) []reflect.Value {
|
2014-08-20 09:12:17 -04:00
|
|
|
if len(v) <= 1 {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
switch v[0].Kind() {
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
if order == "desc" {
|
|
|
|
sort.Sort(sort.Reverse(mapKeyByInt{v}))
|
|
|
|
} else {
|
|
|
|
sort.Sort(mapKeyByInt{v})
|
|
|
|
}
|
|
|
|
case reflect.String:
|
2022-04-10 14:30:52 -04:00
|
|
|
stringLess, close := collatorStringLess(examplePage)
|
|
|
|
defer close()
|
2014-08-20 09:12:17 -04:00
|
|
|
if order == "desc" {
|
2022-04-10 14:30:52 -04:00
|
|
|
sort.Sort(sort.Reverse(mapKeyByStr{stringLess, v}))
|
2014-08-20 09:12:17 -04:00
|
|
|
} else {
|
2022-04-10 14:30:52 -04:00
|
|
|
sort.Sort(mapKeyByStr{stringLess, v})
|
2014-08-20 09:12:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2016-04-07 09:21:41 -04:00
|
|
|
// PagesGroup represents a list of page groups.
|
|
|
|
// This is what you get when doing page grouping in the templates.
|
2014-08-29 23:50:25 -04:00
|
|
|
type PagesGroup []PageGroup
|
|
|
|
|
2016-04-07 09:21:41 -04:00
|
|
|
// Reverse reverses the order of this list of page groups.
|
2014-08-29 23:50:25 -04:00
|
|
|
func (p PagesGroup) Reverse() PagesGroup {
|
|
|
|
for i, j := 0, len(p)-1; i < j; i, j = i+1, j-1 {
|
|
|
|
p[i], p[j] = p[j], p[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2014-10-17 11:10:19 -04:00
|
|
|
var (
|
|
|
|
errorType = reflect.TypeOf((*error)(nil)).Elem()
|
2019-01-02 06:33:26 -05:00
|
|
|
pagePtrType = reflect.TypeOf((*Page)(nil)).Elem()
|
|
|
|
pagesType = reflect.TypeOf(Pages{})
|
2014-10-17 11:10:19 -04:00
|
|
|
)
|
|
|
|
|
2016-04-07 09:21:41 -04:00
|
|
|
// GroupBy groups by the value in the given field or method name and with the given order.
|
|
|
|
// Valid values for order is asc, desc, rev and reverse.
|
2014-08-29 23:51:29 -04:00
|
|
|
func (p Pages) GroupBy(key string, order ...string) (PagesGroup, error) {
|
2014-08-20 09:12:17 -04:00
|
|
|
if len(p) < 1 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2014-08-29 23:51:29 -04:00
|
|
|
direction := "asc"
|
|
|
|
|
|
|
|
if len(order) > 0 && (strings.ToLower(order[0]) == "desc" || strings.ToLower(order[0]) == "rev" || strings.ToLower(order[0]) == "reverse") {
|
|
|
|
direction = "desc"
|
2014-08-20 09:12:17 -04:00
|
|
|
}
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
var ft any
|
2022-03-08 04:06:12 -05:00
|
|
|
index := hreflect.GetMethodIndexByName(pagePtrType, key)
|
|
|
|
if index != -1 {
|
|
|
|
m := pagePtrType.Method(index)
|
2019-01-02 06:33:26 -05:00
|
|
|
if m.Type.NumOut() == 0 || m.Type.NumOut() > 2 {
|
2014-10-17 11:10:19 -04:00
|
|
|
return nil, errors.New(key + " is a Page method but you can't use it with GroupBy")
|
|
|
|
}
|
|
|
|
if m.Type.NumOut() == 1 && m.Type.Out(0).Implements(errorType) {
|
|
|
|
return nil, errors.New(key + " is a Page method but you can't use it with GroupBy")
|
|
|
|
}
|
|
|
|
if m.Type.NumOut() == 2 && !m.Type.Out(1).Implements(errorType) {
|
|
|
|
return nil, errors.New(key + " is a Page method but you can't use it with GroupBy")
|
|
|
|
}
|
|
|
|
ft = m
|
2014-11-11 08:53:10 -05:00
|
|
|
} else {
|
2022-03-08 04:06:12 -05:00
|
|
|
var ok bool
|
2014-11-11 08:53:10 -05:00
|
|
|
ft, ok = pagePtrType.Elem().FieldByName(key)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New(key + " is neither a field nor a method of Page")
|
|
|
|
}
|
2014-08-20 09:12:17 -04:00
|
|
|
}
|
2014-10-16 20:20:09 -04:00
|
|
|
|
2014-10-17 11:10:19 -04:00
|
|
|
var tmp reflect.Value
|
|
|
|
switch e := ft.(type) {
|
|
|
|
case reflect.StructField:
|
2019-01-02 06:33:26 -05:00
|
|
|
tmp = reflect.MakeMap(reflect.MapOf(e.Type, pagesType))
|
2014-10-17 11:10:19 -04:00
|
|
|
case reflect.Method:
|
2019-01-02 06:33:26 -05:00
|
|
|
tmp = reflect.MakeMap(reflect.MapOf(e.Type.Out(0), pagesType))
|
2014-10-17 11:10:19 -04:00
|
|
|
}
|
2014-08-20 09:12:17 -04:00
|
|
|
|
|
|
|
for _, e := range p {
|
|
|
|
ppv := reflect.ValueOf(e)
|
2014-10-17 11:10:19 -04:00
|
|
|
var fv reflect.Value
|
|
|
|
switch ft.(type) {
|
|
|
|
case reflect.StructField:
|
|
|
|
fv = ppv.Elem().FieldByName(key)
|
|
|
|
case reflect.Method:
|
2022-03-08 04:06:12 -05:00
|
|
|
fv = hreflect.GetMethodByName(ppv, key).Call([]reflect.Value{})[0]
|
2014-10-17 11:10:19 -04:00
|
|
|
}
|
|
|
|
if !fv.IsValid() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !tmp.MapIndex(fv).IsValid() {
|
2019-01-02 06:33:26 -05:00
|
|
|
tmp.SetMapIndex(fv, reflect.MakeSlice(pagesType, 0, 0))
|
2014-10-17 11:10:19 -04:00
|
|
|
}
|
2014-11-11 08:53:10 -05:00
|
|
|
tmp.SetMapIndex(fv, reflect.Append(tmp.MapIndex(fv), ppv))
|
2014-10-17 11:10:19 -04:00
|
|
|
}
|
|
|
|
|
2022-04-10 14:30:52 -04:00
|
|
|
sortedKeys := sortKeys(p[0], tmp.MapKeys(), direction)
|
2017-11-11 03:39:43 -05:00
|
|
|
r := make([]PageGroup, len(sortedKeys))
|
|
|
|
for i, k := range sortedKeys {
|
2019-01-02 06:33:26 -05:00
|
|
|
r[i] = PageGroup{Key: k.Interface(), Pages: tmp.MapIndex(k).Interface().(Pages)}
|
2014-10-17 11:10:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
2016-04-07 09:21:41 -04:00
|
|
|
// GroupByParam groups by the given page parameter key's value and with the given order.
|
|
|
|
// Valid values for order is asc, desc, rev and reverse.
|
2014-10-17 11:10:19 -04:00
|
|
|
func (p Pages) GroupByParam(key string, order ...string) (PagesGroup, error) {
|
|
|
|
if len(p) < 1 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
direction := "asc"
|
|
|
|
|
|
|
|
if len(order) > 0 && (strings.ToLower(order[0]) == "desc" || strings.ToLower(order[0]) == "rev" || strings.ToLower(order[0]) == "reverse") {
|
|
|
|
direction = "desc"
|
|
|
|
}
|
|
|
|
|
|
|
|
var tmp reflect.Value
|
|
|
|
var keyt reflect.Type
|
|
|
|
for _, e := range p {
|
2019-01-02 06:33:26 -05:00
|
|
|
param := resource.GetParamToLower(e, key)
|
2014-10-17 11:10:19 -04:00
|
|
|
if param != nil {
|
|
|
|
if _, ok := param.([]string); !ok {
|
|
|
|
keyt = reflect.TypeOf(param)
|
2019-01-02 06:33:26 -05:00
|
|
|
tmp = reflect.MakeMap(reflect.MapOf(keyt, pagesType))
|
2014-10-17 11:10:19 -04:00
|
|
|
break
|
2014-10-16 20:20:09 -04:00
|
|
|
}
|
2014-08-20 09:12:17 -04:00
|
|
|
}
|
|
|
|
}
|
2014-10-17 11:10:19 -04:00
|
|
|
if !tmp.IsValid() {
|
2019-01-02 06:33:26 -05:00
|
|
|
return nil, errors.New("there is no such a param")
|
2014-10-17 11:10:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, e := range p {
|
2019-01-02 06:33:26 -05:00
|
|
|
param := resource.GetParam(e, key)
|
|
|
|
|
2014-10-17 11:10:19 -04:00
|
|
|
if param == nil || reflect.TypeOf(param) != keyt {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
v := reflect.ValueOf(param)
|
|
|
|
if !tmp.MapIndex(v).IsValid() {
|
2019-01-02 06:33:26 -05:00
|
|
|
tmp.SetMapIndex(v, reflect.MakeSlice(pagesType, 0, 0))
|
2014-10-17 11:10:19 -04:00
|
|
|
}
|
|
|
|
tmp.SetMapIndex(v, reflect.Append(tmp.MapIndex(v), reflect.ValueOf(e)))
|
|
|
|
}
|
2014-08-20 09:12:17 -04:00
|
|
|
|
|
|
|
var r []PageGroup
|
2022-04-10 14:30:52 -04:00
|
|
|
for _, k := range sortKeys(p[0], tmp.MapKeys(), direction) {
|
2019-01-02 06:33:26 -05:00
|
|
|
r = append(r, PageGroup{Key: k.Interface(), Pages: tmp.MapIndex(k).Interface().(Pages)})
|
2014-08-20 09:12:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
2022-04-05 03:57:58 -04:00
|
|
|
func (p Pages) groupByDateField(format string, sorter func(p Pages) Pages, getDate func(p Page) time.Time, order ...string) (PagesGroup, error) {
|
2014-08-20 09:12:17 -04:00
|
|
|
if len(p) < 1 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2014-10-17 11:10:19 -04:00
|
|
|
sp := sorter(p)
|
2014-08-29 23:51:29 -04:00
|
|
|
|
|
|
|
if !(len(order) > 0 && (strings.ToLower(order[0]) == "asc" || strings.ToLower(order[0]) == "rev" || strings.ToLower(order[0]) == "reverse")) {
|
2014-08-20 09:12:17 -04:00
|
|
|
sp = sp.Reverse()
|
|
|
|
}
|
|
|
|
|
2020-06-19 04:13:35 -04:00
|
|
|
if sp == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2022-04-05 03:57:58 -04:00
|
|
|
firstPage := sp[0].(Page)
|
|
|
|
date := getDate(firstPage)
|
|
|
|
|
|
|
|
// Pages may be a mix of multiple languages, so we need to use the language
|
|
|
|
// for the currently rendered Site.
|
|
|
|
currentSite := firstPage.Site().Current()
|
|
|
|
formatter := langs.GetTimeFormatter(currentSite.Language())
|
|
|
|
formatted := formatter.Format(date, format)
|
2014-08-20 09:12:17 -04:00
|
|
|
var r []PageGroup
|
2022-04-05 03:57:58 -04:00
|
|
|
r = append(r, PageGroup{Key: formatted, Pages: make(Pages, 0)})
|
2014-08-29 23:48:24 -04:00
|
|
|
r[0].Pages = append(r[0].Pages, sp[0])
|
2014-08-20 09:12:17 -04:00
|
|
|
|
|
|
|
i := 0
|
|
|
|
for _, e := range sp[1:] {
|
2022-04-05 03:57:58 -04:00
|
|
|
date = getDate(e.(Page))
|
|
|
|
formatted := formatter.Format(date, format)
|
|
|
|
if r[i].Key.(string) != formatted {
|
|
|
|
r = append(r, PageGroup{Key: formatted})
|
2014-08-20 09:12:17 -04:00
|
|
|
i++
|
|
|
|
}
|
2014-08-29 23:48:24 -04:00
|
|
|
r[i].Pages = append(r[i].Pages, e)
|
2014-08-20 09:12:17 -04:00
|
|
|
}
|
|
|
|
return r, nil
|
|
|
|
}
|
2014-10-17 11:10:19 -04:00
|
|
|
|
2016-11-21 17:01:42 -05:00
|
|
|
// GroupByDate groups by the given page's Date value in
|
|
|
|
// the given format and with the given order.
|
2016-04-07 09:21:41 -04:00
|
|
|
// Valid values for order is asc, desc, rev and reverse.
|
|
|
|
// For valid format strings, see https://golang.org/pkg/time/#Time.Format
|
2014-10-17 11:10:19 -04:00
|
|
|
func (p Pages) GroupByDate(format string, order ...string) (PagesGroup, error) {
|
|
|
|
sorter := func(p Pages) Pages {
|
|
|
|
return p.ByDate()
|
|
|
|
}
|
2022-04-05 03:57:58 -04:00
|
|
|
getDate := func(p Page) time.Time {
|
|
|
|
return p.Date()
|
2014-10-17 11:10:19 -04:00
|
|
|
}
|
2022-04-05 03:57:58 -04:00
|
|
|
return p.groupByDateField(format, sorter, getDate, order...)
|
2014-10-17 11:10:19 -04:00
|
|
|
}
|
|
|
|
|
2016-11-21 17:01:42 -05:00
|
|
|
// GroupByPublishDate groups by the given page's PublishDate value in
|
|
|
|
// the given format and with the given order.
|
2016-04-07 09:21:41 -04:00
|
|
|
// Valid values for order is asc, desc, rev and reverse.
|
|
|
|
// For valid format strings, see https://golang.org/pkg/time/#Time.Format
|
2014-10-17 11:10:19 -04:00
|
|
|
func (p Pages) GroupByPublishDate(format string, order ...string) (PagesGroup, error) {
|
|
|
|
sorter := func(p Pages) Pages {
|
|
|
|
return p.ByPublishDate()
|
|
|
|
}
|
2022-04-05 03:57:58 -04:00
|
|
|
getDate := func(p Page) time.Time {
|
|
|
|
return p.PublishDate()
|
2014-10-17 11:10:19 -04:00
|
|
|
}
|
2022-04-05 03:57:58 -04:00
|
|
|
return p.groupByDateField(format, sorter, getDate, order...)
|
2014-10-17 11:10:19 -04:00
|
|
|
}
|
|
|
|
|
2016-11-21 17:01:42 -05:00
|
|
|
// GroupByExpiryDate groups by the given page's ExpireDate value in
|
|
|
|
// the given format and with the given order.
|
2016-05-11 10:09:43 -04:00
|
|
|
// Valid values for order is asc, desc, rev and reverse.
|
|
|
|
// For valid format strings, see https://golang.org/pkg/time/#Time.Format
|
|
|
|
func (p Pages) GroupByExpiryDate(format string, order ...string) (PagesGroup, error) {
|
|
|
|
sorter := func(p Pages) Pages {
|
|
|
|
return p.ByExpiryDate()
|
|
|
|
}
|
2022-04-05 03:57:58 -04:00
|
|
|
getDate := func(p Page) time.Time {
|
|
|
|
return p.ExpiryDate()
|
2016-05-11 10:09:43 -04:00
|
|
|
}
|
2022-04-05 03:57:58 -04:00
|
|
|
return p.groupByDateField(format, sorter, getDate, order...)
|
2016-05-11 10:09:43 -04:00
|
|
|
}
|
|
|
|
|
2020-06-19 03:37:37 -04:00
|
|
|
// GroupByLastmod groups by the given page's Lastmod value in
|
|
|
|
// the given format and with the given order.
|
|
|
|
// Valid values for order is asc, desc, rev and reverse.
|
|
|
|
// For valid format strings, see https://golang.org/pkg/time/#Time.Format
|
|
|
|
func (p Pages) GroupByLastmod(format string, order ...string) (PagesGroup, error) {
|
|
|
|
sorter := func(p Pages) Pages {
|
|
|
|
return p.ByLastmod()
|
|
|
|
}
|
2022-04-05 03:57:58 -04:00
|
|
|
getDate := func(p Page) time.Time {
|
|
|
|
return p.Lastmod()
|
2020-06-19 03:37:37 -04:00
|
|
|
}
|
2022-04-05 03:57:58 -04:00
|
|
|
return p.groupByDateField(format, sorter, getDate, order...)
|
2020-06-19 03:37:37 -04:00
|
|
|
}
|
|
|
|
|
2016-11-21 17:01:42 -05:00
|
|
|
// GroupByParamDate groups by a date set as a param on the page in
|
|
|
|
// the given format and with the given order.
|
2016-04-07 09:21:41 -04:00
|
|
|
// Valid values for order is asc, desc, rev and reverse.
|
|
|
|
// For valid format strings, see https://golang.org/pkg/time/#Time.Format
|
2014-10-17 11:10:19 -04:00
|
|
|
func (p Pages) GroupByParamDate(key string, format string, order ...string) (PagesGroup, error) {
|
2020-06-19 04:13:35 -04:00
|
|
|
// Cache the dates.
|
|
|
|
dates := make(map[Page]time.Time)
|
|
|
|
|
|
|
|
sorter := func(pages Pages) Pages {
|
2014-10-17 11:10:19 -04:00
|
|
|
var r Pages
|
2020-06-19 04:13:35 -04:00
|
|
|
|
|
|
|
for _, p := range pages {
|
2020-12-28 10:06:10 -05:00
|
|
|
param := resource.GetParam(p, key)
|
2020-06-19 04:13:35 -04:00
|
|
|
var t time.Time
|
|
|
|
|
|
|
|
if param != nil {
|
|
|
|
var ok bool
|
|
|
|
if t, ok = param.(time.Time); !ok {
|
|
|
|
// Probably a string. Try to convert it to time.Time.
|
|
|
|
t = cast.ToTime(param)
|
|
|
|
}
|
2014-10-17 11:10:19 -04:00
|
|
|
}
|
2020-06-19 04:13:35 -04:00
|
|
|
|
|
|
|
dates[p] = t
|
|
|
|
r = append(r, p)
|
2014-10-17 11:10:19 -04:00
|
|
|
}
|
2020-06-19 04:13:35 -04:00
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
pdate := func(p1, p2 Page) bool {
|
2020-06-19 04:13:35 -04:00
|
|
|
return dates[p1].Unix() < dates[p2].Unix()
|
2014-10-17 11:10:19 -04:00
|
|
|
}
|
2016-03-24 22:12:03 -04:00
|
|
|
pageBy(pdate).Sort(r)
|
2014-10-17 11:10:19 -04:00
|
|
|
return r
|
|
|
|
}
|
2022-04-05 03:57:58 -04:00
|
|
|
getDate := func(p Page) time.Time {
|
|
|
|
return dates[p]
|
2014-10-17 11:10:19 -04:00
|
|
|
}
|
2022-04-05 03:57:58 -04:00
|
|
|
return p.groupByDateField(format, sorter, getDate, order...)
|
2014-10-17 11:10:19 -04:00
|
|
|
}
|
2019-01-02 06:33:26 -05:00
|
|
|
|
2020-12-16 06:11:32 -05:00
|
|
|
// ProbablyEq wraps compare.ProbablyEqer
|
2022-04-21 04:59:13 -04:00
|
|
|
// For internal use.
|
2022-03-17 17:03:27 -04:00
|
|
|
func (p PageGroup) ProbablyEq(other any) bool {
|
2019-04-02 04:52:43 -04:00
|
|
|
otherP, ok := other.(PageGroup)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Key != otherP.Key {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return p.Pages.ProbablyEq(otherP.Pages)
|
|
|
|
}
|
|
|
|
|
2022-04-21 04:59:13 -04:00
|
|
|
// Slice is for internal use.
|
2019-01-02 06:33:26 -05:00
|
|
|
// for the template functions. See collections.Slice.
|
2022-03-17 17:03:27 -04:00
|
|
|
func (p PageGroup) Slice(in any) (any, error) {
|
2019-01-02 06:33:26 -05:00
|
|
|
switch items := in.(type) {
|
|
|
|
case PageGroup:
|
|
|
|
return items, nil
|
2022-03-17 17:03:27 -04:00
|
|
|
case []any:
|
2019-01-02 06:33:26 -05:00
|
|
|
groups := make(PagesGroup, len(items))
|
|
|
|
for i, v := range items {
|
|
|
|
g, ok := v.(PageGroup)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("type %T is not a PageGroup", v)
|
|
|
|
}
|
|
|
|
groups[i] = g
|
|
|
|
}
|
|
|
|
return groups, nil
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("invalid slice type %T", items)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Len returns the number of pages in the page group.
|
|
|
|
func (psg PagesGroup) Len() int {
|
|
|
|
l := 0
|
|
|
|
for _, pg := range psg {
|
|
|
|
l += len(pg.Pages)
|
|
|
|
}
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2020-12-16 06:11:32 -05:00
|
|
|
// ProbablyEq wraps compare.ProbablyEqer
|
2022-03-17 17:03:27 -04:00
|
|
|
func (psg PagesGroup) ProbablyEq(other any) bool {
|
2019-04-02 04:52:43 -04:00
|
|
|
otherPsg, ok := other.(PagesGroup)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(psg) != len(otherPsg) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range psg {
|
|
|
|
if !psg[i].ProbablyEq(otherPsg[i]) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
// ToPagesGroup tries to convert seq into a PagesGroup.
|
2022-03-17 17:03:27 -04:00
|
|
|
func ToPagesGroup(seq any) (PagesGroup, error) {
|
2019-01-02 06:33:26 -05:00
|
|
|
switch v := seq.(type) {
|
|
|
|
case nil:
|
|
|
|
return nil, nil
|
|
|
|
case PagesGroup:
|
|
|
|
return v, nil
|
|
|
|
case []PageGroup:
|
|
|
|
return PagesGroup(v), nil
|
2022-03-17 17:03:27 -04:00
|
|
|
case []any:
|
2019-01-02 06:33:26 -05:00
|
|
|
l := len(v)
|
|
|
|
if l == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
switch v[0].(type) {
|
|
|
|
case PageGroup:
|
|
|
|
pagesGroup := make(PagesGroup, l)
|
|
|
|
for i, ipg := range v {
|
|
|
|
if pg, ok := ipg.(PageGroup); ok {
|
|
|
|
pagesGroup[i] = pg
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("unsupported type in paginate from slice, got %T instead of PageGroup", ipg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pagesGroup, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|