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 May 10, 2024
1 parent 70c13f4 commit db55e3e
Show file tree
Hide file tree
Showing 41 changed files with 1,314 additions and 251 deletions.
14 changes: 14 additions & 0 deletions common/maps/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ func NewCache[K comparable, T any]() *Cache[K, T] {
}

// Delete deletes the given key from the cache.
// If c is nil, this method is a no-op.
func (c *Cache[K, T]) Get(key K) (T, bool) {
if c == nil {
var zero T
return zero, false
}
c.RLock()
v, found := c.m[key]
c.RUnlock()
Expand Down Expand Up @@ -60,6 +65,15 @@ func (c *Cache[K, T]) Set(key K, value T) {
c.Unlock()
}

// ForEeach calls the given function for each key/value pair in the cache.
func (c *Cache[K, T]) ForEeach(f func(K, T)) {
c.RLock()
defer c.RUnlock()
for k, v := range c.m {
f(k, v)
}
}

// SliceCache is a simple thread safe cache backed by a map.
type SliceCache[T any] struct {
m map[string][]T
Expand Down
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

0 comments on commit db55e3e

Please sign in to comment.