mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-29 22:29:56 +02:00
Add js.Batch
Fixes #12626 Closes #7499 Closes #9978 Closes #12879 Closes #13113 Fixes #13116
This commit is contained in:
@@ -14,10 +14,11 @@
|
||||
package resource
|
||||
|
||||
import (
|
||||
"github.com/gohugoio/hugo/common/maps"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gohugoio/hugo/common/maps"
|
||||
|
||||
"github.com/gohugoio/hugo/helpers"
|
||||
"github.com/pelletier/go-toml/v2"
|
||||
|
||||
|
@@ -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
|
||||
}
|
||||
}
|
||||
|
105
resources/resource/resources_integration_test.go
Normal file
105
resources/resource/resources_integration_test.go
Normal file
@@ -0,0 +1,105 @@
|
||||
// Copyright 2024 The Hugo Authors. All rights reserved.
|
||||
//
|
||||
// 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 resource_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gohugoio/hugo/hugolib"
|
||||
)
|
||||
|
||||
func TestResourcesMount(t *testing.T) {
|
||||
files := `
|
||||
-- hugo.toml --
|
||||
-- assets/text/txt1.txt --
|
||||
Text 1.
|
||||
-- assets/text/txt2.txt --
|
||||
Text 2.
|
||||
-- assets/text/sub/txt3.txt --
|
||||
Text 3.
|
||||
-- assets/text/sub/txt4.txt --
|
||||
Text 4.
|
||||
-- content/mybundle/index.md --
|
||||
---
|
||||
title: "My Bundle"
|
||||
---
|
||||
-- content/mybundle/txt1.txt --
|
||||
Text 1.
|
||||
-- content/mybundle/sub/txt2.txt --
|
||||
Text 1.
|
||||
-- layouts/index.html --
|
||||
{{ $mybundle := site.GetPage "mybundle" }}
|
||||
{{ $subResources := resources.Match "/text/sub/*.*" }}
|
||||
{{ $subResourcesMount := $subResources.Mount "/text/sub" "/newroot" }}
|
||||
resources:text/txt1.txt:{{ with resources.Get "text/txt1.txt" }}{{ .Name }}{{ end }}|
|
||||
resources:text/txt2.txt:{{ with resources.Get "text/txt2.txt" }}{{ .Name }}{{ end }}|
|
||||
resources:text/sub/txt3.txt:{{ with resources.Get "text/sub/txt3.txt" }}{{ .Name }}{{ end }}|
|
||||
subResources.range:{{ range $subResources }}{{ .Name }}|{{ end }}|
|
||||
subResources:"text/sub/txt3.txt:{{ with $subResources.Get "text/sub/txt3.txt" }}{{ .Name }}{{ end }}|
|
||||
subResourcesMount:/newroot/txt3.txt:{{ with $subResourcesMount.Get "/newroot/txt3.txt" }}{{ .Name }}{{ end }}|
|
||||
page:txt1.txt:{{ with $mybundle.Resources.Get "txt1.txt" }}{{ .Name }}{{ end }}|
|
||||
page:./txt1.txt:{{ with $mybundle.Resources.Get "./txt1.txt" }}{{ .Name }}{{ end }}|
|
||||
page:sub/txt2.txt:{{ with $mybundle.Resources.Get "sub/txt2.txt" }}{{ .Name }}{{ end }}|
|
||||
`
|
||||
b := hugolib.Test(t, files)
|
||||
|
||||
b.AssertFileContent("public/index.html", `
|
||||
resources:text/txt1.txt:/text/txt1.txt|
|
||||
resources:text/txt2.txt:/text/txt2.txt|
|
||||
resources:text/sub/txt3.txt:/text/sub/txt3.txt|
|
||||
subResources:"text/sub/txt3.txt:/text/sub/txt3.txt|
|
||||
subResourcesMount:/newroot/txt3.txt:/text/sub/txt3.txt|
|
||||
page:txt1.txt:txt1.txt|
|
||||
page:./txt1.txt:txt1.txt|
|
||||
page:sub/txt2.txt:sub/txt2.txt|
|
||||
`)
|
||||
}
|
||||
|
||||
func TestResourcesMountOnRename(t *testing.T) {
|
||||
files := `
|
||||
-- hugo.toml --
|
||||
disableKinds = ["taxonomy", "term", "home", "sitemap"]
|
||||
-- content/mybundle/index.md --
|
||||
---
|
||||
title: "My Bundle"
|
||||
resources:
|
||||
- name: /foo/bars.txt
|
||||
src: foo/txt1.txt
|
||||
- name: foo/bars2.txt
|
||||
src: foo/txt2.txt
|
||||
---
|
||||
-- content/mybundle/foo/txt1.txt --
|
||||
Text 1.
|
||||
-- content/mybundle/foo/txt2.txt --
|
||||
Text 2.
|
||||
-- layouts/_default/single.html --
|
||||
Single.
|
||||
{{ $mybundle := site.GetPage "mybundle" }}
|
||||
Resources:{{ range $mybundle.Resources }}Name: {{ .Name }}|{{ end }}$
|
||||
{{ $subResourcesMount := $mybundle.Resources.Mount "/foo" "/newroot" }}
|
||||
{{ $subResourcesMount2 := $mybundle.Resources.Mount "foo" "/newroot" }}
|
||||
{{ $subResourcesMount3 := $mybundle.Resources.Mount "foo" "." }}
|
||||
subResourcesMount:/newroot/bars.txt:{{ with $subResourcesMount.Get "/newroot/bars.txt" }}{{ .Name }}{{ end }}|
|
||||
subResourcesMount:/newroot/bars2.txt:{{ with $subResourcesMount.Get "/newroot/bars2.txt" }}{{ .Name }}{{ end }}|
|
||||
subResourcesMount2:/newroot/bars2.txt:{{ with $subResourcesMount2.Get "/newroot/bars2.txt" }}{{ .Name }}{{ end }}|
|
||||
subResourcesMount3:bars2.txt:{{ with $subResourcesMount3.Get "bars2.txt" }}{{ .Name }}{{ end }}|
|
||||
`
|
||||
b := hugolib.Test(t, files)
|
||||
b.AssertFileContent("public/mybundle/index.html",
|
||||
"Resources:Name: foo/bars.txt|Name: foo/bars2.txt|$",
|
||||
"subResourcesMount:/newroot/bars.txt:|\nsubResourcesMount:/newroot/bars2.txt:|",
|
||||
"subResourcesMount2:/newroot/bars2.txt:foo/bars2.txt|",
|
||||
"subResourcesMount3:bars2.txt:foo/bars2.txt|",
|
||||
)
|
||||
}
|
122
resources/resource/resources_test.go
Normal file
122
resources/resource/resources_test.go
Normal file
@@ -0,0 +1,122 @@
|
||||
// Copyright 2024 The Hugo Authors. All rights reserved.
|
||||
//
|
||||
// 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 resource
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
qt "github.com/frankban/quicktest"
|
||||
)
|
||||
|
||||
func TestResourcesMount(t *testing.T) {
|
||||
c := qt.New(t)
|
||||
c.Assert(true, qt.IsTrue)
|
||||
|
||||
var m ResourceGetter
|
||||
var r Resources
|
||||
|
||||
check := func(in, expect string) {
|
||||
c.Helper()
|
||||
r := m.Get(in)
|
||||
c.Assert(r, qt.Not(qt.IsNil))
|
||||
c.Assert(r.Name(), qt.Equals, expect)
|
||||
}
|
||||
|
||||
checkNil := func(in string) {
|
||||
c.Helper()
|
||||
r := m.Get(in)
|
||||
c.Assert(r, qt.IsNil)
|
||||
}
|
||||
|
||||
// Misc tests.
|
||||
r = Resources{
|
||||
testResource{name: "/foo/theme.css"},
|
||||
}
|
||||
|
||||
m = r.Mount("/foo", ".")
|
||||
check("./theme.css", "/foo/theme.css")
|
||||
|
||||
// Relative target.
|
||||
r = Resources{
|
||||
testResource{name: "/a/b/c/d.txt"},
|
||||
testResource{name: "/a/b/c/e/f.txt"},
|
||||
testResource{name: "/a/b/d.txt"},
|
||||
testResource{name: "/a/b/e.txt"},
|
||||
}
|
||||
|
||||
m = r.Mount("/a/b/c", "z")
|
||||
check("z/d.txt", "/a/b/c/d.txt")
|
||||
check("z/e/f.txt", "/a/b/c/e/f.txt")
|
||||
|
||||
m = r.Mount("/a/b", "")
|
||||
check("d.txt", "/a/b/d.txt")
|
||||
m = r.Mount("/a/b", ".")
|
||||
check("d.txt", "/a/b/d.txt")
|
||||
m = r.Mount("/a/b", "./")
|
||||
check("d.txt", "/a/b/d.txt")
|
||||
check("./d.txt", "/a/b/d.txt")
|
||||
|
||||
m = r.Mount("/a/b", ".")
|
||||
check("./d.txt", "/a/b/d.txt")
|
||||
|
||||
// Absolute target.
|
||||
m = r.Mount("/a/b/c", "/z")
|
||||
check("/z/d.txt", "/a/b/c/d.txt")
|
||||
check("/z/e/f.txt", "/a/b/c/e/f.txt")
|
||||
checkNil("/z/f.txt")
|
||||
|
||||
m = r.Mount("/a/b", "/z")
|
||||
check("/z/c/d.txt", "/a/b/c/d.txt")
|
||||
check("/z/c/e/f.txt", "/a/b/c/e/f.txt")
|
||||
check("/z/d.txt", "/a/b/d.txt")
|
||||
checkNil("/z/f.txt")
|
||||
|
||||
m = r.Mount("", "")
|
||||
check("/a/b/c/d.txt", "/a/b/c/d.txt")
|
||||
check("/a/b/c/e/f.txt", "/a/b/c/e/f.txt")
|
||||
check("/a/b/d.txt", "/a/b/d.txt")
|
||||
checkNil("/a/b/f.txt")
|
||||
|
||||
m = r.Mount("/a/b", "/a/b")
|
||||
check("/a/b/c/d.txt", "/a/b/c/d.txt")
|
||||
check("/a/b/c/e/f.txt", "/a/b/c/e/f.txt")
|
||||
check("/a/b/d.txt", "/a/b/d.txt")
|
||||
checkNil("/a/b/f.txt")
|
||||
|
||||
// Resources with relative paths.
|
||||
r = Resources{
|
||||
testResource{name: "a/b/c/d.txt"},
|
||||
testResource{name: "a/b/c/e/f.txt"},
|
||||
testResource{name: "a/b/d.txt"},
|
||||
testResource{name: "a/b/e.txt"},
|
||||
testResource{name: "n.txt"},
|
||||
}
|
||||
|
||||
m = r.Mount("a/b", "z")
|
||||
check("z/d.txt", "a/b/d.txt")
|
||||
checkNil("/z/d.txt")
|
||||
}
|
||||
|
||||
type testResource struct {
|
||||
Resource
|
||||
name string
|
||||
}
|
||||
|
||||
func (r testResource) Name() string {
|
||||
return r.name
|
||||
}
|
||||
|
||||
func (r testResource) NameNormalized() string {
|
||||
return r.name
|
||||
}
|
Reference in New Issue
Block a user