2013-07-04 11:32:55 -04:00
|
|
|
// Copyright © 2013 Steve Francia <spf@spf13.com>.
|
|
|
|
//
|
|
|
|
// Licensed under the Simple Public License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://opensource.org/licenses/Simple-2.0
|
|
|
|
//
|
|
|
|
// 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 (
|
2013-12-05 09:29:41 -05:00
|
|
|
"github.com/spf13/hugo/helpers"
|
2013-07-04 11:32:55 -04:00
|
|
|
"sort"
|
|
|
|
)
|
|
|
|
|
2013-12-20 09:10:05 -05:00
|
|
|
/*
|
|
|
|
* An index list is a list of all indexes and their values
|
|
|
|
* EG. List['tags'] => TagIndex (from above)
|
|
|
|
*/
|
|
|
|
type IndexList map[string]Index
|
|
|
|
|
|
|
|
/*
|
|
|
|
* An index is a map of keywords to a list of pages.
|
|
|
|
* For example
|
|
|
|
* TagIndex['technology'] = WeightedPages
|
|
|
|
* TagIndex['golang'] = WeightedPages2
|
|
|
|
*/
|
|
|
|
type Index map[string]WeightedPages
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
/*
|
|
|
|
* This is another representation of an Index using an array rather than a map.
|
|
|
|
* Important because you can't order a map.
|
|
|
|
*/
|
|
|
|
type OrderedIndex []OrderedIndexEntry
|
2013-11-24 16:10:20 -05:00
|
|
|
|
2013-12-20 09:10:05 -05:00
|
|
|
/*
|
|
|
|
* Similar to an element of an Index, but with the key embedded (as name)
|
|
|
|
* Eg: {Name: Technology, WeightedPages: Indexedpages}
|
|
|
|
*/
|
|
|
|
type OrderedIndexEntry struct {
|
|
|
|
Name string
|
|
|
|
WeightedPages WeightedPages
|
2013-11-24 16:10:20 -05:00
|
|
|
}
|
|
|
|
|
2013-07-04 11:32:55 -04:00
|
|
|
// KeyPrep... Indexes should be case insensitive. Can make it easily conditional later.
|
|
|
|
func kp(in string) string {
|
2014-02-02 09:18:01 -05:00
|
|
|
return helpers.MakePath(in)
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
2013-12-20 09:10:05 -05:00
|
|
|
func (i Index) Get(key string) WeightedPages { return i[kp(key)] }
|
|
|
|
func (i Index) Count(key string) int { return len(i[kp(key)]) }
|
|
|
|
func (i Index) Add(key string, w WeightedPage) {
|
2013-07-04 11:32:55 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-12-20 09:10:05 -05:00
|
|
|
// Returns an ordered index with a non defined order
|
|
|
|
func (i Index) IndexArray() OrderedIndex {
|
|
|
|
ies := make([]OrderedIndexEntry, len(i))
|
2013-10-25 18:40:55 -04:00
|
|
|
count := 0
|
|
|
|
for k, v := range i {
|
2013-12-20 09:10:05 -05:00
|
|
|
ies[count] = OrderedIndexEntry{Name: k, WeightedPages: v}
|
2013-10-25 18:40:55 -04:00
|
|
|
count++
|
|
|
|
}
|
|
|
|
return ies
|
|
|
|
}
|
|
|
|
|
2013-12-20 09:10:05 -05:00
|
|
|
// Returns an ordered index sorted by key name
|
|
|
|
func (i Index) Alphabetical() OrderedIndex {
|
|
|
|
name := func(i1, i2 *OrderedIndexEntry) bool {
|
2013-10-25 18:40:55 -04:00
|
|
|
return i1.Name < i2.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
ia := i.IndexArray()
|
2013-12-20 09:10:05 -05:00
|
|
|
OIby(name).Sort(ia)
|
2013-10-25 18:40:55 -04:00
|
|
|
return ia
|
|
|
|
}
|
|
|
|
|
2013-12-20 09:10:05 -05:00
|
|
|
// Returns an ordered index sorted by # of pages per key
|
|
|
|
func (i Index) ByCount() OrderedIndex {
|
|
|
|
count := func(i1, i2 *OrderedIndexEntry) bool {
|
2013-10-31 09:49:29 -04:00
|
|
|
return len(i1.WeightedPages) > len(i2.WeightedPages)
|
2013-10-25 18:40:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ia := i.IndexArray()
|
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
|
|
|
|
func (ie OrderedIndexEntry) Pages() []*Page {
|
2013-10-31 09:49:29 -04:00
|
|
|
return ie.WeightedPages.Pages()
|
|
|
|
}
|
|
|
|
|
2013-12-20 09:10:05 -05:00
|
|
|
func (ie OrderedIndexEntry) Count() int {
|
2013-10-31 09:49:29 -04:00
|
|
|
return len(ie.WeightedPages)
|
|
|
|
}
|
|
|
|
|
2013-12-20 09:10:05 -05:00
|
|
|
/*
|
|
|
|
* Implementation of a custom sorter for OrderedIndexes
|
|
|
|
*/
|
2013-10-31 09:49:29 -04:00
|
|
|
|
2013-12-20 09:10:05 -05:00
|
|
|
// A type to implement the sort interface for IndexEntries.
|
|
|
|
type orderedIndexSorter struct {
|
|
|
|
index OrderedIndex
|
|
|
|
by OIby
|
|
|
|
}
|
2013-10-25 18:40:55 -04:00
|
|
|
|
2013-12-20 09:10:05 -05:00
|
|
|
// Closure used in the Sort.Less method.
|
|
|
|
type OIby func(i1, i2 *OrderedIndexEntry) bool
|
|
|
|
|
|
|
|
func (by OIby) Sort(index OrderedIndex) {
|
|
|
|
ps := &orderedIndexSorter{
|
|
|
|
index: index,
|
|
|
|
by: by, // The Sort method's receiver is the function (closure) that defines the sort order.
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
2013-10-25 18:40:55 -04:00
|
|
|
sort.Sort(ps)
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
2013-10-25 18:40:55 -04:00
|
|
|
// Len is part of sort.Interface.
|
2013-12-20 09:10:05 -05:00
|
|
|
func (s *orderedIndexSorter) Len() int {
|
|
|
|
return len(s.index)
|
2013-10-25 18:40:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Swap is part of sort.Interface.
|
2013-12-20 09:10:05 -05:00
|
|
|
func (s *orderedIndexSorter) Swap(i, j int) {
|
|
|
|
s.index[i], s.index[j] = s.index[j], s.index[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.
|
2013-12-20 09:10:05 -05:00
|
|
|
func (s *orderedIndexSorter) Less(i, j int) bool {
|
|
|
|
return s.by(&s.index[i], &s.index[j])
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2013-12-20 09:10:05 -05:00
|
|
|
func (p WeightedPages) Len() int { return len(p) }
|
|
|
|
func (p WeightedPages) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
|
|
|
func (p WeightedPages) Sort() { sort.Sort(p) }
|
|
|
|
func (p WeightedPages) Count() int { return len(p) }
|
|
|
|
func (p WeightedPages) Less(i, j int) bool {
|
|
|
|
if p[i].Weight == p[j].Weight {
|
|
|
|
return p[i].Page.Date.Unix() > p[j].Page.Date.Unix()
|
|
|
|
} else {
|
|
|
|
return p[i].Weight < p[j].Weight
|
|
|
|
}
|
2013-11-24 16:10:20 -05:00
|
|
|
}
|
|
|
|
|
2013-12-20 09:10:05 -05:00
|
|
|
// TODO mimic PagesSorter for WeightedPages
|