summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVincent Danjean <vdanjean@users.noreply.github.com>2018-09-08 11:14:09 +0200
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2018-09-08 11:14:09 +0200
commitcfda13b36367465016f4458ab9924c948ed02b6f (patch)
tree0a7a99e77fecba344f18871de1a2ba35e9d5776b
parentbb2fe814c2db0c494b3b678a5da20a6cc0538857 (diff)
hugolib: Allow creating page groups from any page collection
This also adjusts the pagination logic to allow for these new collections. Note that we will follow up with a template function named `group` that will be the end user API. The `.Group` method on `Page` should be considered as internal. Updates #4865
-rw-r--r--hugolib/pageGroup.go7
-rw-r--r--hugolib/pagination.go38
2 files changed, 44 insertions, 1 deletions
diff --git a/hugolib/pageGroup.go b/hugolib/pageGroup.go
index 8aaa1018c..f12eff253 100644
--- a/hugolib/pageGroup.go
+++ b/hugolib/pageGroup.go
@@ -296,3 +296,10 @@ func (p Pages) GroupByParamDate(key string, format string, order ...string) (Pag
}
return p.groupByDateField(sorter, formatter, order...)
}
+
+// Group creates a PageGroup from a key and a Pages object
+func (p *Page) Group(key interface{}, pages Pages) (PageGroup, error) {
+ pageGroup := PageGroup{Key: key, Pages: pages}
+
+ return pageGroup, nil
+}
diff --git a/hugolib/pagination.go b/hugolib/pagination.go
index 58cec576b..9a9b10c48 100644
--- a/hugolib/pagination.go
+++ b/hugolib/pagination.go
@@ -399,7 +399,11 @@ func paginatePages(td targetPathDescriptor, seq interface{}, pagerSize int) (pag
var paginator *paginator
- if groups, ok := seq.(PagesGroup); ok {
+ groups, err := toPagesGroup(seq)
+ if err != nil {
+ return nil, err
+ }
+ if groups != nil {
paginator, _ = newPaginatorFromPageGroups(groups, pagerSize, urlFactory)
} else {
pages, err := toPages(seq)
@@ -414,6 +418,36 @@ func paginatePages(td targetPathDescriptor, seq interface{}, pagerSize int) (pag
return pagers, nil
}
+func toPagesGroup(seq interface{}) (PagesGroup, error) {
+ switch v := seq.(type) {
+ case nil:
+ return nil, nil
+ case PagesGroup:
+ return v, nil
+ case []PageGroup:
+ return PagesGroup(v), nil
+ case []interface{}:
+ 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(pagesGroup), nil
+ }
+ }
+
+ return nil, nil
+}
+
func toPages(seq interface{}) (Pages, error) {
if seq == nil {
return Pages{}, nil
@@ -424,6 +458,8 @@ func toPages(seq interface{}) (Pages, error) {
return seq.(Pages), nil
case *Pages:
return *(seq.(*Pages)), nil
+ case []*Page:
+ return Pages(seq.([]*Page)), nil
case WeightedPages:
return (seq.(WeightedPages)).Pages(), nil
case PageGroup: