summaryrefslogtreecommitdiffstats
path: root/tpl/resources/resources.go
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2021-10-20 10:11:48 +0200
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2022-04-05 18:00:44 +0200
commite58a540895c28b8884823dcb1b64c272422f9923 (patch)
tree3fefb267f87e61a969a6ccf776d75fe64b50b2b1 /tpl/resources/resources.go
parent20162518c450770ebfd54e0e987f34a5cccf236b (diff)
resources: Create a common ResourceFinder interface
And make both .Resources and resources implement it. This gets us 2 new methods/functions, so you can now also do: * .Resources.Get * resources.ByType Note that GetRemote is not covered by this interface, as that is only available as a global template function. Fixes #8653
Diffstat (limited to 'tpl/resources/resources.go')
-rw-r--r--tpl/resources/resources.go49
1 files changed, 36 insertions, 13 deletions
diff --git a/tpl/resources/resources.go b/tpl/resources/resources.go
index 7e137c661..d3a98002f 100644
--- a/tpl/resources/resources.go
+++ b/tpl/resources/resources.go
@@ -16,9 +16,10 @@ package resources
import (
"fmt"
- "path/filepath"
"sync"
+ "github.com/gohugoio/hugo/common/herrors"
+
"github.com/gohugoio/hugo/common/maps"
"github.com/pkg/errors"
@@ -73,6 +74,8 @@ func New(deps *deps.Deps) (*Namespace, error) {
}, nil
}
+var _ resource.ResourceFinder = (*Namespace)(nil)
+
// Namespace provides template functions for the "resources" namespace.
type Namespace struct {
deps *deps.Deps
@@ -107,15 +110,19 @@ func (ns *Namespace) getscssClientDartSass() (*dartsass.Client, error) {
return ns.scssClientDartSass, err
}
-// Get locates the filename given in Hugo's assets filesystem and
-// creates a Resource object that can be used for
-// further transformations.
-func (ns *Namespace) Get(filename any) (resource.Resource, error) {
+// Get locates the filename given in Hugo's assets filesystem
+// and creates a Resource object that can be used for further transformations.
+func (ns *Namespace) Get(filename any) resource.Resource {
filenamestr, err := cast.ToStringE(filename)
if err != nil {
- return nil, err
+ panic(err)
+ }
+ r, err := ns.createClient.Get(filenamestr)
+ if err != nil {
+ panic(err)
}
- return ns.createClient.Get(filepath.Clean(filenamestr))
+
+ return r
}
// GetRemote gets the URL (via HTTP(s)) in the first argument in args and creates Resource object that can be used for
@@ -168,13 +175,23 @@ func (ns *Namespace) GetRemote(args ...any) resource.Resource {
// It looks for files in the assets file system.
//
// See Match for a more complete explanation about the rules used.
-func (ns *Namespace) GetMatch(pattern any) (resource.Resource, error) {
+func (ns *Namespace) GetMatch(pattern any) resource.Resource {
patternStr, err := cast.ToStringE(pattern)
if err != nil {
- return nil, err
+ panic(err)
+ }
+
+ r, err := ns.createClient.GetMatch(patternStr)
+ if err != nil {
+ panic(err)
}
- return ns.createClient.GetMatch(patternStr)
+ return r
+}
+
+// ByType returns resources of a given resource type (e.g. "image").
+func (ns *Namespace) ByType(typ any) resource.Resources {
+ return ns.createClient.ByType(cast.ToString(typ))
}
// Match gets all resources matching the given base path prefix, e.g
@@ -193,13 +210,19 @@ func (ns *Namespace) GetMatch(pattern any) (resource.Resource, error) {
// It looks for files in the assets file system.
//
// See Match for a more complete explanation about the rules used.
-func (ns *Namespace) Match(pattern any) (resource.Resources, error) {
+func (ns *Namespace) Match(pattern any) resource.Resources {
+ defer herrors.Recover()
patternStr, err := cast.ToStringE(pattern)
if err != nil {
- return nil, err
+ panic(err)
}
- return ns.createClient.Match(patternStr)
+ r, err := ns.createClient.Match(patternStr)
+ if err != nil {
+ panic(err)
+ }
+
+ return r
}
// Concat concatenates a slice of Resource objects. These resources must