hugo/hugolib/taxonomy.go

225 lines
6 KiB
Go
Raw Normal View History

// Copyright 2015 The Hugo Authors. All rights reserved.
2013-07-04 15:32:55 +00:00
//
2015-11-24 03:16:36 +00:00
// Licensed under the Apache License, Version 2.0 (the "License");
2013-07-04 15:32:55 +00:00
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2015-11-24 03:16:36 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
2013-07-04 15:32:55 +00: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 (
"fmt"
2013-07-04 15:32:55 +00:00
"sort"
)
2016-03-24 15:01:25 +00:00
// The TaxonomyList is a list of all taxonomies and their values
// e.g. List['tags'] => TagTaxonomy (from above)
type TaxonomyList map[string]Taxonomy
func (tl TaxonomyList) String() string {
return fmt.Sprintf("TaxonomyList(%d)", len(tl))
}
2016-03-24 15:01:25 +00:00
// A Taxonomy is a map of keywords to a list of pages.
// For example
// TagTaxonomy['technology'] = WeightedPages
// TagTaxonomy['go'] = WeightedPages2
type Taxonomy map[string]WeightedPages
2016-03-24 15:01:25 +00:00
// WeightedPages is a list of Pages with their corresponding (and relative) weight
// [{Weight: 30, Page: *1}, {Weight: 40, Page: *2}]
type WeightedPages []WeightedPage
2016-03-24 15:01:25 +00:00
// A WeightedPage is a Page with a weight.
type WeightedPage struct {
Weight int
*Page
}
func (w WeightedPage) String() string {
return fmt.Sprintf("WeightedPage(%d,%q)", w.Weight, w.Page.Title)
}
2016-03-24 15:01:25 +00:00
// OrderedTaxonomy is another representation of an Taxonomy using an array rather than a map.
// Important because you can't order a map.
type OrderedTaxonomy []OrderedTaxonomyEntry
2013-11-24 21:10:20 +00:00
2016-03-24 15:01:25 +00:00
// OrderedTaxonomyEntry is similar to an element of a Taxonomy, but with the key embedded (as name)
// e.g: {Name: Technology, WeightedPages: Taxonomyedpages}
type OrderedTaxonomyEntry struct {
Name string
WeightedPages WeightedPages
2013-11-24 21:10:20 +00:00
}
2016-03-24 15:01:25 +00:00
// Get the weighted pages for the given key.
func (i Taxonomy) Get(key string) WeightedPages {
return i[key]
}
2016-03-24 15:01:25 +00:00
// Count the weighted pages for the given key.
func (i Taxonomy) Count(key string) int { return len(i[key]) }
2016-03-24 15:01:25 +00:00
func (i Taxonomy) add(key string, w WeightedPage) {
i[key] = append(i[key], w)
2013-07-04 15:32:55 +00:00
}
2016-03-24 15:01:25 +00:00
// TaxonomyArray returns an ordered taxonomy with a non defined order.
func (i Taxonomy) TaxonomyArray() OrderedTaxonomy {
ies := make([]OrderedTaxonomyEntry, len(i))
count := 0
for k, v := range i {
ies[count] = OrderedTaxonomyEntry{Name: k, WeightedPages: v}
count++
}
return ies
}
2016-03-24 15:01:25 +00:00
// Alphabetical returns an ordered taxonomy sorted by key name.
func (i Taxonomy) Alphabetical() OrderedTaxonomy {
name := func(i1, i2 *OrderedTaxonomyEntry) bool {
return i1.Name < i2.Name
}
ia := i.TaxonomyArray()
2016-03-24 15:01:25 +00:00
oiBy(name).Sort(ia)
return ia
}
2016-03-24 15:01:25 +00:00
// ByCount returns an ordered taxonomy sorted by # of pages per key.
// If taxonomies have the same # of pages, sort them alphabetical
func (i Taxonomy) ByCount() OrderedTaxonomy {
count := func(i1, i2 *OrderedTaxonomyEntry) bool {
li1 := len(i1.WeightedPages)
li2 := len(i2.WeightedPages)
if li1 == li2 {
return i1.Name < i2.Name
}
return li1 > li2
}
ia := i.TaxonomyArray()
2016-03-24 15:01:25 +00:00
oiBy(count).Sort(ia)
return ia
}
2016-03-24 15:01:25 +00:00
// Pages returns the Pages for this taxonomy.
func (ie OrderedTaxonomyEntry) Pages() Pages {
2013-10-31 13:49:29 +00:00
return ie.WeightedPages.Pages()
}
2016-03-24 15:01:25 +00:00
// Count returns the count the pages in this taxonomy.
func (ie OrderedTaxonomyEntry) Count() int {
2013-10-31 13:49:29 +00:00
return len(ie.WeightedPages)
}
2016-03-24 15:01:25 +00:00
// Term returns the name given to this taxonomy.
func (ie OrderedTaxonomyEntry) Term() string {
return ie.Name
}
2016-03-24 15:01:25 +00:00
// Reverse reverses the order of the entries in this taxonomy.
2015-05-09 22:21:30 +00: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
}
// A type to implement the sort interface for TaxonomyEntries.
type orderedTaxonomySorter struct {
taxonomy OrderedTaxonomy
2016-03-24 15:01:25 +00:00
by oiBy
}
// Closure used in the Sort.Less method.
2016-03-24 15:01:25 +00:00
type oiBy func(i1, i2 *OrderedTaxonomyEntry) bool
2016-03-24 15:01:25 +00:00
func (by oiBy) Sort(taxonomy OrderedTaxonomy) {
ps := &orderedTaxonomySorter{
taxonomy: taxonomy,
by: by, // The Sort method's receiver is the function (closure) that defines the sort order.
2013-07-04 15:32:55 +00:00
}
sort.Stable(ps)
2013-07-04 15:32:55 +00:00
}
// Len is part of sort.Interface.
func (s *orderedTaxonomySorter) Len() int {
return len(s.taxonomy)
}
// Swap is part of sort.Interface.
func (s *orderedTaxonomySorter) Swap(i, j int) {
s.taxonomy[i], s.taxonomy[j] = s.taxonomy[j], s.taxonomy[i]
}
// Less is part of sort.Interface. It is implemented by calling the "by" closure in the sorter.
func (s *orderedTaxonomySorter) Less(i, j int) bool {
return s.by(&s.taxonomy[i], &s.taxonomy[j])
}
2016-03-24 15:01:25 +00:00
// Pages returns the Pages in this weighted page set.
func (wp WeightedPages) Pages() Pages {
pages := make(Pages, len(wp))
for i := range wp {
pages[i] = wp[i].Page
}
return pages
}
2013-11-24 21:10:20 +00:00
2016-03-24 15:01:25 +00:00
// Prev returns the previous Page relative to the given Page in
// this weighted page set.
func (wp WeightedPages) Prev(cur *Page) *Page {
for x, c := range wp {
if c.Page.UniqueID() == cur.UniqueID() {
if x == 0 {
return wp[len(wp)-1].Page
}
return wp[x-1].Page
}
}
return nil
}
2016-03-24 15:01:25 +00:00
// Next returns the next Page relative to the given Page in
// this weighted page set.
func (wp WeightedPages) Next(cur *Page) *Page {
for x, c := range wp {
if c.Page.UniqueID() == cur.UniqueID() {
if x < len(wp)-1 {
return wp[x+1].Page
}
return wp[0].Page
}
}
return nil
}
2015-03-07 11:53:20 +00:00
func (wp WeightedPages) Len() int { return len(wp) }
func (wp WeightedPages) Swap(i, j int) { wp[i], wp[j] = wp[j], wp[i] }
2016-03-24 15:01:25 +00:00
// Sort stable sorts this weighted page set.
func (wp WeightedPages) Sort() { sort.Stable(wp) }
// Count returns the number of pages in this weighted page set.
func (wp WeightedPages) Count() int { return len(wp) }
2015-03-07 11:53:20 +00:00
func (wp WeightedPages) Less(i, j int) bool {
if wp[i].Weight == wp[j].Weight {
if wp[i].Page.Date.Equal(wp[j].Page.Date) {
return wp[i].Page.Title < wp[j].Page.Title
2014-09-10 19:33:49 +00:00
}
2015-03-07 11:53:20 +00:00
return wp[i].Page.Date.After(wp[i].Page.Date)
}
2015-03-07 11:53:20 +00:00
return wp[i].Weight < wp[j].Weight
2013-11-24 21:10:20 +00:00
}
// TODO mimic PagesSorter for WeightedPages