From 90090175f8084b81ffc24b782df72de34c467d76 Mon Sep 17 00:00:00 2001 From: spf13 Date: Thu, 17 Oct 2013 23:57:25 -0400 Subject: [PATCH] Adding preliminary support for weighted indexes (for ordering by other than date) --- hugolib/index.go | 30 +++++++++++++++++++++++++----- hugolib/site.go | 12 +++++++----- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/hugolib/index.go b/hugolib/index.go index f52c372b6..c4be4cee9 100644 --- a/hugolib/index.go +++ b/hugolib/index.go @@ -23,7 +23,27 @@ type IndexCount struct { Count int } -type Index map[string]Pages +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 type IndexList map[string]Index type OrderedIndex []IndexCount @@ -34,11 +54,11 @@ func kp(in string) string { return template.Urlize(in) } -func (i Index) Get(key string) Pages { return i[kp(key)] } -func (i Index) Count(key string) int { return len(i[kp(key)]) } -func (i Index) Add(key string, p *Page) { +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) { key = kp(key) - i[key] = append(i[key], p) + i[key] = append(i[key], w) } func (l IndexList) BuildOrderedIndexList() OrderedIndexList { diff --git a/hugolib/site.go b/hugolib/site.go index 57475df48..7894f6f9c 100644 --- a/hugolib/site.go +++ b/hugolib/site.go @@ -37,7 +37,7 @@ func MakePermalink(base *url.URL, path *url.URL) *url.URL { return base.ResolveReference(path) } -// Site contains all the information relevent for constructing a static +// Site contains all the information relevant for constructing a static // site. The basic flow of information is as follows: // // 1. A list of Files is parsed and then converted into Pages. @@ -293,7 +293,9 @@ func (s *Site) BuildSiteMeta() (err error) { v, ok := vals.([]string) if ok { for _, idx := range v { - s.Indexes[plural].Add(idx, p) + x := WeightedIndexEntry{0, p} + + s.Indexes[plural].Add(idx, x) } } else { if s.Config.Verbose { @@ -308,7 +310,7 @@ func (s *Site) BuildSiteMeta() (err error) { } for i, p := range s.Pages { - s.Sections.Add(p.Section, s.Pages[i]) + s.Sections.Add(p.Section, WeightedIndexEntry{0, s.Pages[i]}) } for k, _ := range s.Sections { @@ -399,7 +401,7 @@ func (s *Site) RenderIndexes() error { plink := n.Url n.Permalink = permalink(s, plink) n.RSSlink = permalink(s, url+".xml") - n.Date = o[0].Date + n.Date = o[0].Page.Date n.Data[singular] = o n.Data["Pages"] = o layout := "indexes/" + singular + ".html" @@ -455,7 +457,7 @@ func (s *Site) RenderLists() error { n.Url = helpers.Urlize(section + "/" + "index.html") n.Permalink = permalink(s, n.Url) n.RSSlink = permalink(s, section+".xml") - n.Date = data[0].Date + n.Date = data[0].Page.Date n.Data["Pages"] = data layout := "indexes/" + section + ".html"