Adding support for ordering content in indexes by other than date (weighted indexes)
authorspf13 <steve.francia@gmail.com>
Thu, 24 Oct 2013 22:24:47 +0000 (15:24 -0700)
committerspf13 <steve.francia@gmail.com>
Thu, 24 Oct 2013 22:24:47 +0000 (15:24 -0700)
hugolib/index.go
hugolib/site.go
hugolib/site_test.go

index c4be4cee9d48cf0282066551cf593e58a9414608..d4480ffd55aec6969449eaf9573af299024365fd 100644 (file)
@@ -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
index f0905908d2dde80a11ef419ccfb83428d718335c..c9ab1a26213ae100af0e7aff3d1e7ff9b3925910 100644 (file)
@@ -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")
index 8fad98a4c486b1e44a7fe7172a33fb579e997561..e08f0d6d595c12d20a0cad2bbbb6cb3ce9bdbd99 100644 (file)
@@ -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)
+       }
+}