mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-07 20:30:36 -05:00
Add support for weighted pages
Now pages can be sorted by other than date
This commit is contained in:
parent
90090175f8
commit
3558e3d6f0
4 changed files with 92 additions and 4 deletions
|
@ -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:
|
||||
|
|
|
@ -61,6 +61,7 @@ type PageMeta struct {
|
|||
WordCount int
|
||||
FuzzyWordCount int
|
||||
MinRead int
|
||||
Weight int
|
||||
}
|
||||
|
||||
type Position struct {
|
||||
|
@ -71,7 +72,14 @@ 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) 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
|
||||
|
@ -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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue