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-09-03 18:38:20 -04:00
|
|
|
"github.com/spf13/hugo/template"
|
2013-07-04 11:32:55 -04:00
|
|
|
"sort"
|
|
|
|
)
|
|
|
|
|
2013-08-03 03:09:28 -04:00
|
|
|
type IndexCount struct {
|
|
|
|
Name string
|
|
|
|
Count int
|
|
|
|
}
|
|
|
|
|
2013-10-17 23:57:25 -04:00
|
|
|
type WeightedIndexEntry struct {
|
|
|
|
Weight int
|
|
|
|
Page *Page
|
|
|
|
}
|
|
|
|
|
|
|
|
type IndexedPages []WeightedIndexEntry
|
|
|
|
|
|
|
|
func (p IndexedPages) Len() int { return len(p) }
|
|
|
|
func (p IndexedPages) 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (p IndexedPages) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
|
|
|
|
|
|
|
// TODO eliminate unnecessary things
|
|
|
|
func (p IndexedPages) Sort() { sort.Sort(p) }
|
|
|
|
|
|
|
|
type Index map[string]IndexedPages
|
2013-07-04 11:32:55 -04:00
|
|
|
type IndexList map[string]Index
|
|
|
|
|
2013-08-03 03:09:28 -04:00
|
|
|
type OrderedIndex []IndexCount
|
2013-07-04 11:32:55 -04:00
|
|
|
type OrderedIndexList map[string]OrderedIndex
|
|
|
|
|
|
|
|
// KeyPrep... Indexes should be case insensitive. Can make it easily conditional later.
|
|
|
|
func kp(in string) string {
|
2013-09-03 18:38:20 -04:00
|
|
|
return template.Urlize(in)
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
2013-10-17 23:57:25 -04:00
|
|
|
func (i Index) Get(key string) IndexedPages { return i[kp(key)] }
|
|
|
|
func (i Index) Count(key string) int { return len(i[kp(key)]) }
|
|
|
|
func (i Index) Add(key string, w WeightedIndexEntry) {
|
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-08-03 03:09:28 -04:00
|
|
|
func (l IndexList) BuildOrderedIndexList() OrderedIndexList {
|
2013-07-04 11:32:55 -04:00
|
|
|
oil := make(OrderedIndexList, len(l))
|
|
|
|
for idx_name, index := range l {
|
|
|
|
i := 0
|
|
|
|
oi := make(OrderedIndex, len(index))
|
2013-08-03 03:09:28 -04:00
|
|
|
for name, pages := range index {
|
|
|
|
oi[i] = IndexCount{name, len(pages)}
|
2013-07-04 11:32:55 -04:00
|
|
|
i++
|
|
|
|
}
|
2013-08-03 03:09:28 -04:00
|
|
|
sort.Sort(oi)
|
2013-07-04 11:32:55 -04:00
|
|
|
oil[idx_name] = oi
|
|
|
|
}
|
2013-08-03 03:09:28 -04:00
|
|
|
return oil
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
2013-08-03 03:09:28 -04:00
|
|
|
func (idx OrderedIndex) Len() int { return len(idx) }
|
|
|
|
func (idx OrderedIndex) Less(i, j int) bool { return idx[i].Count > idx[j].Count }
|
|
|
|
func (idx OrderedIndex) Swap(i, j int) { idx[i], idx[j] = idx[j], idx[i] }
|