shithub: hugo

Download patch

ref: 3558e3d6f031a8a537750b20e4445699e9dba528
parent: 90090175f8084b81ffc24b782df72de34c467d76
author: spf13 <[email protected]>
date: Fri Oct 18 07:01:31 EDT 2013

Add support for weighted pages
Now pages can be sorted by other than date

--- a/hugolib/metadata.go
+++ b/hugolib/metadata.go
@@ -4,6 +4,7 @@
 	"errors"
 	"fmt"
 	"os"
+	"strconv"
 	"time"
 )
 
@@ -67,6 +68,24 @@
 	}
 
 	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 {
--- a/hugolib/page.go
+++ b/hugolib/page.go
@@ -61,6 +61,7 @@
 	WordCount      int
 	FuzzyWordCount int
 	MinRead        int
+	Weight         int
 }
 
 type Position struct {
@@ -70,10 +71,17 @@
 
 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) }
 func (p Pages) Limit(n int) Pages { return p[0:n] }
@@ -346,6 +354,8 @@
 			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 {
--- a/hugolib/site.go
+++ b/hugolib/site.go
@@ -310,7 +310,7 @@
 	}
 
 	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 {
--- a/hugolib/site_test.go
+++ b/hugolib/site_test.go
@@ -321,3 +321,62 @@
 		}
 	}
 }
+
+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)
+	}
+}