shithub: hugo

Download patch

ref: 90090175f8084b81ffc24b782df72de34c467d76
parent: 678ddef46a2cd0f8968da78ca80b5fb5aa14d7d2
author: spf13 <[email protected]>
date: Thu Oct 17 19:57:25 EDT 2013

Adding preliminary support for weighted indexes (for ordering by other than date)

--- a/hugolib/index.go
+++ b/hugolib/index.go
@@ -23,7 +23,27 @@
 	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 @@
 	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 {
--- a/hugolib/site.go
+++ b/hugolib/site.go
@@ -37,7 +37,7 @@
 	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 @@
 				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 @@
 	}
 
 	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 @@
 			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 @@
 		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"