github.com/SDLMoe/hugo@v0.47.1/hugolib/pageSort.go (about)

     1  // Copyright 2015 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 hugolib
    15  
    16  import (
    17  	"sort"
    18  
    19  	"github.com/spf13/cast"
    20  )
    21  
    22  var spc = newPageCache()
    23  
    24  /*
    25   * Implementation of a custom sorter for Pages
    26   */
    27  
    28  // A pageSorter implements the sort interface for Pages
    29  type pageSorter struct {
    30  	pages Pages
    31  	by    pageBy
    32  }
    33  
    34  // pageBy is a closure used in the Sort.Less method.
    35  type pageBy func(p1, p2 *Page) bool
    36  
    37  // Sort stable sorts the pages given the receiver's sort order.
    38  func (by pageBy) Sort(pages Pages) {
    39  	ps := &pageSorter{
    40  		pages: pages,
    41  		by:    by, // The Sort method's receiver is the function (closure) that defines the sort order.
    42  	}
    43  	sort.Stable(ps)
    44  }
    45  
    46  // defaultPageSort is the default sort for pages in Hugo:
    47  // Order by Weight, Date, LinkTitle and then full file path.
    48  var defaultPageSort = func(p1, p2 *Page) bool {
    49  	if p1.Weight == p2.Weight {
    50  		if p1.Date.Unix() == p2.Date.Unix() {
    51  			if p1.LinkTitle() == p2.LinkTitle() {
    52  				return (p1.FullFilePath() < p2.FullFilePath())
    53  			}
    54  			return (p1.LinkTitle() < p2.LinkTitle())
    55  		}
    56  		return p1.Date.Unix() > p2.Date.Unix()
    57  	}
    58  
    59  	if p2.Weight == 0 {
    60  		return true
    61  	}
    62  
    63  	if p1.Weight == 0 {
    64  		return false
    65  	}
    66  
    67  	return p1.Weight < p2.Weight
    68  }
    69  
    70  var languagePageSort = func(p1, p2 *Page) bool {
    71  	if p1.Language().Weight == p2.Language().Weight {
    72  		if p1.Date.Unix() == p2.Date.Unix() {
    73  			if p1.LinkTitle() == p2.LinkTitle() {
    74  				return (p1.FullFilePath() < p2.FullFilePath())
    75  			}
    76  			return (p1.LinkTitle() < p2.LinkTitle())
    77  		}
    78  		return p1.Date.Unix() > p2.Date.Unix()
    79  	}
    80  
    81  	if p2.Language().Weight == 0 {
    82  		return true
    83  	}
    84  
    85  	if p1.Language().Weight == 0 {
    86  		return false
    87  	}
    88  
    89  	return p1.Language().Weight < p2.Language().Weight
    90  }
    91  
    92  func (ps *pageSorter) Len() int      { return len(ps.pages) }
    93  func (ps *pageSorter) Swap(i, j int) { ps.pages[i], ps.pages[j] = ps.pages[j], ps.pages[i] }
    94  
    95  // Less is part of sort.Interface. It is implemented by calling the "by" closure in the sorter.
    96  func (ps *pageSorter) Less(i, j int) bool { return ps.by(ps.pages[i], ps.pages[j]) }
    97  
    98  // Sort sorts the pages by the default sort order defined:
    99  // Order by Weight, Date, LinkTitle and then full file path.
   100  func (p Pages) Sort() {
   101  	pageBy(defaultPageSort).Sort(p)
   102  }
   103  
   104  // Limit limits the number of pages returned to n.
   105  func (p Pages) Limit(n int) Pages {
   106  	if len(p) > n {
   107  		return p[0:n]
   108  	}
   109  	return p
   110  }
   111  
   112  // ByWeight sorts the Pages by weight and returns a copy.
   113  //
   114  // Adjacent invocations on the same receiver will return a cached result.
   115  //
   116  // This may safely be executed  in parallel.
   117  func (p Pages) ByWeight() Pages {
   118  	key := "pageSort.ByWeight"
   119  	pages, _ := spc.get(key, pageBy(defaultPageSort).Sort, p)
   120  	return pages
   121  }
   122  
   123  // ByTitle sorts the Pages by title and returns a copy.
   124  //
   125  // Adjacent invocations on the same receiver will return a cached result.
   126  //
   127  // This may safely be executed  in parallel.
   128  func (p Pages) ByTitle() Pages {
   129  
   130  	key := "pageSort.ByTitle"
   131  
   132  	title := func(p1, p2 *Page) bool {
   133  		return p1.title < p2.title
   134  	}
   135  
   136  	pages, _ := spc.get(key, pageBy(title).Sort, p)
   137  	return pages
   138  }
   139  
   140  // ByLinkTitle sorts the Pages by link title and returns a copy.
   141  //
   142  // Adjacent invocations on the same receiver will return a cached result.
   143  //
   144  // This may safely be executed  in parallel.
   145  func (p Pages) ByLinkTitle() Pages {
   146  
   147  	key := "pageSort.ByLinkTitle"
   148  
   149  	linkTitle := func(p1, p2 *Page) bool {
   150  		return p1.linkTitle < p2.linkTitle
   151  	}
   152  
   153  	pages, _ := spc.get(key, pageBy(linkTitle).Sort, p)
   154  
   155  	return pages
   156  }
   157  
   158  // ByDate sorts the Pages by date and returns a copy.
   159  //
   160  // Adjacent invocations on the same receiver will return a cached result.
   161  //
   162  // This may safely be executed  in parallel.
   163  func (p Pages) ByDate() Pages {
   164  
   165  	key := "pageSort.ByDate"
   166  
   167  	date := func(p1, p2 *Page) bool {
   168  		return p1.Date.Unix() < p2.Date.Unix()
   169  	}
   170  
   171  	pages, _ := spc.get(key, pageBy(date).Sort, p)
   172  
   173  	return pages
   174  }
   175  
   176  // ByPublishDate sorts the Pages by publish date and returns a copy.
   177  //
   178  // Adjacent invocations on the same receiver will return a cached result.
   179  //
   180  // This may safely be executed  in parallel.
   181  func (p Pages) ByPublishDate() Pages {
   182  
   183  	key := "pageSort.ByPublishDate"
   184  
   185  	pubDate := func(p1, p2 *Page) bool {
   186  		return p1.PublishDate.Unix() < p2.PublishDate.Unix()
   187  	}
   188  
   189  	pages, _ := spc.get(key, pageBy(pubDate).Sort, p)
   190  
   191  	return pages
   192  }
   193  
   194  // ByExpiryDate sorts the Pages by publish date and returns a copy.
   195  //
   196  // Adjacent invocations on the same receiver will return a cached result.
   197  //
   198  // This may safely be executed  in parallel.
   199  func (p Pages) ByExpiryDate() Pages {
   200  
   201  	key := "pageSort.ByExpiryDate"
   202  
   203  	expDate := func(p1, p2 *Page) bool {
   204  		return p1.ExpiryDate.Unix() < p2.ExpiryDate.Unix()
   205  	}
   206  
   207  	pages, _ := spc.get(key, pageBy(expDate).Sort, p)
   208  
   209  	return pages
   210  }
   211  
   212  // ByLastmod sorts the Pages by the last modification date and returns a copy.
   213  //
   214  // Adjacent invocations on the same receiver will return a cached result.
   215  //
   216  // This may safely be executed  in parallel.
   217  func (p Pages) ByLastmod() Pages {
   218  
   219  	key := "pageSort.ByLastmod"
   220  
   221  	date := func(p1, p2 *Page) bool {
   222  		return p1.Lastmod.Unix() < p2.Lastmod.Unix()
   223  	}
   224  
   225  	pages, _ := spc.get(key, pageBy(date).Sort, p)
   226  
   227  	return pages
   228  }
   229  
   230  // ByLength sorts the Pages by length and returns a copy.
   231  //
   232  // Adjacent invocations on the same receiver will return a cached result.
   233  //
   234  // This may safely be executed  in parallel.
   235  func (p Pages) ByLength() Pages {
   236  
   237  	key := "pageSort.ByLength"
   238  
   239  	length := func(p1, p2 *Page) bool {
   240  		return len(p1.content()) < len(p2.content())
   241  	}
   242  
   243  	pages, _ := spc.get(key, pageBy(length).Sort, p)
   244  
   245  	return pages
   246  }
   247  
   248  // ByLanguage sorts the Pages by the language's Weight.
   249  //
   250  // Adjacent invocations on the same receiver will return a cached result.
   251  //
   252  // This may safely be executed  in parallel.
   253  func (p Pages) ByLanguage() Pages {
   254  
   255  	key := "pageSort.ByLanguage"
   256  
   257  	pages, _ := spc.get(key, pageBy(languagePageSort).Sort, p)
   258  
   259  	return pages
   260  }
   261  
   262  // Reverse reverses the order in Pages and returns a copy.
   263  //
   264  // Adjacent invocations on the same receiver will return a cached result.
   265  //
   266  // This may safely be executed  in parallel.
   267  func (p Pages) Reverse() Pages {
   268  	key := "pageSort.Reverse"
   269  
   270  	reverseFunc := func(pages Pages) {
   271  		for i, j := 0, len(pages)-1; i < j; i, j = i+1, j-1 {
   272  			pages[i], pages[j] = pages[j], pages[i]
   273  		}
   274  	}
   275  
   276  	pages, _ := spc.get(key, reverseFunc, p)
   277  
   278  	return pages
   279  }
   280  
   281  func (p Pages) ByParam(paramsKey interface{}) Pages {
   282  	paramsKeyStr := cast.ToString(paramsKey)
   283  	key := "pageSort.ByParam." + paramsKeyStr
   284  
   285  	paramsKeyComparator := func(p1, p2 *Page) bool {
   286  		v1, _ := p1.Param(paramsKeyStr)
   287  		v2, _ := p2.Param(paramsKeyStr)
   288  		s1 := cast.ToString(v1)
   289  		s2 := cast.ToString(v2)
   290  
   291  		// Sort nils last.
   292  		if s1 == "" {
   293  			return false
   294  		} else if s2 == "" {
   295  			return true
   296  		}
   297  
   298  		return s1 < s2
   299  	}
   300  
   301  	pages, _ := spc.get(key, pageBy(paramsKeyComparator).Sort, p)
   302  
   303  	return pages
   304  }