| 1 |
// Copyright 2019 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package page |
| 15 |
|
| 16 |
import ( |
| 17 |
"fmt" |
| 18 |
"math/rand" |
| 19 |
|
| 20 |
"github.com/gohugoio/hugo/compare" |
| 21 |
|
| 22 |
"github.com/gohugoio/hugo/resources/resource" |
| 23 |
) |
| 24 |
|
| 25 |
// Pages is a slice of Page objects. This is the most common list type in Hugo. |
| 26 |
type Pages []Page |
| 27 |
|
| 28 |
// String returns a string representation of the list. |
| 29 |
// For internal use. |
| 30 |
func (ps Pages) String() string { |
| 31 |
return fmt.Sprintf("Pages(%d)", len(ps)) |
| 32 |
} |
| 33 |
|
| 34 |
// Used in tests. |
| 35 |
func (ps Pages) shuffle() { |
| 36 |
for i := range ps { |
| 37 |
j := rand.Intn(i + 1) |
| 38 |
ps[i], ps[j] = ps[j], ps[i] |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
// ToResources wraps resource.ResourcesConverter. |
| 43 |
// For internal use. |
| 44 |
func (pages Pages) ToResources() resource.Resources { |
| 45 |
r := make(resource.Resources, len(pages)) |
| 46 |
for i, p := range pages { |
| 47 |
r[i] = p |
| 48 |
} |
| 49 |
return r |
| 50 |
} |
| 51 |
|
| 52 |
// ToPages tries to convert seq into Pages. |
| 53 |
func ToPages(seq any) (Pages, error) { |
| 54 |
if seq == nil { |
| 55 |
return Pages{}, nil |
| 56 |
} |
| 57 |
|
| 58 |
switch v := seq.(type) { |
| 59 |
case Pages: |
| 60 |
return v, nil |
| 61 |
case *Pages: |
| 62 |
return *(v), nil |
| 63 |
case WeightedPages: |
| 64 |
return v.Pages(), nil |
| 65 |
case PageGroup: |
| 66 |
return v.Pages, nil |
| 67 |
case []Page: |
| 68 |
pages := make(Pages, len(v)) |
| 69 |
copy(pages, v) |
| 70 |
return pages, nil |
| 71 |
case []any: |
| 72 |
pages := make(Pages, len(v)) |
| 73 |
success := true |
| 74 |
for i, vv := range v { |
| 75 |
p, ok := vv.(Page) |
| 76 |
if !ok { |
| 77 |
success = false |
| 78 |
break |
| 79 |
} |
| 80 |
pages[i] = p |
| 81 |
} |
| 82 |
if success { |
| 83 |
return pages, nil |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
return nil, fmt.Errorf("cannot convert type %T to Pages", seq) |
| 88 |
} |
| 89 |
|
| 90 |
// Group groups the pages in in by key. |
| 91 |
// This implements collections.Grouper. |
| 92 |
func (p Pages) Group(key any, in any) (any, error) { |
| 93 |
pages, err := ToPages(in) |
| 94 |
if err != nil { |
| 95 |
return PageGroup{}, err |
| 96 |
} |
| 97 |
return PageGroup{Key: key, Pages: pages}, nil |
| 98 |
} |
| 99 |
|
| 100 |
// Len returns the number of pages in the list. |
| 101 |
func (p Pages) Len() int { |
| 102 |
return len(p) |
| 103 |
} |
| 104 |
|
| 105 |
// ProbablyEq wraps compare.ProbablyEqer |
| 106 |
// For internal use. |
| 107 |
func (pages Pages) ProbablyEq(other any) bool { |
| 108 |
otherPages, ok := other.(Pages) |
| 109 |
if !ok { |
| 110 |
return false |
| 111 |
} |
| 112 |
|
| 113 |
if len(pages) != len(otherPages) { |
| 114 |
return false |
| 115 |
} |
| 116 |
|
| 117 |
step := 1 |
| 118 |
|
| 119 |
for i := 0; i < len(pages); i += step { |
| 120 |
if !pages[i].Eq(otherPages[i]) { |
| 121 |
return false |
| 122 |
} |
| 123 |
|
| 124 |
if i > 50 { |
| 125 |
// This is most likely the same. |
| 126 |
step = 50 |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
return true |
| 131 |
} |
| 132 |
|
| 133 |
// IndexOf returns the index of page in p, or -1 if not found. |
| 134 |
func (p Pages) IndexOf(page Page) int { |
| 135 |
return searchPage(page, p) |
| 136 |
} |
| 137 |
|
| 138 |
// PagesFactory somehow creates some Pages. |
| 139 |
// We do a lot of lazy Pages initialization in Hugo, so we need a type. |
| 140 |
type PagesFactory func() Pages |
| 141 |
|
| 142 |
var ( |
| 143 |
_ resource.ResourcesConverter = Pages{} |
| 144 |
_ compare.ProbablyEqer = Pages{} |
| 145 |
) |