2015-12-07 13:57:01 -05:00
|
|
|
// Copyright 2015 The Hugo Authors. All rights reserved.
|
2014-01-27 17:16:28 -05:00
|
|
|
//
|
2015-11-23 22:16:36 -05:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
2014-01-27 17:16:28 -05: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-01-27 17:16:28 -05: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.
|
|
|
|
|
|
|
|
package hugolib
|
|
|
|
|
|
|
|
import (
|
2014-01-29 17:50:31 -05:00
|
|
|
"sort"
|
2014-01-27 17:16:28 -05:00
|
|
|
)
|
|
|
|
|
2015-07-20 19:29:22 -04:00
|
|
|
var spc = newPageCache()
|
|
|
|
|
2014-01-27 17:16:28 -05:00
|
|
|
/*
|
|
|
|
* Implementation of a custom sorter for Pages
|
|
|
|
*/
|
|
|
|
|
2015-03-07 06:53:20 -05:00
|
|
|
// A PageSorter implements the sort interface for Pages
|
2014-01-27 17:16:28 -05:00
|
|
|
type PageSorter struct {
|
2014-01-29 17:50:31 -05:00
|
|
|
pages Pages
|
|
|
|
by PageBy
|
2014-01-27 17:16:28 -05:00
|
|
|
}
|
|
|
|
|
2015-03-07 06:53:20 -05:00
|
|
|
// PageBy is a closure used in the Sort.Less method.
|
2014-01-27 17:16:28 -05:00
|
|
|
type PageBy func(p1, p2 *Page) bool
|
|
|
|
|
|
|
|
func (by PageBy) Sort(pages Pages) {
|
2014-01-29 17:50:31 -05:00
|
|
|
ps := &PageSorter{
|
|
|
|
pages: pages,
|
|
|
|
by: by, // The Sort method's receiver is the function (closure) that defines the sort order.
|
|
|
|
}
|
2014-09-09 05:59:47 -04:00
|
|
|
sort.Stable(ps)
|
2014-01-27 17:16:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
var DefaultPageSort = func(p1, p2 *Page) bool {
|
2014-01-29 17:50:31 -05:00
|
|
|
if p1.Weight == p2.Weight {
|
2015-07-25 10:34:35 -04:00
|
|
|
if p1.Date.Unix() == p2.Date.Unix() {
|
2015-09-29 23:43:11 -04:00
|
|
|
if p1.LinkTitle() == p2.LinkTitle() {
|
|
|
|
return (p1.FullFilePath() < p2.FullFilePath())
|
|
|
|
}
|
2015-07-25 11:38:13 -04:00
|
|
|
return (p1.LinkTitle() < p2.LinkTitle())
|
2015-07-25 10:34:35 -04:00
|
|
|
}
|
2014-01-29 17:50:31 -05:00
|
|
|
return p1.Date.Unix() > p2.Date.Unix()
|
|
|
|
}
|
2015-03-07 06:53:20 -05:00
|
|
|
return p1.Weight < p2.Weight
|
2014-01-27 17:16:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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]) }
|
|
|
|
|
|
|
|
func (p Pages) Sort() {
|
2014-01-29 17:50:31 -05:00
|
|
|
PageBy(DefaultPageSort).Sort(p)
|
2014-01-27 17:16:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p Pages) Limit(n int) Pages {
|
2014-01-29 17:50:31 -05:00
|
|
|
if len(p) < n {
|
|
|
|
return p[0:n]
|
|
|
|
}
|
2015-03-07 06:53:20 -05:00
|
|
|
return p
|
2014-01-27 17:16:28 -05:00
|
|
|
}
|
|
|
|
|
2015-07-20 19:29:22 -04:00
|
|
|
// ByWeight sorts the Pages by weight and returns a copy.
|
|
|
|
//
|
|
|
|
// Adjacent invocactions on the same receiver will return a cached result.
|
|
|
|
//
|
|
|
|
// This may safely be executed in parallel.
|
2014-01-27 17:16:28 -05:00
|
|
|
func (p Pages) ByWeight() Pages {
|
2015-07-20 19:29:22 -04:00
|
|
|
key := "pageSort.ByWeight"
|
|
|
|
pages, _ := spc.get(key, p, PageBy(DefaultPageSort).Sort)
|
|
|
|
return pages
|
2014-01-27 17:16:28 -05:00
|
|
|
}
|
|
|
|
|
2015-07-20 19:29:22 -04:00
|
|
|
// ByTitle sorts the Pages by title and returns a copy.
|
|
|
|
//
|
|
|
|
// Adjacent invocactions on the same receiver will return a cached result.
|
|
|
|
//
|
|
|
|
// This may safely be executed in parallel.
|
2014-03-05 01:29:57 -05:00
|
|
|
func (p Pages) ByTitle() Pages {
|
2015-07-20 19:29:22 -04:00
|
|
|
|
|
|
|
key := "pageSort.ByTitle"
|
|
|
|
|
2014-03-05 01:29:57 -05:00
|
|
|
title := func(p1, p2 *Page) bool {
|
|
|
|
return p1.Title < p2.Title
|
|
|
|
}
|
|
|
|
|
2015-07-20 19:29:22 -04:00
|
|
|
pages, _ := spc.get(key, p, PageBy(title).Sort)
|
|
|
|
return pages
|
2014-03-05 01:29:57 -05:00
|
|
|
}
|
|
|
|
|
2015-07-20 19:29:22 -04:00
|
|
|
// ByLinkTitle sorts the Pages by link title and returns a copy.
|
|
|
|
//
|
|
|
|
// Adjacent invocactions on the same receiver will return a cached result.
|
|
|
|
//
|
|
|
|
// This may safely be executed in parallel.
|
2014-03-05 01:29:57 -05:00
|
|
|
func (p Pages) ByLinkTitle() Pages {
|
2015-07-20 19:29:22 -04:00
|
|
|
|
|
|
|
key := "pageSort.ByLinkTitle"
|
|
|
|
|
2014-03-05 01:29:57 -05:00
|
|
|
linkTitle := func(p1, p2 *Page) bool {
|
|
|
|
return p1.linkTitle < p2.linkTitle
|
|
|
|
}
|
|
|
|
|
2015-07-20 19:29:22 -04:00
|
|
|
pages, _ := spc.get(key, p, PageBy(linkTitle).Sort)
|
|
|
|
|
|
|
|
return pages
|
2014-03-05 01:29:57 -05:00
|
|
|
}
|
|
|
|
|
2015-07-20 19:29:22 -04:00
|
|
|
// ByDate sorts the Pages by date and returns a copy.
|
|
|
|
//
|
|
|
|
// Adjacent invocactions on the same receiver will return a cached result.
|
|
|
|
//
|
|
|
|
// This may safely be executed in parallel.
|
2014-01-27 17:16:28 -05:00
|
|
|
func (p Pages) ByDate() Pages {
|
2015-07-20 19:29:22 -04:00
|
|
|
|
|
|
|
key := "pageSort.ByDate"
|
|
|
|
|
2014-01-29 17:50:31 -05:00
|
|
|
date := func(p1, p2 *Page) bool {
|
|
|
|
return p1.Date.Unix() < p2.Date.Unix()
|
|
|
|
}
|
2014-01-27 17:16:28 -05:00
|
|
|
|
2015-07-20 19:29:22 -04:00
|
|
|
pages, _ := spc.get(key, p, PageBy(date).Sort)
|
|
|
|
|
|
|
|
return pages
|
2014-01-27 17:16:28 -05:00
|
|
|
}
|
|
|
|
|
2015-07-20 19:29:22 -04:00
|
|
|
// ByPublishDate sorts the Pages by publish date and returns a copy.
|
|
|
|
//
|
|
|
|
// Adjacent invocactions on the same receiver will return a cached result.
|
|
|
|
//
|
|
|
|
// This may safely be executed in parallel.
|
2014-10-17 11:10:19 -04:00
|
|
|
func (p Pages) ByPublishDate() Pages {
|
2015-07-20 19:29:22 -04:00
|
|
|
|
|
|
|
key := "pageSort.ByPublishDate"
|
|
|
|
|
2014-10-17 11:10:19 -04:00
|
|
|
pubDate := func(p1, p2 *Page) bool {
|
|
|
|
return p1.PublishDate.Unix() < p2.PublishDate.Unix()
|
|
|
|
}
|
|
|
|
|
2015-07-20 19:29:22 -04:00
|
|
|
pages, _ := spc.get(key, p, PageBy(pubDate).Sort)
|
|
|
|
|
|
|
|
return pages
|
2014-10-17 11:10:19 -04:00
|
|
|
}
|
|
|
|
|
2015-07-20 19:29:22 -04:00
|
|
|
// ByLength sorts the Pages by length and returns a copy.
|
|
|
|
//
|
|
|
|
// Adjacent invocactions on the same receiver will return a cached result.
|
|
|
|
//
|
|
|
|
// This may safely be executed in parallel.
|
2014-01-27 17:16:28 -05:00
|
|
|
func (p Pages) ByLength() Pages {
|
2015-07-20 19:29:22 -04:00
|
|
|
|
|
|
|
key := "pageSort.ByLength"
|
|
|
|
|
2014-01-29 17:50:31 -05:00
|
|
|
length := func(p1, p2 *Page) bool {
|
|
|
|
return len(p1.Content) < len(p2.Content)
|
|
|
|
}
|
2014-01-27 17:16:28 -05:00
|
|
|
|
2015-07-20 19:29:22 -04:00
|
|
|
pages, _ := spc.get(key, p, PageBy(length).Sort)
|
|
|
|
|
|
|
|
return pages
|
2014-01-27 17:16:28 -05:00
|
|
|
}
|
|
|
|
|
2015-07-20 19:29:22 -04:00
|
|
|
// Reverse reverses the order in Pages and returns a copy.
|
|
|
|
//
|
|
|
|
// Adjacent invocactions on the same receiver will return a cached result.
|
|
|
|
//
|
|
|
|
// This may safely be executed in parallel.
|
2014-01-27 17:16:28 -05:00
|
|
|
func (p Pages) Reverse() Pages {
|
2015-07-20 19:29:22 -04:00
|
|
|
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]
|
|
|
|
}
|
2014-01-29 17:50:31 -05:00
|
|
|
}
|
2014-01-27 17:16:28 -05:00
|
|
|
|
2015-07-20 19:29:22 -04:00
|
|
|
pages, _ := spc.get(key, p, reverseFunc)
|
|
|
|
|
|
|
|
return pages
|
2014-01-27 17:16:28 -05:00
|
|
|
}
|