diff options
author | spf13 <[email protected]> | 2013-10-24 15:24:47 -0700 |
---|---|---|
committer | spf13 <[email protected]> | 2013-10-24 15:24:47 -0700 |
commit | a5606b06ca6144542b4201a73c5b6c262f946ed3 (patch) | |
tree | 302dd52fb2396d2aec3afcbd864b7d91d2f5726a /hugolib | |
parent | 471fb1ff699473d7821812490b0ee472ecebb8b2 (diff) | |
download | hugo-a5606b06ca6144542b4201a73c5b6c262f946ed3.tar.gz hugo-a5606b06ca6144542b4201a73c5b6c262f946ed3.zip |
Adding support for ordering content in indexes by other than date (weighted indexes)
Diffstat (limited to 'hugolib')
-rw-r--r-- | hugolib/index.go | 14 | ||||
-rw-r--r-- | hugolib/site.go | 10 | ||||
-rw-r--r-- | hugolib/site_test.go | 69 |
3 files changed, 86 insertions, 7 deletions
diff --git a/hugolib/index.go b/hugolib/index.go index c4be4cee9..d4480ffd5 100644 --- a/hugolib/index.go +++ b/hugolib/index.go @@ -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 diff --git a/hugolib/site.go b/hugolib/site.go index f0905908d..c9ab1a262 100644 --- a/hugolib/site.go +++ b/hugolib/site.go @@ -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") diff --git a/hugolib/site_test.go b/hugolib/site_test.go index 8fad98a4c..e08f0d6d5 100644 --- a/hugolib/site_test.go +++ b/hugolib/site_test.go @@ -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) + } +} |