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
This commit is contained in:
Bjørn Erik Pedersen
2021-10-20 10:11:48 +02:00
parent 20162518c4
commit e58a540895
7 changed files with 148 additions and 36 deletions

View File

@@ -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)
}
return ns.createClient.Get(filepath.Clean(filenamestr))
r, err := ns.createClient.Get(filenamestr)
if err != nil {
panic(err)
}
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)
}
return ns.createClient.GetMatch(patternStr)
r, err := ns.createClient.GetMatch(patternStr)
if err != nil {
panic(err)
}
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