summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorspf13 <steve.francia@gmail.com>2013-10-25 18:40:55 -0400
committerspf13 <steve.francia@gmail.com>2013-10-25 18:40:55 -0400
commitd20b41a2cf5e1f88408de54c58a2db06926987fc (patch)
tree3b8f2948feedaffe6db4fa587a10e32fe69b3c0e
parent9388f236062c97e44b3964e4097e7bf34658d8b8 (diff)
Big index overhaul. Now supporting ordering tokens by count or alphabetically. Also made full indexes available to the Site variable.
-rw-r--r--hugolib/index.go84
-rw-r--r--hugolib/page_index_test.go6
-rw-r--r--hugolib/site.go4
3 files changed, 66 insertions, 28 deletions
diff --git a/hugolib/index.go b/hugolib/index.go
index d4480ffd5..4150d87e3 100644
--- a/hugolib/index.go
+++ b/hugolib/index.go
@@ -18,11 +18,6 @@ import (
"sort"
)
-type IndexCount struct {
- Name string
- Count int
-}
-
type WeightedIndexEntry struct {
Weight int
Page *Page
@@ -52,9 +47,6 @@ func (ip IndexedPages) Pages() Pages {
type Index map[string]IndexedPages
type IndexList map[string]Index
-type OrderedIndex []IndexCount
-type OrderedIndexList map[string]OrderedIndex
-
// KeyPrep... Indexes should be case insensitive. Can make it easily conditional later.
func kp(in string) string {
return template.Urlize(in)
@@ -67,21 +59,67 @@ func (i Index) Add(key string, w WeightedIndexEntry) {
i[key] = append(i[key], w)
}
-func (l IndexList) BuildOrderedIndexList() OrderedIndexList {
- oil := make(OrderedIndexList, len(l))
- for idx_name, index := range l {
- i := 0
- oi := make(OrderedIndex, len(index))
- for name, pages := range index {
- oi[i] = IndexCount{name, len(pages)}
- i++
- }
- sort.Sort(oi)
- oil[idx_name] = oi
+func (i Index) IndexArray() []IndexEntry {
+ ies := make([]IndexEntry, len(i))
+ count := 0
+ for k, v := range i {
+ ies[count] = IndexEntry{Name: k, Pages: v}
+ count++
+ }
+ return ies
+}
+
+func (i Index) Alphabetical() []IndexEntry {
+ name := func(i1, i2 *IndexEntry) bool {
+ return i1.Name < i2.Name
+ }
+
+ ia := i.IndexArray()
+ By(name).Sort(ia)
+ return ia
+}
+
+func (i Index) ByCount() []IndexEntry {
+ count := func(i1, i2 *IndexEntry) bool {
+ return len(i1.Pages) < len(i2.Pages)
+ }
+
+ ia := i.IndexArray()
+ By(count).Sort(ia)
+ return ia
+}
+
+type IndexEntry struct {
+ Name string
+ Pages IndexedPages
+}
+
+type By func(i1, i2 *IndexEntry) bool
+
+func (by By) Sort(indexEntrys []IndexEntry) {
+ ps := &indexEntrySorter{
+ indexEntrys: indexEntrys,
+ by: by, // The Sort method's receiver is the function (closure) that defines the sort order.
}
- return oil
+ sort.Sort(ps)
}
-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] }
+type indexEntrySorter struct {
+ indexEntrys []IndexEntry
+ by func(p1, p2 *IndexEntry) bool // Closure used in the Less method.
+}
+
+// Len is part of sort.Interface.
+func (s *indexEntrySorter) Len() int {
+ return len(s.indexEntrys)
+}
+
+// Swap is part of sort.Interface.
+func (s *indexEntrySorter) Swap(i, j int) {
+ s.indexEntrys[i], s.indexEntrys[j] = s.indexEntrys[j], s.indexEntrys[i]
+}
+
+// Less is part of sort.Interface. It is implemented by calling the "by" closure in the sorter.
+func (s *indexEntrySorter) Less(i, j int) bool {
+ return s.by(&s.indexEntrys[i], &s.indexEntrys[j])
+}
diff --git a/hugolib/page_index_test.go b/hugolib/page_index_test.go
index c98003108..0edb0484a 100644
--- a/hugolib/page_index_test.go
+++ b/hugolib/page_index_test.go
@@ -12,7 +12,7 @@ categories: 'd'
YAML frontmatter with tags and categories index.`
var PAGE_YAML_WITH_INDEXES_B = `---
-tags:
+tags:
- "a"
- "b"
- "c"
@@ -23,8 +23,8 @@ YAML frontmatter with tags and categories index.`
var PAGE_JSON_WITH_INDEXES = `{
"categories": "d",
"tags": [
- "a",
- "b",
+ "a",
+ "b",
"c"
]
}
diff --git a/hugolib/site.go b/hugolib/site.go
index c97419416..dc4214f2b 100644
--- a/hugolib/site.go
+++ b/hugolib/site.go
@@ -73,7 +73,7 @@ type Site struct {
type SiteInfo struct {
BaseUrl template.URL
- Indexes OrderedIndexList
+ Indexes IndexList
Recent *Pages
LastChange time.Time
Title string
@@ -330,7 +330,7 @@ func (s *Site) BuildSiteMeta() (err error) {
s.Sections[k].Sort()
}
- s.Info.Indexes = s.Indexes.BuildOrderedIndexList()
+ s.Info.Indexes = s.Indexes
if len(s.Pages) == 0 {
return