2019-01-02 06:33:26 -05:00
|
|
|
// Copyright 2019 The Hugo Authors. All rights reserved.
|
2017-04-09 04:33:04 -04:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package hugolib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
"github.com/gohugoio/hugo/resources/page"
|
|
|
|
"github.com/gohugoio/hugo/resources/resource"
|
2017-07-02 14:14:06 -04:00
|
|
|
|
2017-06-13 13:07:35 -04:00
|
|
|
radix "github.com/hashicorp/go-immutable-radix"
|
2017-04-09 04:33:04 -04:00
|
|
|
)
|
|
|
|
|
2017-07-02 12:20:14 -04:00
|
|
|
// Sections returns the top level sections.
|
2019-01-02 06:33:26 -05:00
|
|
|
func (s *SiteInfo) Sections() page.Pages {
|
2017-07-02 12:20:14 -04:00
|
|
|
home, err := s.Home()
|
|
|
|
if err == nil {
|
|
|
|
return home.Sections()
|
|
|
|
}
|
|
|
|
return nil
|
2017-04-09 04:33:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Home is a shortcut to the home page, equivalent to .Site.GetPage "home".
|
2019-01-02 06:33:26 -05:00
|
|
|
func (s *SiteInfo) Home() (page.Page, error) {
|
|
|
|
return s.s.home, nil
|
2017-04-09 04:33:04 -04:00
|
|
|
}
|
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
func (s *Site) assembleSections() pageStatePages {
|
|
|
|
var newPages pageStatePages
|
2018-11-28 06:36:59 -05:00
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
if !s.isEnabled(page.KindSection) {
|
2017-04-09 04:33:04 -04:00
|
|
|
return newPages
|
|
|
|
}
|
|
|
|
|
|
|
|
// Maps section kind pages to their path, i.e. "my/section"
|
2019-01-02 06:33:26 -05:00
|
|
|
sectionPages := make(map[string]*pageState)
|
2017-04-09 04:33:04 -04:00
|
|
|
|
|
|
|
// The sections with content files will already have been created.
|
2019-01-02 06:33:26 -05:00
|
|
|
for _, sect := range s.findWorkPagesByKind(page.KindSection) {
|
|
|
|
sectionPages[sect.SectionsPath()] = sect
|
2017-04-09 04:33:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
sectKey = "__hs"
|
|
|
|
sectSectKey = "_a" + sectKey
|
|
|
|
sectPageKey = "_b" + sectKey
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
inPages = radix.New().Txn()
|
|
|
|
inSections = radix.New().Txn()
|
2019-01-02 06:33:26 -05:00
|
|
|
undecided pageStatePages
|
2017-04-09 04:33:04 -04:00
|
|
|
)
|
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
home := s.findFirstWorkPageByKindIn(page.KindHome)
|
2018-02-25 01:15:03 -05:00
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
for i, p := range s.workAllPages {
|
|
|
|
|
|
|
|
if p.Kind() != page.KindPage {
|
2017-04-09 04:33:04 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
sections := p.SectionsEntries()
|
|
|
|
|
|
|
|
if len(sections) == 0 {
|
2017-04-09 04:33:04 -04:00
|
|
|
// Root level pages. These will have the home page as their Parent.
|
|
|
|
p.parent = home
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
sectionKey := p.SectionsPath()
|
|
|
|
_, found := sectionPages[sectionKey]
|
|
|
|
|
|
|
|
if !found && len(sections) == 1 {
|
2017-04-09 04:33:04 -04:00
|
|
|
|
|
|
|
// We only create content-file-less sections for the root sections.
|
2019-01-02 06:33:26 -05:00
|
|
|
n := s.newPage(page.KindSection, sections[0])
|
|
|
|
|
|
|
|
sectionPages[sectionKey] = n
|
|
|
|
newPages = append(newPages, n)
|
2017-06-13 05:26:17 -04:00
|
|
|
found = true
|
2017-04-09 04:33:04 -04:00
|
|
|
}
|
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
if len(sections) > 1 {
|
2017-06-13 05:26:17 -04:00
|
|
|
// Create the root section if not found.
|
2019-01-02 06:33:26 -05:00
|
|
|
_, rootFound := sectionPages[sections[0]]
|
2017-06-13 05:26:17 -04:00
|
|
|
if !rootFound {
|
2019-01-02 06:33:26 -05:00
|
|
|
sect := s.newPage(page.KindSection, sections[0])
|
|
|
|
sectionPages[sections[0]] = sect
|
2017-06-13 05:26:17 -04:00
|
|
|
newPages = append(newPages, sect)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if found {
|
|
|
|
pagePath := path.Join(sectionKey, sectPageKey, strconv.Itoa(i))
|
|
|
|
inPages.Insert([]byte(pagePath), p)
|
|
|
|
} else {
|
|
|
|
undecided = append(undecided, p)
|
|
|
|
}
|
2017-04-09 04:33:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create any missing sections in the tree.
|
|
|
|
// A sub-section needs a content file, but to create a navigational tree,
|
|
|
|
// given a content file in /content/a/b/c/_index.md, we cannot create just
|
|
|
|
// the c section.
|
|
|
|
for _, sect := range sectionPages {
|
2019-01-02 06:33:26 -05:00
|
|
|
sections := sect.SectionsEntries()
|
|
|
|
for i := len(sections); i > 0; i-- {
|
|
|
|
sectionPath := sections[:i]
|
2017-04-09 04:33:04 -04:00
|
|
|
sectionKey := path.Join(sectionPath...)
|
2019-01-02 06:33:26 -05:00
|
|
|
_, found := sectionPages[sectionKey]
|
2017-04-09 04:33:04 -04:00
|
|
|
if !found {
|
2019-01-02 06:33:26 -05:00
|
|
|
sect = s.newPage(page.KindSection, sectionPath[len(sectionPath)-1])
|
|
|
|
sect.m.sections = sectionPath
|
2017-04-09 04:33:04 -04:00
|
|
|
sectionPages[sectionKey] = sect
|
|
|
|
newPages = append(newPages, sect)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, sect := range sectionPages {
|
|
|
|
inPages.Insert([]byte(path.Join(k, sectSectKey)), sect)
|
|
|
|
inSections.Insert([]byte(k), sect)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2019-01-02 06:33:26 -05:00
|
|
|
currentSection *pageState
|
|
|
|
children page.Pages
|
|
|
|
dates *resource.Dates
|
2017-04-09 04:33:04 -04:00
|
|
|
rootSections = inSections.Commit().Root()
|
|
|
|
)
|
|
|
|
|
2017-06-13 05:26:17 -04:00
|
|
|
for i, p := range undecided {
|
|
|
|
// Now we can decide where to put this page into the tree.
|
2019-01-02 06:33:26 -05:00
|
|
|
sectionKey := p.SectionsPath()
|
|
|
|
|
2017-06-13 05:26:17 -04:00
|
|
|
_, v, _ := rootSections.LongestPrefix([]byte(sectionKey))
|
2019-01-02 06:33:26 -05:00
|
|
|
sect := v.(*pageState)
|
|
|
|
pagePath := path.Join(path.Join(sect.SectionsEntries()...), sectSectKey, "u", strconv.Itoa(i))
|
2017-06-13 05:26:17 -04:00
|
|
|
inPages.Insert([]byte(pagePath), p)
|
|
|
|
}
|
|
|
|
|
|
|
|
var rootPages = inPages.Commit().Root()
|
|
|
|
|
2017-04-09 04:33:04 -04:00
|
|
|
rootPages.Walk(func(path []byte, v interface{}) bool {
|
2019-01-02 06:33:26 -05:00
|
|
|
p := v.(*pageState)
|
2017-04-09 04:33:04 -04:00
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
if p.Kind() == page.KindSection {
|
2017-04-09 04:33:04 -04:00
|
|
|
if currentSection != nil {
|
|
|
|
// A new section
|
2019-01-02 06:33:26 -05:00
|
|
|
currentSection.setPages(children)
|
2019-04-13 12:58:06 -04:00
|
|
|
if dates != nil {
|
|
|
|
currentSection.m.Dates = *dates
|
|
|
|
}
|
2017-04-09 04:33:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
currentSection = p
|
2019-01-02 06:33:26 -05:00
|
|
|
children = make(page.Pages, 0)
|
2019-04-13 12:58:06 -04:00
|
|
|
dates = nil
|
|
|
|
// Use section's dates from front matter if set.
|
|
|
|
if resource.IsZeroDates(currentSection) {
|
|
|
|
dates = &resource.Dates{}
|
|
|
|
}
|
2017-04-09 04:33:04 -04:00
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Regular page
|
|
|
|
p.parent = currentSection
|
|
|
|
children = append(children, p)
|
2019-04-13 12:58:06 -04:00
|
|
|
if dates != nil {
|
|
|
|
dates.UpdateDateAndLastmodIfAfter(p)
|
|
|
|
}
|
2019-04-05 05:05:25 -04:00
|
|
|
|
2017-04-09 04:33:04 -04:00
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
|
|
|
if currentSection != nil {
|
2019-01-02 06:33:26 -05:00
|
|
|
currentSection.setPages(children)
|
2019-04-13 12:58:06 -04:00
|
|
|
if dates != nil {
|
|
|
|
currentSection.m.Dates = *dates
|
|
|
|
}
|
2017-04-09 04:33:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build the sections hierarchy
|
|
|
|
for _, sect := range sectionPages {
|
2019-01-02 06:33:26 -05:00
|
|
|
sections := sect.SectionsEntries()
|
|
|
|
if len(sections) == 1 {
|
|
|
|
if home != nil {
|
|
|
|
sect.parent = home
|
|
|
|
}
|
2017-04-09 04:33:04 -04:00
|
|
|
} else {
|
2019-01-02 06:33:26 -05:00
|
|
|
parentSearchKey := path.Join(sect.SectionsEntries()[:len(sections)-1]...)
|
2017-04-09 04:33:04 -04:00
|
|
|
_, v, _ := rootSections.LongestPrefix([]byte(parentSearchKey))
|
2019-01-02 06:33:26 -05:00
|
|
|
p := v.(*pageState)
|
2017-04-09 04:33:04 -04:00
|
|
|
sect.parent = p
|
|
|
|
}
|
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
sect.addSectionToParent()
|
2017-04-09 04:33:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
sectionsParamId = "mainSections"
|
|
|
|
sectionsParamIdLower = strings.ToLower(sectionsParamId)
|
|
|
|
mainSections interface{}
|
|
|
|
mainSectionsFound bool
|
|
|
|
maxSectionWeight int
|
|
|
|
)
|
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
mainSections, mainSectionsFound = s.Info.Params()[sectionsParamIdLower]
|
2017-04-09 04:33:04 -04:00
|
|
|
|
|
|
|
for _, sect := range sectionPages {
|
2019-01-02 06:33:26 -05:00
|
|
|
sect.sortParentSections()
|
2017-04-09 04:33:04 -04:00
|
|
|
|
|
|
|
if !mainSectionsFound {
|
2019-01-02 06:33:26 -05:00
|
|
|
weight := len(sect.Pages()) + (len(sect.Sections()) * 5)
|
2017-04-09 04:33:04 -04:00
|
|
|
if weight >= maxSectionWeight {
|
|
|
|
mainSections = []string{sect.Section()}
|
|
|
|
maxSectionWeight = weight
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to make this as backwards compatible as possible.
|
2019-01-02 06:33:26 -05:00
|
|
|
s.Info.Params()[sectionsParamId] = mainSections
|
|
|
|
s.Info.Params()[sectionsParamIdLower] = mainSections
|
2017-04-09 04:33:04 -04:00
|
|
|
|
|
|
|
return newPages
|
|
|
|
|
|
|
|
}
|