mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-29 22:51:32 -05:00
Adding support for ordering content in indexes by other than date (weighted indexes)
This commit is contained in:
parent
471fb1ff69
commit
a5606b06ca
3 changed files with 86 additions and 7 deletions
|
@ -30,7 +30,9 @@ type WeightedIndexEntry struct {
|
|||
|
||||
type IndexedPages []WeightedIndexEntry
|
||||
|
||||
func (p IndexedPages) Len() int { return len(p) }
|
||||
func (p IndexedPages) Len() int { return len(p) }
|
||||
func (p IndexedPages) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
||||
func (p IndexedPages) Sort() { sort.Sort(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()
|
||||
|
@ -38,10 +40,14 @@ func (p IndexedPages) Less(i, j int) bool {
|
|||
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) }
|
||||
func (ip IndexedPages) Pages() Pages {
|
||||
pages := make(Pages, len(ip))
|
||||
for i, _ := range ip {
|
||||
pages[i] = ip[i].Page
|
||||
}
|
||||
return pages
|
||||
}
|
||||
|
||||
type Index map[string]IndexedPages
|
||||
type IndexList map[string]Index
|
||||
|
|
|
@ -288,12 +288,16 @@ func (s *Site) BuildSiteMeta() (err error) {
|
|||
s.Indexes[plural] = make(Index)
|
||||
for _, p := range s.Pages {
|
||||
vals := p.GetParam(plural)
|
||||
weight := p.GetParam(plural + "_weight")
|
||||
if weight == nil {
|
||||
weight = 0
|
||||
}
|
||||
|
||||
if vals != nil {
|
||||
v, ok := vals.([]string)
|
||||
if ok {
|
||||
for _, idx := range v {
|
||||
x := WeightedIndexEntry{0, p}
|
||||
x := WeightedIndexEntry{weight.(int), p}
|
||||
|
||||
s.Indexes[plural].Add(idx, x)
|
||||
}
|
||||
|
@ -403,7 +407,7 @@ func (s *Site) RenderIndexes() error {
|
|||
n.RSSlink = permalink(s, url+".xml")
|
||||
n.Date = o[0].Page.Date
|
||||
n.Data[singular] = o
|
||||
n.Data["Pages"] = o
|
||||
n.Data["Pages"] = o.Pages()
|
||||
layout := "indexes/" + singular + ".html"
|
||||
|
||||
var base string
|
||||
|
@ -458,7 +462,7 @@ func (s *Site) RenderLists() error {
|
|||
n.Permalink = permalink(s, n.Url)
|
||||
n.RSSlink = permalink(s, section+".xml")
|
||||
n.Date = data[0].Page.Date
|
||||
n.Data["Pages"] = data
|
||||
n.Data["Pages"] = data.Pages()
|
||||
layout := "indexes/" + section + ".html"
|
||||
|
||||
err := s.render(n, section, layout, "_default/indexes.html")
|
||||
|
|
|
@ -380,3 +380,72 @@ func TestOrderedPages(t *testing.T) {
|
|||
t.Errorf("Pages in unexpected order. Second should be '%s', got '%s'", "Three", s.Sections["sect"][1].Page.Title)
|
||||
}
|
||||
}
|
||||
|
||||
var PAGE_WITH_WEIGHTED_INDEXES_2 = []byte(`+++
|
||||
tags = [ "a", "b", "c" ]
|
||||
tags_weight = 22
|
||||
categories = ["d"]
|
||||
title = "foo"
|
||||
categories_weight = 44
|
||||
+++
|
||||
Front Matter with weighted tags and categories`)
|
||||
|
||||
var PAGE_WITH_WEIGHTED_INDEXES_1 = []byte(`+++
|
||||
tags = [ "a" ]
|
||||
tags_weight = 33
|
||||
title = "bar"
|
||||
categories = [ "d", "e" ]
|
||||
categories_weight = 11
|
||||
alias = "spf13"
|
||||
date = 1979-05-27T07:32:00Z
|
||||
+++
|
||||
Front Matter with weighted tags and categories`)
|
||||
|
||||
var PAGE_WITH_WEIGHTED_INDEXES_3 = []byte(`+++
|
||||
title = "bza"
|
||||
categories = [ "e" ]
|
||||
categories_weight = 11
|
||||
alias = "spf13"
|
||||
date = 2010-05-27T07:32:00Z
|
||||
+++
|
||||
Front Matter with weighted tags and categories`)
|
||||
|
||||
func TestWeightedIndexes(t *testing.T) {
|
||||
files := make(map[string][]byte)
|
||||
target := &target.InMemoryTarget{Files: files}
|
||||
sources := []source.ByteSource{
|
||||
{"sect/doc1.md", PAGE_WITH_WEIGHTED_INDEXES_1, "sect"},
|
||||
{"sect/doc2.md", PAGE_WITH_WEIGHTED_INDEXES_2, "sect"},
|
||||
{"sect/doc3.md", PAGE_WITH_WEIGHTED_INDEXES_3, "sect"},
|
||||
}
|
||||
indexes := make(map[string]string)
|
||||
|
||||
indexes["tag"] = "tags"
|
||||
indexes["category"] = "categories"
|
||||
s := &Site{
|
||||
Target: target,
|
||||
Config: Config{BaseUrl: "http://auth/bub/", Indexes: indexes},
|
||||
Source: &source.InMemorySource{sources},
|
||||
}
|
||||
s.initializeSiteInfo()
|
||||
|
||||
if err := s.CreatePages(); err != nil {
|
||||
t.Fatalf("Unable to create pages: %s", err)
|
||||
}
|
||||
|
||||
if err := s.BuildSiteMeta(); err != nil {
|
||||
t.Fatalf("Unable to build site metadata: %s", err)
|
||||
}
|
||||
|
||||
if s.Indexes["tags"]["a"][0].Page.Title != "bar" {
|
||||
t.Errorf("Pages in unexpected order, 'bar' expected first, got '%v'", s.Indexes["tags"]["a"][0].Page.Title)
|
||||
}
|
||||
|
||||
if s.Indexes["categories"]["d"][0].Page.Title != "foo" {
|
||||
t.Errorf("Pages in unexpected order, 'foo' expected first, got '%v'", s.Indexes["categories"]["d"][0].Page.Title)
|
||||
}
|
||||
|
||||
if s.Indexes["categories"]["e"][0].Page.Title != "bza" {
|
||||
t.Errorf("Pages in unexpected order, 'bza' expected first, got '%v'", s.Indexes["categories"]["e"][0].Page.Title)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue