mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-21 21:35:28 +02:00
@@ -16,9 +16,12 @@
|
||||
package create
|
||||
|
||||
import (
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/spf13/afero"
|
||||
"github.com/gohugoio/hugo/hugofs/glob"
|
||||
|
||||
"github.com/gohugoio/hugo/hugofs"
|
||||
|
||||
"github.com/gohugoio/hugo/common/hugio"
|
||||
"github.com/gohugoio/hugo/resources"
|
||||
@@ -36,18 +39,75 @@ func New(rs *resources.Spec) *Client {
|
||||
return &Client{rs: rs}
|
||||
}
|
||||
|
||||
// Get creates a new Resource by opening the given filename in the given filesystem.
|
||||
func (c *Client) Get(fs afero.Fs, filename string) (resource.Resource, error) {
|
||||
// Get creates a new Resource by opening the given filename in the assets filesystem.
|
||||
func (c *Client) Get(filename string) (resource.Resource, error) {
|
||||
filename = filepath.Clean(filename)
|
||||
return c.rs.ResourceCache.GetOrCreate(resources.ResourceKeyPartition(filename), filename, func() (resource.Resource, error) {
|
||||
return c.rs.New(resources.ResourceSourceDescriptor{
|
||||
Fs: fs,
|
||||
Fs: c.rs.BaseFs.Assets.Fs,
|
||||
LazyPublish: true,
|
||||
SourceFilename: filename})
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
// Match gets the resources matching the given pattern from the assets filesystem.
|
||||
func (c *Client) Match(pattern string) (resource.Resources, error) {
|
||||
return c.match(pattern, false)
|
||||
}
|
||||
|
||||
// GetMatch gets first resource matching the given pattern from the assets filesystem.
|
||||
func (c *Client) GetMatch(pattern string) (resource.Resource, error) {
|
||||
res, err := c.match(pattern, true)
|
||||
if err != nil || len(res) == 0 {
|
||||
return nil, err
|
||||
}
|
||||
return res[0], err
|
||||
}
|
||||
|
||||
func (c *Client) match(pattern string, firstOnly bool) (resource.Resources, error) {
|
||||
var partition string
|
||||
if firstOnly {
|
||||
partition = "__get-match"
|
||||
} else {
|
||||
partition = "__match"
|
||||
}
|
||||
|
||||
// TODO(bep) match will be improved as part of https://github.com/gohugoio/hugo/issues/6199
|
||||
partition = path.Join(resources.CACHE_OTHER, partition)
|
||||
key := glob.NormalizePath(pattern)
|
||||
|
||||
return c.rs.ResourceCache.GetOrCreateResources(partition, key, func() (resource.Resources, error) {
|
||||
var res resource.Resources
|
||||
|
||||
handle := func(info hugofs.FileMetaInfo) (bool, error) {
|
||||
meta := info.Meta()
|
||||
r, err := c.rs.New(resources.ResourceSourceDescriptor{
|
||||
LazyPublish: true,
|
||||
OpenReadSeekCloser: func() (hugio.ReadSeekCloser, error) {
|
||||
return meta.Open()
|
||||
},
|
||||
RelTargetFilename: meta.Path()})
|
||||
|
||||
if err != nil {
|
||||
return true, err
|
||||
}
|
||||
|
||||
res = append(res, r)
|
||||
|
||||
return firstOnly, nil
|
||||
|
||||
}
|
||||
|
||||
if err := hugofs.Glob(c.rs.BaseFs.Assets.Fs, pattern, handle); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return res, nil
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
// FromString creates a new Resource from a string with the given relative target path.
|
||||
func (c *Client) FromString(targetPath, content string) (resource.Resource, error) {
|
||||
return c.rs.ResourceCache.GetOrCreate(resources.CACHE_OTHER, targetPath, func() (resource.Resource, error) {
|
||||
|
Reference in New Issue
Block a user