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

This commit is contained in:
spf13 2013-10-17 23:57:25 -04:00
parent 678ddef46a
commit 90090175f8
2 changed files with 32 additions and 10 deletions

View file

@ -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) 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, p *Page) {
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 {

View file

@ -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"