mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-07 20:30:36 -05:00
Adding preliminary support for weighted indexes (for ordering by other than date)
This commit is contained in:
parent
678ddef46a
commit
90090175f8
2 changed files with 32 additions and 10 deletions
|
@ -23,7 +23,27 @@ type IndexCount struct {
|
||||||
Count int
|
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 IndexList map[string]Index
|
||||||
|
|
||||||
type OrderedIndex []IndexCount
|
type OrderedIndex []IndexCount
|
||||||
|
@ -34,11 +54,11 @@ func kp(in string) string {
|
||||||
return template.Urlize(in)
|
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) 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)
|
key = kp(key)
|
||||||
i[key] = append(i[key], p)
|
i[key] = append(i[key], w)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l IndexList) BuildOrderedIndexList() OrderedIndexList {
|
func (l IndexList) BuildOrderedIndexList() OrderedIndexList {
|
||||||
|
|
|
@ -37,7 +37,7 @@ func MakePermalink(base *url.URL, path *url.URL) *url.URL {
|
||||||
return base.ResolveReference(path)
|
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:
|
// site. The basic flow of information is as follows:
|
||||||
//
|
//
|
||||||
// 1. A list of Files is parsed and then converted into Pages.
|
// 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)
|
v, ok := vals.([]string)
|
||||||
if ok {
|
if ok {
|
||||||
for _, idx := range v {
|
for _, idx := range v {
|
||||||
s.Indexes[plural].Add(idx, p)
|
x := WeightedIndexEntry{0, p}
|
||||||
|
|
||||||
|
s.Indexes[plural].Add(idx, x)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if s.Config.Verbose {
|
if s.Config.Verbose {
|
||||||
|
@ -308,7 +310,7 @@ func (s *Site) BuildSiteMeta() (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, p := range s.Pages {
|
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 {
|
for k, _ := range s.Sections {
|
||||||
|
@ -399,7 +401,7 @@ func (s *Site) RenderIndexes() error {
|
||||||
plink := n.Url
|
plink := n.Url
|
||||||
n.Permalink = permalink(s, plink)
|
n.Permalink = permalink(s, plink)
|
||||||
n.RSSlink = permalink(s, url+".xml")
|
n.RSSlink = permalink(s, url+".xml")
|
||||||
n.Date = o[0].Date
|
n.Date = o[0].Page.Date
|
||||||
n.Data[singular] = o
|
n.Data[singular] = o
|
||||||
n.Data["Pages"] = o
|
n.Data["Pages"] = o
|
||||||
layout := "indexes/" + singular + ".html"
|
layout := "indexes/" + singular + ".html"
|
||||||
|
@ -455,7 +457,7 @@ func (s *Site) RenderLists() error {
|
||||||
n.Url = helpers.Urlize(section + "/" + "index.html")
|
n.Url = helpers.Urlize(section + "/" + "index.html")
|
||||||
n.Permalink = permalink(s, n.Url)
|
n.Permalink = permalink(s, n.Url)
|
||||||
n.RSSlink = permalink(s, section+".xml")
|
n.RSSlink = permalink(s, section+".xml")
|
||||||
n.Date = data[0].Date
|
n.Date = data[0].Page.Date
|
||||||
n.Data["Pages"] = data
|
n.Data["Pages"] = data
|
||||||
layout := "indexes/" + section + ".html"
|
layout := "indexes/" + section + ".html"
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue