From e58a540895c28b8884823dcb1b64c272422f9923 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Wed, 20 Oct 2021 10:11:48 +0200 Subject: 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 --- tpl/resources/resources.go | 49 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 13 deletions(-) (limited to 'tpl/resources') 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 -- cgit v1.2.3