Skip to content

Commit

Permalink
hugolib: Enable nested sections
Browse files Browse the repository at this point in the history
Fixes #465
  • Loading branch information
bep committed Jun 8, 2017
1 parent bef5048 commit b396893
Show file tree
Hide file tree
Showing 11 changed files with 635 additions and 183 deletions.
32 changes: 5 additions & 27 deletions hugolib/hugo_sites.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,11 @@ func (h *HugoSites) createMissingPages() error {
}
}

// Will create content-less root sections.
newSections := s.assembleSections()
s.Pages = append(s.Pages, newSections...)
newPages = append(newPages, newSections...)

// taxonomy list and terms pages
taxonomies := s.Language.GetStringMapString("taxonomies")
if len(taxonomies) > 0 {
Expand Down Expand Up @@ -384,33 +389,6 @@ func (h *HugoSites) createMissingPages() error {
}
}
}

if s.isEnabled(KindSection) {
sectionPages := s.findPagesByKind(KindSection)
if len(sectionPages) < len(s.Sections) {
for name, section := range s.Sections {
// A section may be created for the root content folder if a
// content file is placed there.
// We cannot create a section node for that, because
// that would overwrite the home page.
if name == "" {
continue
}
foundSection := false
for _, sectionPage := range sectionPages {
if sectionPage.sections[0] == name {
foundSection = true
break
}
}
if !foundSection {
n := s.newSectionPage(name, section)
s.Pages = append(s.Pages, n)
newPages = append(newPages, n)
}
}
}
}
}

if len(newPages) > 0 {
Expand Down
30 changes: 16 additions & 14 deletions hugolib/menu_old_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,25 +402,27 @@ func doTestSectionPagesMenu(canonifyURLs bool, t *testing.T) {
"canonifyURLs", canonifyURLs,
)

require.Equal(t, 3, len(s.Sections))
sects := s.getPage(KindHome).Sections()

firstSectionPages := s.Sections["first"]
require.Equal(t, 3, len(sects))

firstSectionPages := s.getPage(KindSection, "first").Pages
require.Equal(t, 2, len(firstSectionPages))
secondSectionPages := s.Sections["second-section"]
secondSectionPages := s.getPage(KindSection, "second-section").Pages
require.Equal(t, 1, len(secondSectionPages))
fishySectionPages := s.Sections["fish-and-chips"]
fishySectionPages := s.getPage(KindSection, "Fish and Chips").Pages
require.Equal(t, 1, len(fishySectionPages))

nodeFirst := s.getPage(KindSection, "first")
require.NotNil(t, nodeFirst)
nodeSecond := s.getPage(KindSection, "second-section")
require.NotNil(t, nodeSecond)
nodeFishy := s.getPage(KindSection, "fish-and-chips")
require.Equal(t, "fish-and-chips", nodeFishy.sections[0])
nodeFishy := s.getPage(KindSection, "Fish and Chips")
require.Equal(t, "Fish and Chips", nodeFishy.sections[0])

firstSectionMenuEntry := findTestMenuEntryByID(s, "spm", "first")
secondSectionMenuEntry := findTestMenuEntryByID(s, "spm", "second-section")
fishySectionMenuEntry := findTestMenuEntryByID(s, "spm", "fish-and-chips")
fishySectionMenuEntry := findTestMenuEntryByID(s, "spm", "Fish and Chips")

require.NotNil(t, firstSectionMenuEntry)
require.NotNil(t, secondSectionMenuEntry)
Expand All @@ -436,19 +438,19 @@ func doTestSectionPagesMenu(canonifyURLs bool, t *testing.T) {
require.Equal(t, "Fish and Chips", fishySectionMenuEntry.Name)

for _, p := range firstSectionPages {
require.True(t, p.Page.HasMenuCurrent("spm", firstSectionMenuEntry))
require.False(t, p.Page.HasMenuCurrent("spm", secondSectionMenuEntry))
require.True(t, p.HasMenuCurrent("spm", firstSectionMenuEntry))
require.False(t, p.HasMenuCurrent("spm", secondSectionMenuEntry))
}

for _, p := range secondSectionPages {
require.False(t, p.Page.HasMenuCurrent("spm", firstSectionMenuEntry))
require.True(t, p.Page.HasMenuCurrent("spm", secondSectionMenuEntry))
require.False(t, p.HasMenuCurrent("spm", firstSectionMenuEntry))
require.True(t, p.HasMenuCurrent("spm", secondSectionMenuEntry))
}

for _, p := range fishySectionPages {
require.False(t, p.Page.HasMenuCurrent("spm", firstSectionMenuEntry))
require.False(t, p.Page.HasMenuCurrent("spm", secondSectionMenuEntry))
require.True(t, p.Page.HasMenuCurrent("spm", fishySectionMenuEntry))
require.False(t, p.HasMenuCurrent("spm", firstSectionMenuEntry))
require.False(t, p.HasMenuCurrent("spm", secondSectionMenuEntry))
require.True(t, p.HasMenuCurrent("spm", fishySectionMenuEntry))
}
}

Expand Down
123 changes: 66 additions & 57 deletions hugolib/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,16 @@ type Page struct {
// isn't accomanied by one.
sections []string

// Will only be set for sections and regular pages.
parent *Page

// When we create paginator pages, we create a copy of the original,
// but keep track of it here.
origOnCopy *Page

// Will only be set for section pages and the home page.
subSections Pages

s *Site

// Pulled over from old Node. TODO(bep) reorg and group (embed)
Expand Down Expand Up @@ -228,6 +238,9 @@ func (p *Page) createLayoutDescriptor() output.LayoutDescriptor {

switch p.Kind {
case KindSection:
// In Hugo 0.22 we introduce nested sections, but we still only
// use the first level to pick the correct template. This may change in
// the future.
section = p.sections[0]
case KindTaxonomy, KindTaxonomyTerm:
section = p.s.taxonomiesPluralSingular[p.sections[0]]
Expand Down Expand Up @@ -265,6 +278,11 @@ func (p *Page) IsHome() bool {
return p.Kind == KindHome
}

// IsSection returns whether this is a section page.
func (p *Page) IsSection() bool {
return p.Kind == KindSection
}

// IsPage returns whether this is a regular content page.
func (p *Page) IsPage() bool {
return p.Kind == KindPage
Expand Down Expand Up @@ -667,6 +685,9 @@ func (p *Page) Type() string {
return "page"
}

// Section returns the first path element below the content root. Note that
// since Hugo 0.22 we support nested sections, but this will always be the first
// element of any nested path.
func (p *Page) Section() string {
if p.Kind == KindSection {
return p.sections[0]
Expand Down Expand Up @@ -1100,10 +1121,6 @@ func (p *Page) HasMenuCurrent(menuID string, me *MenuEntry) bool {
if sectionPagesMenu != "" {
section := p.Section()

if !p.s.Info.preserveTaxonomyNames {
section = p.s.PathSpec.MakePathSanitized(section)
}

if section != "" && sectionPagesMenu == menuID && section == me.Identifier {
return true
}
Expand Down Expand Up @@ -1415,59 +1432,54 @@ func (p *Page) prepareLayouts() error {
}

func (p *Page) prepareData(s *Site) error {

var pages Pages

p.Data = make(map[string]interface{})
switch p.Kind {
case KindPage:
case KindHome:
pages = s.RegularPages
case KindSection:
sectionData, ok := s.Sections[p.Section()]
if !ok {
return fmt.Errorf("Data for section %s not found", p.Section())
}
pages = sectionData.Pages()
case KindTaxonomy:
plural := p.sections[0]
term := p.sections[1]

if s.Info.preserveTaxonomyNames {
if v, ok := s.taxonomiesOrigKey[fmt.Sprintf("%s-%s", plural, term)]; ok {
term = v
if p.Kind != KindSection {
var pages Pages
p.Data = make(map[string]interface{})

switch p.Kind {
case KindPage:
case KindHome:
pages = s.RegularPages
case KindTaxonomy:
plural := p.sections[0]
term := p.sections[1]

if s.Info.preserveTaxonomyNames {
if v, ok := s.taxonomiesOrigKey[fmt.Sprintf("%s-%s", plural, term)]; ok {
term = v
}
}
}

singular := s.taxonomiesPluralSingular[plural]
taxonomy := s.Taxonomies[plural].Get(term)

p.Data[singular] = taxonomy
p.Data["Singular"] = singular
p.Data["Plural"] = plural
p.Data["Term"] = term
pages = taxonomy.Pages()
case KindTaxonomyTerm:
plural := p.sections[0]
singular := s.taxonomiesPluralSingular[plural]

p.Data["Singular"] = singular
p.Data["Plural"] = plural
p.Data["Terms"] = s.Taxonomies[plural]
// keep the following just for legacy reasons
p.Data["OrderedIndex"] = p.Data["Terms"]
p.Data["Index"] = p.Data["Terms"]

// A list of all KindTaxonomy pages with matching plural
for _, p := range s.findPagesByKind(KindTaxonomy) {
if p.sections[0] == plural {
pages = append(pages, p)
singular := s.taxonomiesPluralSingular[plural]
taxonomy := s.Taxonomies[plural].Get(term)

p.Data[singular] = taxonomy
p.Data["Singular"] = singular
p.Data["Plural"] = plural
p.Data["Term"] = term
pages = taxonomy.Pages()
case KindTaxonomyTerm:
plural := p.sections[0]
singular := s.taxonomiesPluralSingular[plural]

p.Data["Singular"] = singular
p.Data["Plural"] = plural
p.Data["Terms"] = s.Taxonomies[plural]
// keep the following just for legacy reasons
p.Data["OrderedIndex"] = p.Data["Terms"]
p.Data["Index"] = p.Data["Terms"]

// A list of all KindTaxonomy pages with matching plural
for _, p := range s.findPagesByKind(KindTaxonomy) {
if p.sections[0] == plural {
pages = append(pages, p)
}
}
}
}

p.Data["Pages"] = pages
p.Pages = pages
p.Data["Pages"] = pages
p.Pages = pages
}

// Now we know enough to set missing dates on home page etc.
p.updatePageDates()
Expand Down Expand Up @@ -1736,11 +1748,8 @@ func (p *Page) setValuesForKind(s *Site) {
switch p.Kind {
case KindHome:
p.URLPath.URL = "/"
case KindSection:
p.URLPath.URL = "/" + p.sections[0] + "/"
case KindTaxonomy:
p.URLPath.URL = "/" + path.Join(p.sections...) + "/"
case KindTaxonomyTerm:
case KindPage:
default:
p.URLPath.URL = "/" + path.Join(p.sections...) + "/"
}
}
Expand Down
1 change: 0 additions & 1 deletion hugolib/page_collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ func (c *PageCollections) getPage(typ string, sections ...string) *Page {
key = path.Join(sections...)
}

// TODO(bep) section error
p, _ := c.pageCache.Get(typ, key)
if p == nil {
return nil
Expand Down
Loading

0 comments on commit b396893

Please sign in to comment.