| 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 |
"context" |
| 18 |
"sort" |
| 19 |
|
| 20 |
"github.com/gohugoio/hugo/common/collections" |
| 21 |
"github.com/gohugoio/hugo/common/types" |
| 22 |
"github.com/gohugoio/hugo/langs" |
| 23 |
|
| 24 |
"github.com/gohugoio/hugo/resources/resource" |
| 25 |
|
| 26 |
"github.com/gohugoio/hugo/compare" |
| 27 |
"github.com/spf13/cast" |
| 28 |
) |
| 29 |
|
| 30 |
var spc = newPageCache() |
| 31 |
|
| 32 |
/* |
| 33 |
* Implementation of a custom sorter for Pages |
| 34 |
*/ |
| 35 |
|
| 36 |
// A pageSorter implements the sort interface for Pages |
| 37 |
type pageSorter struct { |
| 38 |
pages Pages |
| 39 |
by pageBy |
| 40 |
} |
| 41 |
|
| 42 |
// pageBy is a closure used in the Sort.Less method. |
| 43 |
type pageBy func(p1, p2 Page) bool |
| 44 |
|
| 45 |
func getOrdinals(p1, p2 Page) (int, int) { |
| 46 |
p1o, ok1 := p1.(collections.Order) |
| 47 |
if !ok1 { |
| 48 |
return -1, -1 |
| 49 |
} |
| 50 |
p2o, ok2 := p2.(collections.Order) |
| 51 |
if !ok2 { |
| 52 |
return -1, -1 |
| 53 |
} |
| 54 |
|
| 55 |
return p1o.Ordinal(), p2o.Ordinal() |
| 56 |
} |
| 57 |
|
| 58 |
func getWeight0s(p1, p2 Page) (w1 int, w2 int) { |
| 59 |
p1w, ok1 := p1.(types.Weight0Provider) |
| 60 |
if !ok1 { |
| 61 |
return |
| 62 |
} |
| 63 |
p2w, ok2 := p2.(types.Weight0Provider) |
| 64 |
if !ok2 { |
| 65 |
return |
| 66 |
} |
| 67 |
|
| 68 |
return p1w.Weight0(), p2w.Weight0() |
| 69 |
} |
| 70 |
|
| 71 |
// Sort stable sorts the pages given the receiver's sort order. |
| 72 |
func (by pageBy) Sort(pages Pages) { |
| 73 |
ps := &pageSorter{ |
| 74 |
pages: pages, |
| 75 |
by: by, // The Sort method's receiver is the function (closure) that defines the sort order. |
| 76 |
} |
| 77 |
sort.Stable(ps) |
| 78 |
} |
| 79 |
|
| 80 |
var ( |
| 81 |
|
| 82 |
// DefaultPageSort is the default sort func for pages in Hugo: |
| 83 |
// Order by Ordinal, Weight, Date, LinkTitle and then full file path. |
| 84 |
DefaultPageSort = func(p1, p2 Page) bool { |
| 85 |
o1, o2 := getOrdinals(p1, p2) |
| 86 |
if o1 != o2 && o1 != -1 && o2 != -1 { |
| 87 |
return o1 < o2 |
| 88 |
} |
| 89 |
// Weight0, as by the weight of the taxonomy entrie in the front matter. |
| 90 |
w01, w02 := getWeight0s(p1, p2) |
| 91 |
if w01 != w02 && w01 != -1 && w02 != -1 { |
| 92 |
return w01 < w02 |
| 93 |
} |
| 94 |
|
| 95 |
if p1.Weight() == p2.Weight() { |
| 96 |
if p1.Date().Unix() == p2.Date().Unix() { |
| 97 |
c := collatorStringCompare(func(p Page) string { return p.LinkTitle() }, p1, p2) |
| 98 |
if c == 0 { |
| 99 |
// This is the full normalized path, which will contain extension and any language code preserved, |
| 100 |
// which is what we want for sorting. |
| 101 |
return compare.LessStrings(p1.PathInfo().Path(), p2.PathInfo().Path()) |
| 102 |
} |
| 103 |
return c < 0 |
| 104 |
} |
| 105 |
return p1.Date().Unix() > p2.Date().Unix() |
| 106 |
} |
| 107 |
|
| 108 |
if p2.Weight() == 0 { |
| 109 |
return true |
| 110 |
} |
| 111 |
|
| 112 |
if p1.Weight() == 0 { |
| 113 |
return false |
| 114 |
} |
| 115 |
|
| 116 |
return p1.Weight() < p2.Weight() |
| 117 |
} |
| 118 |
|
| 119 |
lessPageLanguage = func(p1, p2 Page) bool { |
| 120 |
if p1.Language().Weight == p2.Language().Weight { |
| 121 |
if p1.Date().Unix() == p2.Date().Unix() { |
| 122 |
c := compare.Strings(p1.LinkTitle(), p2.LinkTitle()) |
| 123 |
if c == 0 { |
| 124 |
if p1.File() != nil && p2.File() != nil { |
| 125 |
return compare.LessStrings(p1.File().Filename(), p2.File().Filename()) |
| 126 |
} |
| 127 |
} |
| 128 |
return c < 0 |
| 129 |
} |
| 130 |
return p1.Date().Unix() > p2.Date().Unix() |
| 131 |
} |
| 132 |
|
| 133 |
if p2.Language().Weight == 0 { |
| 134 |
return true |
| 135 |
} |
| 136 |
|
| 137 |
if p1.Language().Weight == 0 { |
| 138 |
return false |
| 139 |
} |
| 140 |
|
| 141 |
return p1.Language().Weight < p2.Language().Weight |
| 142 |
} |
| 143 |
|
| 144 |
lessPageTitle = func(p1, p2 Page) bool { |
| 145 |
return collatorStringCompare(func(p Page) string { return p.Title() }, p1, p2) < 0 |
| 146 |
} |
| 147 |
|
| 148 |
lessPageLinkTitle = func(p1, p2 Page) bool { |
| 149 |
return collatorStringCompare(func(p Page) string { return p.LinkTitle() }, p1, p2) < 0 |
| 150 |
} |
| 151 |
|
| 152 |
lessPageDate = func(p1, p2 Page) bool { |
| 153 |
return p1.Date().Unix() < p2.Date().Unix() |
| 154 |
} |
| 155 |
|
| 156 |
lessPagePubDate = func(p1, p2 Page) bool { |
| 157 |
return p1.PublishDate().Unix() < p2.PublishDate().Unix() |
| 158 |
} |
| 159 |
|
| 160 |
lessPageDims = func(p1, p2 Page) bool { |
| 161 |
d1, d2 := GetSiteVector(p1), GetSiteVector(p2) |
| 162 |
if n := d1.Compare(d2); n != 0 { |
| 163 |
return n < 0 |
| 164 |
} |
| 165 |
|
| 166 |
// Fall back to the default sort order. |
| 167 |
return DefaultPageSort(p1, p2) |
| 168 |
} |
| 169 |
) |
| 170 |
|
| 171 |
func (ps *pageSorter) Len() int { return len(ps.pages) } |
| 172 |
func (ps *pageSorter) Swap(i, j int) { ps.pages[i], ps.pages[j] = ps.pages[j], ps.pages[i] } |
| 173 |
|
| 174 |
// Less is part of sort.Interface. It is implemented by calling the "by" closure in the sorter. |
| 175 |
func (ps *pageSorter) Less(i, j int) bool { return ps.by(ps.pages[i], ps.pages[j]) } |
| 176 |
|
| 177 |
// Limit limits the number of pages returned to n. |
| 178 |
func (p Pages) Limit(n int) Pages { |
| 179 |
if len(p) > n { |
| 180 |
return p[0:n] |
| 181 |
} |
| 182 |
return p |
| 183 |
} |
| 184 |
|
| 185 |
var collatorStringSort = func(getString func(Page) string) func(p Pages) { |
| 186 |
return func(p Pages) { |
| 187 |
if len(p) == 0 { |
| 188 |
return |
| 189 |
} |
| 190 |
// Pages may be a mix of multiple languages, so we need to use the language |
| 191 |
// for the currently rendered Site. |
| 192 |
currentSite := p[0].Site().Current() |
| 193 |
coll := langs.GetCollator1(currentSite.Language()) |
| 194 |
coll.Lock() |
| 195 |
defer coll.Unlock() |
| 196 |
|
| 197 |
sort.SliceStable(p, func(i, j int) bool { |
| 198 |
return coll.CompareStrings(getString(p[i]), getString(p[j])) < 0 |
| 199 |
}) |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
var collatorStringCompare = func(getString func(Page) string, p1, p2 Page) int { |
| 204 |
currentSite := p1.Site().Current() |
| 205 |
coll := langs.GetCollator1(currentSite.Language()) |
| 206 |
coll.Lock() |
| 207 |
c := coll.CompareStrings(getString(p1), getString(p2)) |
| 208 |
coll.Unlock() |
| 209 |
return c |
| 210 |
} |
| 211 |
|
| 212 |
var collatorStringLess = func(p Page) (less func(s1, s2 string) bool, close func()) { |
| 213 |
currentSite := p.Site().Current() |
| 214 |
// Make sure to use the second collator to prevent deadlocks. |
| 215 |
// See issue 11039. |
| 216 |
coll := langs.GetCollator2(currentSite.Language()) |
| 217 |
coll.Lock() |
| 218 |
return func(s1, s2 string) bool { |
| 219 |
return coll.CompareStrings(s1, s2) < 1 |
| 220 |
}, |
| 221 |
func() { |
| 222 |
coll.Unlock() |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
// ByWeight sorts the Pages by weight and returns a copy. |
| 227 |
// |
| 228 |
// Adjacent invocations on the same receiver will return a cached result. |
| 229 |
// |
| 230 |
// This may safely be executed in parallel. |
| 231 |
func (p Pages) ByWeight() Pages { |
| 232 |
const key = "pageSort.ByWeight" |
| 233 |
pages, _ := spc.get(key, pageBy(DefaultPageSort).Sort, p) |
| 234 |
return pages |
| 235 |
} |
| 236 |
|
| 237 |
// SortByDefault sorts pages by the default sort. |
| 238 |
func SortByDefault(pages Pages) { |
| 239 |
pageBy(DefaultPageSort).Sort(pages) |
| 240 |
} |
| 241 |
|
| 242 |
// ByTitle sorts the Pages by title and returns a copy. |
| 243 |
// |
| 244 |
// Adjacent invocations on the same receiver will return a cached result. |
| 245 |
// |
| 246 |
// This may safely be executed in parallel. |
| 247 |
func (p Pages) ByTitle() Pages { |
| 248 |
const key = "pageSort.ByTitle" |
| 249 |
|
| 250 |
pages, _ := spc.get(key, collatorStringSort(func(p Page) string { return p.Title() }), p) |
| 251 |
|
| 252 |
return pages |
| 253 |
} |
| 254 |
|
| 255 |
// ByLinkTitle sorts the Pages by link title and returns a copy. |
| 256 |
// |
| 257 |
// Adjacent invocations on the same receiver will return a cached result. |
| 258 |
// |
| 259 |
// This may safely be executed in parallel. |
| 260 |
func (p Pages) ByLinkTitle() Pages { |
| 261 |
const key = "pageSort.ByLinkTitle" |
| 262 |
|
| 263 |
pages, _ := spc.get(key, collatorStringSort(func(p Page) string { return p.LinkTitle() }), p) |
| 264 |
|
| 265 |
return pages |
| 266 |
} |
| 267 |
|
| 268 |
// ByDate sorts the Pages by date and returns a copy. |
| 269 |
// |
| 270 |
// Adjacent invocations on the same receiver will return a cached result. |
| 271 |
// |
| 272 |
// This may safely be executed in parallel. |
| 273 |
func (p Pages) ByDate() Pages { |
| 274 |
const key = "pageSort.ByDate" |
| 275 |
|
| 276 |
pages, _ := spc.get(key, pageBy(lessPageDate).Sort, p) |
| 277 |
|
| 278 |
return pages |
| 279 |
} |
| 280 |
|
| 281 |
// ByPublishDate sorts the Pages by publish date and returns a copy. |
| 282 |
// |
| 283 |
// Adjacent invocations on the same receiver will return a cached result. |
| 284 |
// |
| 285 |
// This may safely be executed in parallel. |
| 286 |
func (p Pages) ByPublishDate() Pages { |
| 287 |
const key = "pageSort.ByPublishDate" |
| 288 |
|
| 289 |
pages, _ := spc.get(key, pageBy(lessPagePubDate).Sort, p) |
| 290 |
|
| 291 |
return pages |
| 292 |
} |
| 293 |
|
| 294 |
// ByExpiryDate sorts the Pages by publish date and returns a copy. |
| 295 |
// |
| 296 |
// Adjacent invocations on the same receiver will return a cached result. |
| 297 |
// |
| 298 |
// This may safely be executed in parallel. |
| 299 |
func (p Pages) ByExpiryDate() Pages { |
| 300 |
const key = "pageSort.ByExpiryDate" |
| 301 |
|
| 302 |
expDate := func(p1, p2 Page) bool { |
| 303 |
return p1.ExpiryDate().Unix() < p2.ExpiryDate().Unix() |
| 304 |
} |
| 305 |
|
| 306 |
pages, _ := spc.get(key, pageBy(expDate).Sort, p) |
| 307 |
|
| 308 |
return pages |
| 309 |
} |
| 310 |
|
| 311 |
// ByLastmod sorts the Pages by the last modification date and returns a copy. |
| 312 |
// |
| 313 |
// Adjacent invocations on the same receiver will return a cached result. |
| 314 |
// |
| 315 |
// This may safely be executed in parallel. |
| 316 |
func (p Pages) ByLastmod() Pages { |
| 317 |
const key = "pageSort.ByLastmod" |
| 318 |
|
| 319 |
date := func(p1, p2 Page) bool { |
| 320 |
return p1.Lastmod().Unix() < p2.Lastmod().Unix() |
| 321 |
} |
| 322 |
|
| 323 |
pages, _ := spc.get(key, pageBy(date).Sort, p) |
| 324 |
|
| 325 |
return pages |
| 326 |
} |
| 327 |
|
| 328 |
// ByLength sorts the Pages by length and returns a copy. |
| 329 |
// |
| 330 |
// Adjacent invocations on the same receiver will return a cached result. |
| 331 |
// |
| 332 |
// This may safely be executed in parallel. |
| 333 |
func (p Pages) ByLength(ctx context.Context) Pages { |
| 334 |
const key = "pageSort.ByLength" |
| 335 |
|
| 336 |
length := func(p1, p2 Page) bool { |
| 337 |
p1l, ok1 := p1.(resource.LengthProvider) |
| 338 |
p2l, ok2 := p2.(resource.LengthProvider) |
| 339 |
|
| 340 |
if !ok1 { |
| 341 |
return true |
| 342 |
} |
| 343 |
|
| 344 |
if !ok2 { |
| 345 |
return false |
| 346 |
} |
| 347 |
|
| 348 |
return p1l.Len(ctx) < p2l.Len(ctx) |
| 349 |
} |
| 350 |
|
| 351 |
pages, _ := spc.get(key, pageBy(length).Sort, p) |
| 352 |
|
| 353 |
return pages |
| 354 |
} |
| 355 |
|
| 356 |
// ByLanguage sorts the Pages by the language's Weight. |
| 357 |
// |
| 358 |
// Adjacent invocations on the same receiver will return a cached result. |
| 359 |
// |
| 360 |
// This may safely be executed in parallel. |
| 361 |
func (p Pages) ByLanguage() Pages { |
| 362 |
const key = "pageSort.ByLanguage" |
| 363 |
|
| 364 |
pages, _ := spc.get(key, pageBy(lessPageLanguage).Sort, p) |
| 365 |
|
| 366 |
return pages |
| 367 |
} |
| 368 |
|
| 369 |
// SortByLanguage sorts the pages by language. |
| 370 |
func SortByLanguage(pages Pages) { |
| 371 |
pageBy(lessPageLanguage).Sort(pages) |
| 372 |
} |
| 373 |
|
| 374 |
// SortByDims sorts the pages by sitesmatrix. |
| 375 |
func SortByDims(pages Pages) { |
| 376 |
pageBy(lessPageDims).Sort(pages) |
| 377 |
} |
| 378 |
|
| 379 |
// Reverse reverses the order in Pages and returns a copy. |
| 380 |
// |
| 381 |
// Adjacent invocations on the same receiver will return a cached result. |
| 382 |
// |
| 383 |
// This may safely be executed in parallel. |
| 384 |
func (p Pages) Reverse() Pages { |
| 385 |
const key = "pageSort.Reverse" |
| 386 |
|
| 387 |
reverseFunc := func(pages Pages) { |
| 388 |
for i, j := 0, len(pages)-1; i < j; i, j = i+1, j-1 { |
| 389 |
pages[i], pages[j] = pages[j], pages[i] |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
pages, _ := spc.get(key, reverseFunc, p) |
| 394 |
|
| 395 |
return pages |
| 396 |
} |
| 397 |
|
| 398 |
// ByParam sorts the pages according to the given page Params key. |
| 399 |
// |
| 400 |
// Adjacent invocations on the same receiver with the same paramsKey will return a cached result. |
| 401 |
// |
| 402 |
// This may safely be executed in parallel. |
| 403 |
func (p Pages) ByParam(paramsKey any) Pages { |
| 404 |
if len(p) < 2 { |
| 405 |
return p |
| 406 |
} |
| 407 |
paramsKeyStr := cast.ToString(paramsKey) |
| 408 |
key := "pageSort.ByParam." + paramsKeyStr |
| 409 |
|
| 410 |
stringLess, close := collatorStringLess(p[0]) |
| 411 |
defer close() |
| 412 |
|
| 413 |
paramsKeyComparator := func(p1, p2 Page) bool { |
| 414 |
v1, _ := p1.Param(paramsKeyStr) |
| 415 |
v2, _ := p2.Param(paramsKeyStr) |
| 416 |
|
| 417 |
if v1 == nil { |
| 418 |
return false |
| 419 |
} |
| 420 |
|
| 421 |
if v2 == nil { |
| 422 |
return true |
| 423 |
} |
| 424 |
|
| 425 |
isNumeric := func(v any) bool { |
| 426 |
switch v.(type) { |
| 427 |
case uint8, uint16, uint32, uint64, int, int8, int16, int32, int64, float32, float64: |
| 428 |
return true |
| 429 |
default: |
| 430 |
return false |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
if isNumeric(v1) && isNumeric(v2) { |
| 435 |
return cast.ToFloat64(v1) < cast.ToFloat64(v2) |
| 436 |
} |
| 437 |
|
| 438 |
s1 := cast.ToString(v1) |
| 439 |
s2 := cast.ToString(v2) |
| 440 |
|
| 441 |
return stringLess(s1, s2) |
| 442 |
} |
| 443 |
|
| 444 |
pages, _ := spc.get(key, pageBy(paramsKeyComparator).Sort, p) |
| 445 |
|
| 446 |
return pages |
| 447 |
} |