From b64617fe4f90da030bcf4a9c5a4913393ce96b14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Mon, 12 Aug 2019 16:43:37 +0200 Subject: Add resources.Match and resources.GetMatch Fix #6190 --- tpl/resources/resources.go | 46 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 4 deletions(-) (limited to 'tpl') diff --git a/tpl/resources/resources.go b/tpl/resources/resources.go index d32e12a05..3d688e21c 100644 --- a/tpl/resources/resources.go +++ b/tpl/resources/resources.go @@ -68,7 +68,7 @@ type Namespace struct { templatesClient *templates.Client } -// Get locates the filename given in Hugo's filesystems: static, assets and content (in that order) +// 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 interface{}) (resource.Resource, error) { filenamestr, err := cast.ToStringE(filename) @@ -78,12 +78,50 @@ func (ns *Namespace) Get(filename interface{}) (resource.Resource, error) { filenamestr = filepath.Clean(filenamestr) - // Resource Get'ing is currently limited to /assets to make it simpler - // to control the behaviour of publishing and partial rebuilding. - return ns.createClient.Get(ns.deps.BaseFs.Assets.Fs, filenamestr) + return ns.createClient.Get(filenamestr) } +// GetMatch finds the first Resource matching the given pattern, or nil if none found. +// +// 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 interface{}) (resource.Resource, error) { + patternStr, err := cast.ToStringE(pattern) + if err != nil { + return nil, err + } + + return ns.createClient.GetMatch(patternStr) + +} + +// Match gets all resources matching the given base path prefix, e.g +// "*.png" will match all png files. The "*" does not match path delimiters (/), +// so if you organize your resources in sub-folders, you need to be explicit about it, e.g.: +// "images/*.png". To match any PNG image anywhere in the bundle you can do "**.png", and +// to match all PNG images below the images folder, use "images/**.jpg". +// +// The matching is case insensitive. +// +// Match matches by using the files name with path relative to the file system root +// with Unix style slashes (/) and no leading slash, e.g. "images/logo.png". +// +// See https://github.com/gobwas/glob for the full rules set. +// +// 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 interface{}) (resource.Resources, error) { + patternStr, err := cast.ToStringE(pattern) + if err != nil { + return nil, err + } + + return ns.createClient.Match(patternStr) +} + // Concat concatenates a slice of Resource objects. These resources must // (currently) be of the same Media Type. func (ns *Namespace) Concat(targetPathIn interface{}, r interface{}) (resource.Resource, error) { -- cgit v1.2.3