Add js.Batch

Fixes #12626
Closes #7499
Closes #9978
Closes #12879
Closes #13113
Fixes #13116
This commit is contained in:
Bjørn Erik Pedersen
2024-12-10 16:22:08 +01:00
parent 157d86414d
commit e293e7ca6d
61 changed files with 4520 additions and 1003 deletions

View File

@@ -16,8 +16,11 @@ package resource
import (
"fmt"
"path"
"strings"
"github.com/gohugoio/hugo/common/hreflect"
"github.com/gohugoio/hugo/common/maps"
"github.com/gohugoio/hugo/common/paths"
"github.com/gohugoio/hugo/hugofs/glob"
"github.com/spf13/cast"
@@ -29,6 +32,51 @@ var _ ResourceFinder = (*Resources)(nil)
// I.e. both pages and images etc.
type Resources []Resource
// Mount mounts the given resources from base to the given target path.
// Note that leading slashes in target marks an absolute path.
// This method is currently only useful in js.Batch.
func (r Resources) Mount(base, target string) ResourceGetter {
return resourceGetterFunc(func(namev any) Resource {
name1, err := cast.ToStringE(namev)
if err != nil {
panic(err)
}
isTargetAbs := strings.HasPrefix(target, "/")
if target != "" {
name1 = strings.TrimPrefix(name1, target)
if !isTargetAbs {
name1 = paths.TrimLeading(name1)
}
}
if base != "" && isTargetAbs {
name1 = path.Join(base, name1)
}
for _, res := range r {
name2 := res.Name()
if base != "" && !isTargetAbs {
name2 = paths.TrimLeading(strings.TrimPrefix(name2, base))
}
if strings.EqualFold(name1, name2) {
return res
}
}
return nil
})
}
type ResourcesProvider interface {
// Resources returns a list of all resources.
Resources() Resources
}
// var _ resource.ResourceFinder = (*Namespace)(nil)
// ResourcesConverter converts a given slice of Resource objects to Resources.
type ResourcesConverter interface {
@@ -63,13 +111,25 @@ func (r Resources) Get(name any) Resource {
panic(err)
}
namestr = paths.AddLeadingSlash(namestr)
isDotCurrent := strings.HasPrefix(namestr, "./")
if isDotCurrent {
namestr = strings.TrimPrefix(namestr, "./")
} else {
namestr = paths.AddLeadingSlash(namestr)
}
check := func(name string) bool {
if !isDotCurrent {
name = paths.AddLeadingSlash(name)
}
return strings.EqualFold(namestr, name)
}
// First check the Name.
// Note that this can be modified by the user in the front matter,
// also, it does not contain any language code.
for _, resource := range r {
if strings.EqualFold(namestr, paths.AddLeadingSlash(resource.Name())) {
if check(resource.Name()) {
return resource
}
}
@@ -77,7 +137,7 @@ func (r Resources) Get(name any) Resource {
// Finally, check the normalized name.
for _, resource := range r {
if nop, ok := resource.(NameNormalizedProvider); ok {
if strings.EqualFold(namestr, paths.AddLeadingSlash(nop.NameNormalized())) {
if check(nop.NameNormalized()) {
return resource
}
}
@@ -197,14 +257,35 @@ type Source interface {
Publish() error
}
// ResourceFinder provides methods to find Resources.
// Note that GetRemote (as found in resources.GetRemote) is
// not covered by this interface, as this is only available as a global template function.
type ResourceFinder interface {
type ResourceGetter interface {
// Get locates the Resource with the given name in the current context (e.g. in .Page.Resources).
//
// It returns nil if no Resource could found, panics if name is invalid.
Get(name any) Resource
}
type IsProbablySameResourceGetter interface {
IsProbablySameResourceGetter(other ResourceGetter) bool
}
// StaleInfoResourceGetter is a ResourceGetter that also provides information about
// whether the underlying resources are stale.
type StaleInfoResourceGetter interface {
StaleInfo
ResourceGetter
}
type resourceGetterFunc func(name any) Resource
func (f resourceGetterFunc) Get(name any) Resource {
return f(name)
}
// ResourceFinder provides methods to find Resources.
// Note that GetRemote (as found in resources.GetRemote) is
// not covered by this interface, as this is only available as a global template function.
type ResourceFinder interface {
ResourceGetter
// GetMatch finds the first Resource matching the given pattern, or nil if none found.
//
@@ -235,3 +316,92 @@ type ResourceFinder interface {
// It returns nil if no Resources could found, panics if typ is invalid.
ByType(typ any) Resources
}
// NewCachedResourceGetter creates a new ResourceGetter from the given objects.
// If multiple objects are provided, they are merged into one where
// the first match wins.
func NewCachedResourceGetter(os ...any) *cachedResourceGetter {
var getters multiResourceGetter
for _, o := range os {
if g, ok := unwrapResourceGetter(o); ok {
getters = append(getters, g)
}
}
return &cachedResourceGetter{
cache: maps.NewCache[string, Resource](),
delegate: getters,
}
}
type multiResourceGetter []ResourceGetter
func (m multiResourceGetter) Get(name any) Resource {
for _, g := range m {
if res := g.Get(name); res != nil {
return res
}
}
return nil
}
var (
_ ResourceGetter = (*cachedResourceGetter)(nil)
_ IsProbablySameResourceGetter = (*cachedResourceGetter)(nil)
)
type cachedResourceGetter struct {
cache *maps.Cache[string, Resource]
delegate ResourceGetter
}
func (c *cachedResourceGetter) Get(name any) Resource {
namestr, err := cast.ToStringE(name)
if err != nil {
panic(err)
}
v, _ := c.cache.GetOrCreate(namestr, func() (Resource, error) {
v := c.delegate.Get(name)
return v, nil
})
return v
}
func (c *cachedResourceGetter) IsProbablySameResourceGetter(other ResourceGetter) bool {
isProbablyEq := true
c.cache.ForEeach(func(k string, v Resource) bool {
if v != other.Get(k) {
isProbablyEq = false
return false
}
return true
})
return isProbablyEq
}
func unwrapResourceGetter(v any) (ResourceGetter, bool) {
if v == nil {
return nil, false
}
switch vv := v.(type) {
case ResourceGetter:
return vv, true
case ResourcesProvider:
return vv.Resources(), true
case func(name any) Resource:
return resourceGetterFunc(vv), true
default:
vvv, ok := hreflect.ToSliceAny(v)
if !ok {
return nil, false
}
var getters multiResourceGetter
for _, vv := range vvv {
if g, ok := unwrapResourceGetter(vv); ok {
getters = append(getters, g)
}
}
return getters, len(getters) > 0
}
}