2015-12-07 13:57:01 -05:00
|
|
|
// Copyright 2015 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.
|
|
|
|
|
|
|
|
package hugolib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
2014-04-05 01:26:43 -04:00
|
|
|
|
|
|
|
"github.com/spf13/hugo/helpers"
|
2013-07-04 11:32:55 -04:00
|
|
|
)
|
|
|
|
|
2013-12-20 09:10:05 -05:00
|
|
|
/*
|
2014-04-08 23:15:57 -04:00
|
|
|
* An taxonomy list is a list of all taxonomies and their values
|
|
|
|
* EG. List['tags'] => TagTaxonomy (from above)
|
2013-12-20 09:10:05 -05:00
|
|
|
*/
|
2014-04-08 23:15:57 -04:00
|
|
|
type TaxonomyList map[string]Taxonomy
|
2013-12-20 09:10:05 -05:00
|
|
|
|
|
|
|
/*
|
2014-04-08 23:15:57 -04:00
|
|
|
* An taxonomy is a map of keywords to a list of pages.
|
2013-12-20 09:10:05 -05:00
|
|
|
* For example
|
2014-04-08 23:15:57 -04:00
|
|
|
* TagTaxonomy['technology'] = WeightedPages
|
|
|
|
* TagTaxonomy['go'] = WeightedPages2
|
2013-12-20 09:10:05 -05:00
|
|
|
*/
|
2014-04-08 23:15:57 -04:00
|
|
|
type Taxonomy map[string]WeightedPages
|
2013-12-20 09:10:05 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* A list of Pages with their corresponding (and relative) weight
|
|
|
|
* [{Weight: 30, Page: *1}, {Weight: 40, Page: *2}]
|
|
|
|
*/
|
|
|
|
type WeightedPages []WeightedPage
|
|
|
|
type WeightedPage struct {
|
2013-10-17 23:57:25 -04:00
|
|
|
Weight int
|
|
|
|
Page *Page
|
|
|
|
}
|
|
|
|
|
2013-12-20 09:10:05 -05:00
|
|
|
/*
|
2014-04-08 23:15:57 -04:00
|
|
|
* This is another representation of an Taxonomy using an array rather than a map.
|
2013-12-20 09:10:05 -05:00
|
|
|
* 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
|
|
|
|
2013-12-20 09:10:05 -05:00
|
|
|
/*
|
2014-04-08 23:15:57 -04:00
|
|
|
* Similar to an element of an Taxonomy, but with the key embedded (as name)
|
|
|
|
* Eg: {Name: Technology, WeightedPages: Taxonomyedpages}
|
2013-12-20 09:10:05 -05:00
|
|
|
*/
|
2014-04-08 23:15:57 -04:00
|
|
|
type OrderedTaxonomyEntry struct {
|
2013-12-20 09:10:05 -05:00
|
|
|
Name string
|
|
|
|
WeightedPages WeightedPages
|
2013-11-24 16:10:20 -05:00
|
|
|
}
|
|
|
|
|
2014-04-08 23:15:57 -04:00
|
|
|
// KeyPrep... Taxonomies should be case insensitive. Can make it easily conditional later.
|
2013-07-04 11:32:55 -04:00
|
|
|
func kp(in string) string {
|
2015-09-01 08:53:25 -04:00
|
|
|
return helpers.MakePathSanitized(in)
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
2016-03-03 16:01:09 -05:00
|
|
|
func (i Taxonomy) Get(key string) WeightedPages {
|
|
|
|
if val, ok := i[key]; ok {
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
return i[kp(key)]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i Taxonomy) Count(key string) int { return len(i[kp(key)]) }
|
2015-05-31 14:30:53 -04:00
|
|
|
func (i Taxonomy) Add(key string, w WeightedPage, pretty bool) {
|
|
|
|
if !pretty {
|
|
|
|
key = kp(key)
|
|
|
|
}
|
2013-10-17 23:57:25 -04:00
|
|
|
i[key] = append(i[key], w)
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
2014-04-08 23:15:57 -04:00
|
|
|
// Returns an ordered taxonomy with a non defined order
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-04-08 23:15:57 -04:00
|
|
|
// Returns an ordered taxonomy sorted by key name
|
|
|
|
func (i Taxonomy) Alphabetical() OrderedTaxonomy {
|
|
|
|
name := func(i1, i2 *OrderedTaxonomyEntry) bool {
|
2013-10-25 18:40:55 -04:00
|
|
|
return i1.Name < i2.Name
|
|
|
|
}
|
|
|
|
|
2014-04-08 23:15:57 -04:00
|
|
|
ia := i.TaxonomyArray()
|
2013-12-20 09:10:05 -05:00
|
|
|
OIby(name).Sort(ia)
|
2013-10-25 18:40:55 -04:00
|
|
|
return ia
|
|
|
|
}
|
|
|
|
|
2014-04-08 23:15:57 -04:00
|
|
|
// Returns an ordered taxonomy sorted by # of pages per key
|
|
|
|
func (i Taxonomy) ByCount() OrderedTaxonomy {
|
|
|
|
count := func(i1, i2 *OrderedTaxonomyEntry) bool {
|
2013-10-31 09:49:29 -04:00
|
|
|
return len(i1.WeightedPages) > len(i2.WeightedPages)
|
2013-10-25 18:40:55 -04:00
|
|
|
}
|
|
|
|
|
2014-04-08 23:15:57 -04:00
|
|
|
ia := i.TaxonomyArray()
|
2013-12-20 09:10:05 -05:00
|
|
|
OIby(count).Sort(ia)
|
2013-10-25 18:40:55 -04:00
|
|
|
return ia
|
|
|
|
}
|
|
|
|
|
2013-12-20 09:10:05 -05:00
|
|
|
// Helper to move the page access up a level
|
2014-08-29 23:43:38 -04:00
|
|
|
func (ie OrderedTaxonomyEntry) Pages() Pages {
|
2013-10-31 09:49:29 -04:00
|
|
|
return ie.WeightedPages.Pages()
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2014-08-29 23:44:39 -04:00
|
|
|
func (ie OrderedTaxonomyEntry) Term() string {
|
|
|
|
return ie.Name
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2013-12-20 09:10:05 -05:00
|
|
|
/*
|
2014-04-08 23:15:57 -04:00
|
|
|
* Implementation of a custom sorter for OrderedTaxonomies
|
2013-12-20 09:10:05 -05:00
|
|
|
*/
|
2013-10-31 09:49:29 -04:00
|
|
|
|
2014-04-08 23:15:57 -04:00
|
|
|
// A type to implement the sort interface for TaxonomyEntries.
|
|
|
|
type orderedTaxonomySorter struct {
|
|
|
|
taxonomy OrderedTaxonomy
|
|
|
|
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.
|
2014-04-08 23:15:57 -04:00
|
|
|
type OIby func(i1, i2 *OrderedTaxonomyEntry) bool
|
2013-12-20 09:10:05 -05:00
|
|
|
|
2014-04-08 23:15:57 -04: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 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
|
|
|
}
|
|
|
|
|
|
|
|
func (wp WeightedPages) Pages() Pages {
|
|
|
|
pages := make(Pages, len(wp))
|
|
|
|
for i := range wp {
|
|
|
|
pages[i] = wp[i].Page
|
|
|
|
}
|
|
|
|
return pages
|
2013-10-25 18:40:55 -04:00
|
|
|
}
|
2013-11-24 16:10:20 -05:00
|
|
|
|
2014-11-27 23:08:06 -05:00
|
|
|
func (wp WeightedPages) Prev(cur *Page) *Page {
|
|
|
|
for x, c := range wp {
|
2015-03-11 13:34:57 -04:00
|
|
|
if c.Page.UniqueID() == cur.UniqueID() {
|
2014-11-27 23:08:06 -05:00
|
|
|
if x == 0 {
|
|
|
|
return wp[len(wp)-1].Page
|
|
|
|
}
|
|
|
|
return wp[x-1].Page
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wp WeightedPages) Next(cur *Page) *Page {
|
|
|
|
for x, c := range wp {
|
2015-03-11 13:34:57 -04:00
|
|
|
if c.Page.UniqueID() == cur.UniqueID() {
|
2014-11-27 23:08:06 -05:00
|
|
|
if x < len(wp)-1 {
|
|
|
|
return wp[x+1].Page
|
|
|
|
}
|
|
|
|
return wp[0].Page
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-07 06:53:20 -05:00
|
|
|
func (wp WeightedPages) Len() int { return len(wp) }
|
|
|
|
func (wp WeightedPages) Swap(i, j int) { wp[i], wp[j] = wp[j], wp[i] }
|
|
|
|
func (wp WeightedPages) Sort() { sort.Stable(wp) }
|
|
|
|
func (wp WeightedPages) Count() int { return len(wp) }
|
|
|
|
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 15:33:49 -04:00
|
|
|
}
|
2015-03-07 06:53:20 -05:00
|
|
|
return wp[i].Page.Date.After(wp[i].Page.Date)
|
2013-12-20 09:10:05 -05:00
|
|
|
}
|
2015-03-07 06:53:20 -05:00
|
|
|
return wp[i].Weight < wp[j].Weight
|
2013-11-24 16:10:20 -05:00
|
|
|
}
|
|
|
|
|
2013-12-20 09:10:05 -05:00
|
|
|
// TODO mimic PagesSorter for WeightedPages
|