ref: 7ad721fd78b473ae7d9b05418bb029c51d1d9251
parent: 5714531f34469234035323bc9e1cc9de33a8f839
author: Bjørn Erik Pedersen <[email protected]>
date: Fri Apr 7 09:03:34 EDT 2017
hugolib: Add .Site.Params.mainSections Fixes #3206
--- a/hugolib/site.go
+++ b/hugolib/site.go
@@ -1698,6 +1698,36 @@
wp.Page.PrevInSection = s.Sections[k][i+1].Page
}
}
+
+ }
+
+ var (
+ sectionsParamId = "mainSections"
+ sectionsParamIdLower = strings.ToLower(sectionsParamId)
+ mainSections interface{}
+ found bool
+ )
+
+ if mainSections, found = s.Info.Params[sectionsParamIdLower]; !found {
+ // Pick the section with most regular pages
+ var (
+ chosenSection string
+ pageCount int
+ )
+
+ for sect, pages := range s.Sections {
+ if pages.Count() >= pageCount {
+ chosenSection = sect
+ pageCount = pages.Count()
+ }
+ }
+ mainSections = []string{chosenSection}
+
+ // Try to make this as backwards compatible as possible.
+ s.Info.Params[sectionsParamId] = mainSections
+ s.Info.Params[sectionsParamIdLower] = mainSections
+ } else {
+ s.Info.Params[sectionsParamId] = mainSections
}
}
--- a/hugolib/site_test.go
+++ b/hugolib/site_test.go
@@ -400,6 +400,8 @@
sources := []source.ByteSource{
{Name: filepath.FromSlash("sect/doc1.html"), Content: []byte("doc1")},
+ // Add one more page to sect to make sure sect is picked in mainSections
+ {Name: filepath.FromSlash("sect/sect.html"), Content: []byte("sect")},
{Name: filepath.FromSlash("Fish and Chips/doc2.html"), Content: []byte("doc2")},
{Name: filepath.FromSlash("ラーメン/doc3.html"), Content: []byte("doc3")},
}
@@ -419,6 +421,11 @@
writeSource(t, fs, filepath.Join("layouts", "_default/list.html"), "{{.Title}}")
s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{})
+
+ mainSections, err := s.Info.Param("mainSections")
+ require.NoError(t, err)
+ require.Equal(t, mainSections, []string{"sect"})
+
th := testHelper{s.Cfg, s.Fs, t}
tests := []struct {
doc string