Skip to content

Commit

Permalink
Create pages from _content.gotmpl
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Apr 26, 2024
1 parent 1961327 commit b8908e5
Show file tree
Hide file tree
Showing 20 changed files with 535 additions and 133 deletions.
39 changes: 24 additions & 15 deletions common/paths/pathparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ func (pp *PathParser) parse(component, s string) (*Path, error) {
var err error
// Preserve the original case for titles etc.
p.unnormalized, err = pp.doParse(component, s, pp.newPath(component))

if err != nil {
return nil, err
}
Expand Down Expand Up @@ -195,23 +194,26 @@ func (pp *PathParser) doParse(component, s string, p *Path) (*Path, error) {
}
}

isContentComponent := p.component == files.ComponentFolderContent || p.component == files.ComponentFolderArchetypes
isContent := isContentComponent && files.IsContentExt(p.Ext())

if isContent {
if len(p.identifiers) > 0 {
isContentComponent := p.component == files.ComponentFolderContent || p.component == files.ComponentFolderArchetypes
isContent := isContentComponent && files.IsContentExt(p.Ext())
id := p.identifiers[len(p.identifiers)-1]
b := p.s[p.posContainerHigh : id.Low-1]
switch b {
case "index":
p.bundleType = PathTypeLeaf
case "_index":
p.bundleType = PathTypeBranch
default:
p.bundleType = PathTypeContentSingle
}
if isContent {
switch b {
case "index":
p.bundleType = PathTypeLeaf
case "_index":
p.bundleType = PathTypeBranch
default:
p.bundleType = PathTypeContentSingle
}

if slashCount == 2 && p.IsLeafBundle() {
p.posSectionHigh = 0
if slashCount == 2 && p.IsLeafBundle() {
p.posSectionHigh = 0
}
} else if b == files.NameContentData && files.IsContentDataExt(p.Ext()) {
p.bundleType = PathTypeContentData
}
}

Expand Down Expand Up @@ -246,6 +248,9 @@ const (

// Branch bundles, e.g. /blog/_index.md
PathTypeBranch

// Content data file, _content.gotmpl.
PathTypeContentData
)

type Path struct {
Expand Down Expand Up @@ -541,6 +546,10 @@ func (p *Path) IsLeafBundle() bool {
return p.bundleType == PathTypeLeaf
}

func (p *Path) IsContentData() bool {
return p.bundleType == PathTypeContentData
}

func (p Path) ForBundleType(t PathType) *Path {
p.bundleType = t
return &p
Expand Down
16 changes: 16 additions & 0 deletions common/paths/pathparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,22 @@ func TestParse(t *testing.T) {
c.Assert(p.Path(), qt.Equals, "/a/b/c.txt")
},
},
{
"Content data file gotmpl",
"/a/b/_content.gotmpl",
func(c *qt.C, p *Path) {
c.Assert(p.Path(), qt.Equals, "/a/b/_content.gotmpl")
c.Assert(p.Ext(), qt.Equals, "gotmpl")
c.Assert(p.IsContentData(), qt.IsTrue)
},
},
{
"Content data file yaml",
"/a/b/_content.yaml",
func(c *qt.C, p *Path) {
c.Assert(p.IsContentData(), qt.IsFalse)
},
},
}
for _, test := range tests {
c.Run(test.name, func(c *qt.C) {
Expand Down
11 changes: 11 additions & 0 deletions hugofs/files/classifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ func IsContentExt(ext string) bool {
return contentFileExtensionsSet[ext]
}

func IsGoTmplExt(ext string) bool {
return ext == "gotmpl"
}

// Supported data file extensions for _content.* files.
func IsContentDataExt(ext string) bool {
return IsGoTmplExt(ext)
}

const (
ComponentFolderArchetypes = "archetypes"
ComponentFolderStatic = "static"
Expand All @@ -93,6 +102,8 @@ const (

FolderResources = "resources"
FolderJSConfig = "_jsconfig" // Mounted below /assets with postcss.config.js etc.

NameContentData = "_content"
)

var (
Expand Down
59 changes: 57 additions & 2 deletions hugolib/content_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"github.com/bep/logg"
"github.com/gohugoio/hugo/common/hugio"
"github.com/gohugoio/hugo/common/paths"
"github.com/gohugoio/hugo/hugofs/files"
"github.com/gohugoio/hugo/hugolib/pagesfromdata"
"github.com/gohugoio/hugo/identity"
"github.com/gohugoio/hugo/source"

Expand Down Expand Up @@ -162,11 +164,13 @@ func (cfg contentMapConfig) getTaxonomyConfig(s string) (v viewName) {
return
}

func (m *pageMap) AddFi(fi hugofs.FileMetaInfo) error {
func (m *pageMap) AddFi(fi hugofs.FileMetaInfo, whatChanged *whatChanged) error {
if fi.IsDir() {
return nil
}

rebuilding := m.s.h.buildCounter.Load() > 0

insertResource := func(fim hugofs.FileMetaInfo) error {
pi := fi.Meta().PathInfo
key := pi.Base()
Expand Down Expand Up @@ -222,6 +226,57 @@ func (m *pageMap) AddFi(fi hugofs.FileMetaInfo) error {
if err := insertResource(fi); err != nil {
return err
}
case paths.PathTypeContentData:
m.s.Log.Trace(logg.StringFunc(
func() string {
return fmt.Sprintf("insert pages from data file: %q", fi.Meta().Filename)
},
))

if !files.IsGoTmplExt(pi.Ext()) {
return fmt.Errorf("unsupported data file extension %q", pi.Ext())
}

f := source.NewFileInfo(fi)
if err := func() error {
return pagesfromdata.PagesFromTemplate(
pagesfromdata.PagesFromTemplateOptions{
Fi: fi,
TmplFinder: m.s.TextTmpl(),
TmplExec: m.s.Tmpl(),
Site: m.s, // TODO1 wrapper without RegularPages etc.
Handlepage: func(p pagesfromdata.PageData) error {
pc := p.PageConfig
pc.Path = path.Join(pi.Base(), pc.Path)
ps, pi, err := m.s.h.newPage(
&pageMeta{
f: f,
pageMetaParams: pageMetaParams{
pageConfig: pc,
},
},
)
if err != nil {
return err
}

if ps == nil {
// Disabled page.
return nil
}

n, _, replaced := m.treePages.InsertIntoValuesDimension(pi.Base(), ps)

if rebuilding && replaced {
whatChanged.Add(n.GetIdentity())
}
return nil
},
},
)
}(); err != nil {
return err
}
default:
m.s.Log.Trace(logg.StringFunc(
func() string {
Expand All @@ -244,7 +299,7 @@ func (m *pageMap) AddFi(fi hugofs.FileMetaInfo) error {
return nil
}

m.treePages.InsertWithLock(pi.Base(), p)
m.treePages.InsertIntoValuesDimensionWithLock(pi.Base(), p)

}
return nil
Expand Down

0 comments on commit b8908e5

Please sign in to comment.