2022-12-30 03:20:58 -05:00
|
|
|
// Copyright 2023 The Hugo Authors. All rights reserved.
|
2013-07-04 11:32:55 -04:00
|
|
|
//
|
2015-11-23 22:16:36 -05:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
2013-07-04 11:32:55 -04: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
|
2013-07-04 11:32:55 -04: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.
|
|
|
|
|
2022-12-30 03:20:58 -05:00
|
|
|
package page
|
2013-07-04 11:32:55 -04:00
|
|
|
|
|
|
|
import (
|
2016-11-13 08:27:10 -05:00
|
|
|
"fmt"
|
2013-07-04 11:32:55 -04:00
|
|
|
"sort"
|
2019-01-02 06:33:26 -05:00
|
|
|
|
2019-08-01 04:19:19 -04:00
|
|
|
"github.com/gohugoio/hugo/compare"
|
2022-04-10 14:30:52 -04:00
|
|
|
"github.com/gohugoio/hugo/langs"
|
2013-07-04 11:32:55 -04:00
|
|
|
)
|
|
|
|
|
2016-03-24 11:01:25 -04:00
|
|
|
// The TaxonomyList is a list of all taxonomies and their values
|
|
|
|
// e.g. List['tags'] => TagTaxonomy (from above)
|
2014-04-08 23:15:57 -04:00
|
|
|
type TaxonomyList map[string]Taxonomy
|
2013-12-20 09:10:05 -05:00
|
|
|
|
2016-11-13 08:27:10 -05:00
|
|
|
func (tl TaxonomyList) String() string {
|
|
|
|
return fmt.Sprintf("TaxonomyList(%d)", len(tl))
|
|
|
|
}
|
|
|
|
|
2016-03-24 11:01:25 -04:00
|
|
|
// A Taxonomy is a map of keywords to a list of pages.
|
|
|
|
// For example
|
2022-12-30 03:20:58 -05:00
|
|
|
//
|
|
|
|
// TagTaxonomy['technology'] = WeightedPages
|
|
|
|
// TagTaxonomy['go'] = WeightedPages
|
|
|
|
type Taxonomy map[string]WeightedPages
|
2016-11-13 08:27:10 -05:00
|
|
|
|
2016-03-24 11:01:25 -04:00
|
|
|
// OrderedTaxonomy is another representation of an Taxonomy using an array rather than a map.
|
|
|
|
// Important because you can't order a map.
|
2014-04-08 23:15:57 -04:00
|
|
|
type OrderedTaxonomy []OrderedTaxonomyEntry
|
2013-11-24 16:10:20 -05:00
|
|
|
|
2022-04-10 14:30:52 -04:00
|
|
|
// getOneOPage returns one page in the taxonomy,
|
|
|
|
// nil if there is none.
|
2022-12-30 03:20:58 -05:00
|
|
|
func (t OrderedTaxonomy) getOneOPage() Page {
|
2022-04-10 14:30:52 -04:00
|
|
|
if len(t) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return t[0].Pages()[0]
|
|
|
|
}
|
|
|
|
|
2016-03-24 11:01:25 -04:00
|
|
|
// OrderedTaxonomyEntry is similar to an element of a Taxonomy, but with the key embedded (as name)
|
2022-12-30 03:20:58 -05:00
|
|
|
// e.g: {Name: Technology, WeightedPages: TaxonomyPages}
|
2014-04-08 23:15:57 -04:00
|
|
|
type OrderedTaxonomyEntry struct {
|
2019-01-02 06:33:26 -05:00
|
|
|
Name string
|
2022-12-30 03:20:58 -05:00
|
|
|
WeightedPages
|
2013-11-24 16:10:20 -05:00
|
|
|
}
|
|
|
|
|
2016-03-24 11:01:25 -04:00
|
|
|
// Get the weighted pages for the given key.
|
2022-12-30 03:20:58 -05:00
|
|
|
func (i Taxonomy) Get(key string) WeightedPages {
|
2017-01-10 04:55:03 -05:00
|
|
|
return i[key]
|
2016-03-03 16:01:09 -05:00
|
|
|
}
|
|
|
|
|
2016-03-24 11:01:25 -04:00
|
|
|
// Count the weighted pages for the given key.
|
2017-01-10 04:55:03 -05:00
|
|
|
func (i Taxonomy) Count(key string) int { return len(i[key]) }
|
2016-03-24 11:01:25 -04:00
|
|
|
|
|
|
|
// TaxonomyArray returns an ordered taxonomy with a non defined order.
|
2014-04-08 23:15:57 -04:00
|
|
|
func (i Taxonomy) TaxonomyArray() OrderedTaxonomy {
|
|
|
|
ies := make([]OrderedTaxonomyEntry, len(i))
|
2013-10-25 18:40:55 -04:00
|
|
|
count := 0
|
|
|
|
for k, v := range i {
|
2014-04-08 23:15:57 -04:00
|
|
|
ies[count] = OrderedTaxonomyEntry{Name: k, WeightedPages: v}
|
2013-10-25 18:40:55 -04:00
|
|
|
count++
|
|
|
|
}
|
|
|
|
return ies
|
|
|
|
}
|
|
|
|
|
2016-03-24 11:01:25 -04:00
|
|
|
// Alphabetical returns an ordered taxonomy sorted by key name.
|
2014-04-08 23:15:57 -04:00
|
|
|
func (i Taxonomy) Alphabetical() OrderedTaxonomy {
|
2022-04-10 14:30:52 -04:00
|
|
|
ia := i.TaxonomyArray()
|
|
|
|
p := ia.getOneOPage()
|
|
|
|
if p == nil {
|
|
|
|
return ia
|
|
|
|
}
|
|
|
|
currentSite := p.Site().Current()
|
|
|
|
coll := langs.GetCollator(currentSite.Language())
|
|
|
|
coll.Lock()
|
|
|
|
defer coll.Unlock()
|
2014-04-08 23:15:57 -04:00
|
|
|
name := func(i1, i2 *OrderedTaxonomyEntry) bool {
|
2022-04-10 14:30:52 -04:00
|
|
|
return coll.CompareStrings(i1.Name, i2.Name) < 0
|
2013-10-25 18:40:55 -04:00
|
|
|
}
|
2016-03-24 11:01:25 -04:00
|
|
|
oiBy(name).Sort(ia)
|
2013-10-25 18:40:55 -04:00
|
|
|
return ia
|
|
|
|
}
|
|
|
|
|
2016-03-24 11:01:25 -04:00
|
|
|
// ByCount returns an ordered taxonomy sorted by # of pages per key.
|
2016-04-01 14:17:16 -04:00
|
|
|
// If taxonomies have the same # of pages, sort them alphabetical
|
2014-04-08 23:15:57 -04:00
|
|
|
func (i Taxonomy) ByCount() OrderedTaxonomy {
|
|
|
|
count := func(i1, i2 *OrderedTaxonomyEntry) bool {
|
2016-04-01 14:17:16 -04:00
|
|
|
li1 := len(i1.WeightedPages)
|
|
|
|
li2 := len(i2.WeightedPages)
|
|
|
|
|
|
|
|
if li1 == li2 {
|
2019-08-01 04:19:19 -04:00
|
|
|
return compare.LessStrings(i1.Name, i2.Name)
|
2016-04-01 14:17:16 -04:00
|
|
|
}
|
|
|
|
return li1 > li2
|
2013-10-25 18:40:55 -04:00
|
|
|
}
|
|
|
|
|
2014-04-08 23:15:57 -04:00
|
|
|
ia := i.TaxonomyArray()
|
2016-03-24 11:01:25 -04:00
|
|
|
oiBy(count).Sort(ia)
|
2013-10-25 18:40:55 -04:00
|
|
|
return ia
|
|
|
|
}
|
|
|
|
|
2016-03-24 11:01:25 -04:00
|
|
|
// Pages returns the Pages for this taxonomy.
|
2022-12-30 03:20:58 -05:00
|
|
|
func (ie OrderedTaxonomyEntry) Pages() Pages {
|
2013-10-31 09:49:29 -04:00
|
|
|
return ie.WeightedPages.Pages()
|
|
|
|
}
|
|
|
|
|
2016-03-24 11:01:25 -04:00
|
|
|
// Count returns the count the pages in this taxonomy.
|
2014-04-08 23:15:57 -04:00
|
|
|
func (ie OrderedTaxonomyEntry) Count() int {
|
2013-10-31 09:49:29 -04:00
|
|
|
return len(ie.WeightedPages)
|
|
|
|
}
|
|
|
|
|
2016-03-24 11:01:25 -04:00
|
|
|
// Term returns the name given to this taxonomy.
|
2014-08-29 23:44:39 -04:00
|
|
|
func (ie OrderedTaxonomyEntry) Term() string {
|
|
|
|
return ie.Name
|
|
|
|
}
|
|
|
|
|
2016-03-24 11:01:25 -04:00
|
|
|
// Reverse reverses the order of the entries in this taxonomy.
|
2015-05-09 18:21:30 -04:00
|
|
|
func (t OrderedTaxonomy) Reverse() OrderedTaxonomy {
|
|
|
|
for i, j := 0, len(t)-1; i < j; i, j = i+1, j-1 {
|
|
|
|
t[i], t[j] = t[j], t[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2014-04-08 23:15:57 -04:00
|
|
|
// A type to implement the sort interface for TaxonomyEntries.
|
|
|
|
type orderedTaxonomySorter struct {
|
|
|
|
taxonomy OrderedTaxonomy
|
2016-03-24 11:01:25 -04:00
|
|
|
by oiBy
|
2013-12-20 09:10:05 -05:00
|
|
|
}
|
2013-10-25 18:40:55 -04:00
|
|
|
|
2013-12-20 09:10:05 -05:00
|
|
|
// Closure used in the Sort.Less method.
|
2016-03-24 11:01:25 -04:00
|
|
|
type oiBy func(i1, i2 *OrderedTaxonomyEntry) bool
|
2013-12-20 09:10:05 -05:00
|
|
|
|
2016-03-24 11:01:25 -04:00
|
|
|
func (by oiBy) Sort(taxonomy OrderedTaxonomy) {
|
2014-04-08 23:15:57 -04:00
|
|
|
ps := &orderedTaxonomySorter{
|
|
|
|
taxonomy: taxonomy,
|
|
|
|
by: by, // The Sort method's receiver is the function (closure) that defines the sort order.
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
2014-09-09 05:59:47 -04:00
|
|
|
sort.Stable(ps)
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
2013-10-25 18:40:55 -04:00
|
|
|
// Len is part of sort.Interface.
|
2014-04-08 23:15:57 -04:00
|
|
|
func (s *orderedTaxonomySorter) Len() int {
|
|
|
|
return len(s.taxonomy)
|
2013-10-25 18:40:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Swap is part of sort.Interface.
|
2014-04-08 23:15:57 -04:00
|
|
|
func (s *orderedTaxonomySorter) Swap(i, j int) {
|
|
|
|
s.taxonomy[i], s.taxonomy[j] = s.taxonomy[j], s.taxonomy[i]
|
2013-10-25 18:40:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Less is part of sort.Interface. It is implemented by calling the "by" closure in the sorter.
|
2014-04-08 23:15:57 -04:00
|
|
|
func (s *orderedTaxonomySorter) Less(i, j int) bool {
|
|
|
|
return s.by(&s.taxonomy[i], &s.taxonomy[j])
|
2013-12-20 09:10:05 -05:00
|
|
|
}
|