From 3558e3d6f031a8a537750b20e4445699e9dba528 Mon Sep 17 00:00:00 2001 From: spf13 Date: Fri, 18 Oct 2013 11:01:31 -0400 Subject: [PATCH] Add support for weighted pages Now pages can be sorted by other than date --- hugolib/metadata.go | 19 ++++++++++++++ hugolib/page.go | 16 +++++++++--- hugolib/site.go | 2 +- hugolib/site_test.go | 59 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 92 insertions(+), 4 deletions(-) diff --git a/hugolib/metadata.go b/hugolib/metadata.go index 3bb353a15..3441a991a 100644 --- a/hugolib/metadata.go +++ b/hugolib/metadata.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "os" + "strconv" "time" ) @@ -69,6 +70,24 @@ func interfaceArrayToStringArray(i interface{}) []string { return a } +func interfaceToInt(i interface{}) int { + switch s := i.(type) { + case int: + return s + case string: + v, err := strconv.ParseInt(s, 0, 0) + if err == nil { + return int(v) + } else { + errorf("Only Ints are supported for this key\nErr:", err) + } + default: + errorf("Only Ints are supported for this key") + } + + return 0 +} + func interfaceToString(i interface{}) string { switch s := i.(type) { case string: diff --git a/hugolib/page.go b/hugolib/page.go index 56b604407..313503a7a 100644 --- a/hugolib/page.go +++ b/hugolib/page.go @@ -61,6 +61,7 @@ type PageMeta struct { WordCount int FuzzyWordCount int MinRead int + Weight int } type Position struct { @@ -70,9 +71,16 @@ type Position struct { type Pages []*Page -func (p Pages) Len() int { return len(p) } -func (p Pages) Less(i, j int) bool { return p[i].Date.Unix() > p[j].Date.Unix() } -func (p Pages) Swap(i, j int) { p[i], p[j] = p[j], p[i] } +func (p Pages) Len() int { return len(p) } +func (p Pages) Less(i, j int) bool { + if p[i].Weight == p[j].Weight { + return p[i].Date.Unix() > p[j].Date.Unix() + } else { + return p[i].Weight > p[j].Weight + } +} + +func (p Pages) Swap(i, j int) { p[i], p[j] = p[j], p[i] } // TODO eliminate unnecessary things func (p Pages) Sort() { sort.Sort(p) } @@ -346,6 +354,8 @@ func (page *Page) update(f interface{}) error { page.layout = interfaceToString(v) case "markup": page.Markup = interfaceToString(v) + case "weight": + page.Weight = interfaceToInt(v) case "aliases": page.Aliases = interfaceArrayToStringArray(v) for _, alias := range page.Aliases { diff --git a/hugolib/site.go b/hugolib/site.go index 7894f6f9c..f0905908d 100644 --- a/hugolib/site.go +++ b/hugolib/site.go @@ -310,7 +310,7 @@ func (s *Site) BuildSiteMeta() (err error) { } for i, p := range s.Pages { - s.Sections.Add(p.Section, WeightedIndexEntry{0, s.Pages[i]}) + s.Sections.Add(p.Section, WeightedIndexEntry{s.Pages[i].Weight, s.Pages[i]}) } for k, _ := range s.Sections { diff --git a/hugolib/site_test.go b/hugolib/site_test.go index acf3fd56a..8fad98a4c 100644 --- a/hugolib/site_test.go +++ b/hugolib/site_test.go @@ -321,3 +321,62 @@ func TestAbsUrlify(t *testing.T) { } } } + +var WEIGHTED_PAGE_1 = []byte(`+++ +weight = "2" +title = "One" ++++ +Front Matter with Ordered Pages`) + +var WEIGHTED_PAGE_2 = []byte(`+++ +weight = "6" +title = "Two" ++++ +Front Matter with Ordered Pages 2`) + +var WEIGHTED_PAGE_3 = []byte(`+++ +weight = "4" +title = "Three" +date = "2012-04-06" ++++ +Front Matter with Ordered Pages 3`) + +var WEIGHTED_PAGE_4 = []byte(`+++ +weight = "4" +title = "Four" +date = "2012-01-01" ++++ +Front Matter with Ordered Pages 4`) + +func TestOrderedPages(t *testing.T) { + files := make(map[string][]byte) + target := &target.InMemoryTarget{Files: files} + sources := []source.ByteSource{ + {"sect/doc1.md", WEIGHTED_PAGE_1, "sect"}, + {"sect/doc2.md", WEIGHTED_PAGE_2, "sect"}, + {"sect/doc3.md", WEIGHTED_PAGE_3, "sect"}, + {"sect/doc4.md", WEIGHTED_PAGE_4, "sect"}, + } + s := &Site{ + Target: target, + Config: Config{BaseUrl: "http://auth/bub/"}, + 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.Sections["sect"][0].Weight != 6 || s.Sections["sect"][3].Weight != 2 { + t.Errorf("Pages in unexpected order. First should be '%s', got '%s'", 6, s.Sections["sect"][0].Weight) + } + + if s.Sections["sect"][1].Page.Title != "Three" || s.Sections["sect"][2].Page.Title != "Four" { + t.Errorf("Pages in unexpected order. Second should be '%s', got '%s'", "Three", s.Sections["sect"][1].Page.Title) + } +}