aboutsummaryrefslogtreecommitdiffhomepage
path: root/resources/page/pages_sort.go
blob: 3f487570246729f14cebeee8b5a9fa35adeb2796 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
// Copyright 2019 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// 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.

package page

import (
	"context"
	"sort"

	"github.com/gohugoio/hugo/common/collections"
	"github.com/gohugoio/hugo/langs"

	"github.com/gohugoio/hugo/resources/resource"

	"github.com/gohugoio/hugo/compare"
	"github.com/spf13/cast"
)

var spc = newPageCache()

/*
 * Implementation of a custom sorter for Pages
 */

// A pageSorter implements the sort interface for Pages
type pageSorter struct {
	pages Pages
	by    pageBy
}

// pageBy is a closure used in the Sort.Less method.
type pageBy func(p1, p2 Page) bool

func getOrdinals(p1, p2 Page) (int, int) {
	p1o, ok1 := p1.(collections.Order)
	if !ok1 {
		return -1, -1
	}
	p2o, ok2 := p2.(collections.Order)
	if !ok2 {
		return -1, -1
	}

	return p1o.Ordinal(), p2o.Ordinal()
}

func getWeight0s(p1, p2 Page) (int, int) {
	p1w, ok1 := p1.(resource.Weight0Provider)
	if !ok1 {
		return -1, -1
	}
	p2w, ok2 := p2.(resource.Weight0Provider)
	if !ok2 {
		return -1, -1
	}

	return p1w.Weight0(), p2w.Weight0()
}

// Sort stable sorts the pages given the receiver's sort order.
func (by pageBy) Sort(pages Pages) {
	ps := &pageSorter{
		pages: pages,
		by:    by, // The Sort method's receiver is the function (closure) that defines the sort order.
	}
	sort.Stable(ps)
}

var (

	// DefaultPageSort is the default sort func for pages in Hugo:
	// Order by Ordinal, Weight, Date, LinkTitle and then full file path.
	DefaultPageSort = func(p1, p2 Page) bool {
		o1, o2 := getOrdinals(p1, p2)
		if o1 != o2 && o1 != -1 && o2 != -1 {
			return o1 < o2
		}
		// Weight0, as by the weight of the taxonomy entrie in the front matter.
		w01, w02 := getWeight0s(p1, p2)
		if w01 != w02 && w01 != -1 && w02 != -1 {
			return w01 < w02
		}
		if p1.Weight() == p2.Weight() {
			if p1.Date().Unix() == p2.Date().Unix() {
				c := collatorStringCompare(func(p Page) string { return p.LinkTitle() }, p1, p2)
				if c == 0 {
					if p1.File() == nil || p2.File() == nil {
						return p1.File() == nil
					}
					return compare.LessStrings(p1.File().Filename(), p2.File().Filename())
				}
				return c < 0
			}
			return p1.Date().Unix() > p2.Date().Unix()
		}

		if p2.Weight() == 0 {
			return true
		}

		if p1.Weight() == 0 {
			return false
		}

		return p1.Weight() < p2.Weight()
	}

	lessPageLanguage = func(p1, p2 Page) bool {
		if p1.Language().Weight == p2.Language().Weight {
			if p1.Date().Unix() == p2.Date().Unix() {
				c := compare.Strings(p1.LinkTitle(), p2.LinkTitle())
				if c == 0 {
					if p1.File() != nil && p2.File() != nil {
						return compare.LessStrings(p1.File().Filename(), p2.File().Filename())
					}
				}
				return c < 0
			}
			return p1.Date().Unix() > p2.Date().Unix()
		}

		if p2.Language().Weight == 0 {
			return true
		}

		if p1.Language().Weight == 0 {
			return false
		}

		return p1.Language().Weight < p2.Language().Weight
	}

	lessPageTitle = func(p1, p2 Page) bool {
		return collatorStringCompare(func(p Page) string { return p.Title() }, p1, p2) < 0
	}

	lessPageLinkTitle = func(p1, p2 Page) bool {
		return collatorStringCompare(func(p Page) string { return p.LinkTitle() }, p1, p2) < 0
	}

	lessPageDate = func(p1, p2 Page) bool {
		return p1.Date().Unix() < p2.Date().Unix()
	}

	lessPagePubDate = func(p1, p2 Page) bool {
		return p1.PublishDate().Unix() < p2.PublishDate().Unix()
	}
)

func (ps *pageSorter) Len() int      { return len(ps.pages) }
func (ps *pageSorter) Swap(i, j int) { ps.pages[i], ps.pages[j] = ps.pages[j], ps.pages[i] }

// Less is part of sort.Interface. It is implemented by calling the "by" closure in the sorter.
func (ps *pageSorter) Less(i, j int) bool { return ps.by(ps.pages[i], ps.pages[j]) }

// Limit limits the number of pages returned to n.
func (p Pages) Limit(n int) Pages {
	if len(p) > n {
		return p[0:n]
	}
	return p
}

var collatorStringSort = func(getString func(Page) string) func(p Pages) {
	return func(p Pages) {
		if len(p) == 0 {
			return
		}
		// Pages may be a mix of multiple languages, so we need to use the language
		// for the currently rendered Site.
		currentSite := p[0].Site().Current()
		coll := langs.GetCollator1(currentSite.Language())
		coll.Lock()
		defer coll.Unlock()

		sort.SliceStable(p, func(i, j int) bool {
			return coll.CompareStrings(getString(p[i]), getString(p[j])) < 0
		})
	}
}

var collatorStringCompare = func(getString func(Page) string, p1, p2 Page) int {
	currentSite := p1.Site().Current()
	coll := langs.GetCollator1(currentSite.Language())
	coll.Lock()
	c := coll.CompareStrings(getString(p1), getString(p2))
	coll.Unlock()
	return c
}

var collatorStringLess = func(p Page) (less func(s1, s2 string) bool, close func()) {
	currentSite := p.Site().Current()
	// Make sure to use the second collator to prevent deadlocks.
	// See issue 11039.
	coll := langs.GetCollator2(currentSite.Language())
	coll.Lock()
	return func(s1, s2 string) bool {
			return coll.CompareStrings(s1, s2) < 1
		},
		func() {
			coll.Unlock()
		}
}

// ByWeight sorts the Pages by weight and returns a copy.
//
// Adjacent invocations on the same receiver will return a cached result.
//
// This may safely be executed  in parallel.
func (p Pages) ByWeight() Pages {
	const key = "pageSort.ByWeight"
	pages, _ := spc.get(key, pageBy(DefaultPageSort).Sort, p)
	return pages
}

// SortByDefault sorts pages by the default sort.
func SortByDefault(pages Pages) {
	pageBy(DefaultPageSort).Sort(pages)
}

// ByTitle sorts the Pages by title and returns a copy.
//
// Adjacent invocations on the same receiver will return a cached result.
//
// This may safely be executed  in parallel.
func (p Pages) ByTitle() Pages {
	const key = "pageSort.ByTitle"

	pages, _ := spc.get(key, collatorStringSort(func(p Page) string { return p.Title() }), p)

	return pages
}

// ByLinkTitle sorts the Pages by link title and returns a copy.
//
// Adjacent invocations on the same receiver will return a cached result.
//
// This may safely be executed  in parallel.
func (p Pages) ByLinkTitle() Pages {
	const key = "pageSort.ByLinkTitle"

	pages, _ := spc.get(key, collatorStringSort(func(p Page) string { return p.LinkTitle() }), p)

	return pages
}

// ByDate sorts the Pages by date and returns a copy.
//
// Adjacent invocations on the same receiver will return a cached result.
//
// This may safely be executed  in parallel.
func (p Pages) ByDate() Pages {
	const key = "pageSort.ByDate"

	pages, _ := spc.get(key, pageBy(lessPageDate).Sort, p)

	return pages
}

// ByPublishDate sorts the Pages by publish date and returns a copy.
//
// Adjacent invocations on the same receiver will return a cached result.
//
// This may safely be executed  in parallel.
func (p Pages) ByPublishDate() Pages {
	const key = "pageSort.ByPublishDate"

	pages, _ := spc.get(key, pageBy(lessPagePubDate).Sort, p)

	return pages
}

// ByExpiryDate sorts the Pages by publish date and returns a copy.
//
// Adjacent invocations on the same receiver will return a cached result.
//
// This may safely be executed  in parallel.
func (p Pages) ByExpiryDate() Pages {
	const key = "pageSort.ByExpiryDate"

	expDate := func(p1, p2 Page) bool {
		return p1.ExpiryDate().Unix() < p2.ExpiryDate().Unix()
	}

	pages, _ := spc.get(key, pageBy(expDate).Sort, p)

	return pages
}

// ByLastmod sorts the Pages by the last modification date and returns a copy.
//
// Adjacent invocations on the same receiver will return a cached result.
//
// This may safely be executed  in parallel.
func (p Pages) ByLastmod() Pages {
	const key = "pageSort.ByLastmod"

	date := func(p1, p2 Page) bool {
		return p1.Lastmod().Unix() < p2.Lastmod().Unix()
	}

	pages, _ := spc.get(key, pageBy(date).Sort, p)

	return pages
}

// ByLength sorts the Pages by length and returns a copy.
//
// Adjacent invocations on the same receiver will return a cached result.
//
// This may safely be executed  in parallel.
func (p Pages) ByLength(ctx context.Context) Pages {
	const key = "pageSort.ByLength"

	length := func(p1, p2 Page) bool {
		p1l, ok1 := p1.(resource.LengthProvider)
		p2l, ok2 := p2.(resource.LengthProvider)

		if !ok1 {
			return true
		}

		if !ok2 {
			return false
		}

		return p1l.Len(ctx) < p2l.Len(ctx)
	}

	pages, _ := spc.get(key, pageBy(length).Sort, p)

	return pages
}

// ByLanguage sorts the Pages by the language's Weight.
//
// Adjacent invocations on the same receiver will return a cached result.
//
// This may safely be executed  in parallel.
func (p Pages) ByLanguage() Pages {
	const key = "pageSort.ByLanguage"

	pages, _ := spc.get(key, pageBy(lessPageLanguage).Sort, p)

	return pages
}

// SortByLanguage sorts the pages by language.
func SortByLanguage(pages Pages) {
	pageBy(lessPageLanguage).Sort(pages)
}

// Reverse reverses the order in Pages and returns a copy.
//
// Adjacent invocations on the same receiver will return a cached result.
//
// This may safely be executed  in parallel.
func (p Pages) Reverse() Pages {
	const key = "pageSort.Reverse"

	reverseFunc := func(pages Pages) {
		for i, j := 0, len(pages)-1; i < j; i, j = i+1, j-1 {
			pages[i], pages[j] = pages[j], pages[i]
		}
	}

	pages, _ := spc.get(key, reverseFunc, p)

	return pages
}

// ByParam sorts the pages according to the given page Params key.
//
// Adjacent invocations on the same receiver with the same paramsKey will return a cached result.
//
// This may safely be executed  in parallel.
func (p Pages) ByParam(paramsKey any) Pages {
	if len(p) < 2 {
		return p
	}
	paramsKeyStr := cast.ToString(paramsKey)
	key := "pageSort.ByParam." + paramsKeyStr

	stringLess, close := collatorStringLess(p[0])
	defer close()

	paramsKeyComparator := func(p1, p2 Page) bool {
		v1, _ := p1.Param(paramsKeyStr)
		v2, _ := p2.Param(paramsKeyStr)

		if v1 == nil {
			return false
		}

		if v2 == nil {
			return true
		}

		isNumeric := func(v any) bool {
			switch v.(type) {
			case uint8, uint16, uint32, uint64, int, int8, int16, int32, int64, float32, float64:
				return true
			default:
				return false
			}
		}

		if isNumeric(v1) && isNumeric(v2) {
			return cast.ToFloat64(v1) < cast.ToFloat64(v2)
		}

		s1 := cast.ToString(v1)
		s2 := cast.ToString(v2)

		return stringLess(s1, s2)
	}

	pages, _ := spc.get(key, pageBy(paramsKeyComparator).Sort, p)

	return pages
}