Add support for weighted pages
authorspf13 <steve.francia@gmail.com>
Fri, 18 Oct 2013 15:01:31 +0000 (11:01 -0400)
committerspf13 <steve.francia@gmail.com>
Fri, 18 Oct 2013 15:01:31 +0000 (11:01 -0400)
Now pages can be sorted by other than date

hugolib/metadata.go
hugolib/page.go
hugolib/site.go
hugolib/site_test.go

index 3bb353a1582c6743063e0414ea7e0527b5811b61..3441a991a4a84b733328b7c2db96ecd41c5f6f84 100644 (file)
@@ -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:
index 56b604407bc664c3974a62bf93d612c075020346..313503a7a771df6221e4c3326d4a05c914f5af2d 100644 (file)
@@ -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 {
index 7894f6f9cd42fcb25b192269c075042ccea410cd..f0905908d2dde80a11ef419ccfb83428d718335c 100644 (file)
@@ -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 {
index acf3fd56a44c45f72a62c8f4d3a64e3e6f65df2b..8fad98a4c486b1e44a7fe7172a33fb579e997561 100644 (file)
@@ -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)
+       }
+}