mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-25 22:00:58 +02:00
@@ -135,3 +135,7 @@ func (e *errorResource) DecodeImage() (image.Image, error) {
|
||||
func (e *errorResource) Transform(...ResourceTransformation) (ResourceTransformer, error) {
|
||||
panic(e.ResourceError)
|
||||
}
|
||||
|
||||
func (e *errorResource) TransformWithContext(context.Context, ...ResourceTransformation) (ResourceTransformer, error) {
|
||||
panic(e.ResourceError)
|
||||
}
|
||||
|
@@ -159,12 +159,7 @@ func (p Pages) GroupBy(ctx context.Context, key string, order ...string) (PagesG
|
||||
case reflect.StructField:
|
||||
fv = ppv.Elem().FieldByName(key)
|
||||
case reflect.Method:
|
||||
var args []reflect.Value
|
||||
fn := hreflect.GetMethodByName(ppv, key)
|
||||
if fn.Type().NumIn() > 0 && fn.Type().In(0).Implements(hreflect.ContextInterface) {
|
||||
args = []reflect.Value{reflect.ValueOf(ctx)}
|
||||
}
|
||||
fv = fn.Call(args)[0]
|
||||
fv = hreflect.CallMethodByName(ctx, key, ppv)[0]
|
||||
}
|
||||
if !fv.IsValid() {
|
||||
continue
|
||||
|
@@ -104,6 +104,7 @@ type ResourceTransformer interface {
|
||||
|
||||
type Transformer interface {
|
||||
Transform(...ResourceTransformation) (ResourceTransformer, error)
|
||||
TransformWithContext(context.Context, ...ResourceTransformation) (ResourceTransformer, error)
|
||||
}
|
||||
|
||||
func NewFeatureNotAvailableTransformer(key string, elements ...any) ResourceTransformation {
|
||||
|
@@ -15,6 +15,7 @@
|
||||
package templates
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/gohugoio/hugo/helpers"
|
||||
@@ -61,11 +62,11 @@ func (t *executeAsTemplateTransform) Transform(ctx *resources.ResourceTransforma
|
||||
|
||||
ctx.OutPath = t.targetPath
|
||||
|
||||
return t.t.Tmpl().Execute(templ, ctx.To, t.data)
|
||||
return t.t.Tmpl().ExecuteWithContext(ctx.Ctx, templ, ctx.To, t.data)
|
||||
}
|
||||
|
||||
func (c *Client) ExecuteAsTemplate(res resources.ResourceTransformer, targetPath string, data any) (resource.Resource, error) {
|
||||
return res.Transform(&executeAsTemplateTransform{
|
||||
func (c *Client) ExecuteAsTemplate(ctx context.Context, res resources.ResourceTransformer, targetPath string, data any) (resource.Resource, error) {
|
||||
return res.TransformWithContext(ctx, &executeAsTemplateTransform{
|
||||
rs: c.rs,
|
||||
targetPath: helpers.ToSlashTrimLeading(targetPath),
|
||||
t: c.t,
|
||||
|
@@ -69,6 +69,7 @@ func newResourceAdapter(spec *Spec, lazyPublish bool, target transformableResour
|
||||
return &resourceAdapter{
|
||||
resourceTransformations: &resourceTransformations{},
|
||||
resourceAdapterInner: &resourceAdapterInner{
|
||||
ctx: context.TODO(),
|
||||
spec: spec,
|
||||
publishOnce: po,
|
||||
target: target,
|
||||
@@ -84,6 +85,9 @@ type ResourceTransformation interface {
|
||||
}
|
||||
|
||||
type ResourceTransformationCtx struct {
|
||||
// The context that started the transformation.
|
||||
Ctx context.Context
|
||||
|
||||
// The content to transform.
|
||||
From io.Reader
|
||||
|
||||
@@ -180,6 +184,7 @@ func (r *resourceAdapter) Data() any {
|
||||
func (r resourceAdapter) cloneTo(targetPath string) resource.Resource {
|
||||
newtTarget := r.target.cloneTo(targetPath)
|
||||
newInner := &resourceAdapterInner{
|
||||
ctx: r.ctx,
|
||||
spec: r.spec,
|
||||
target: newtTarget.(transformableResource),
|
||||
}
|
||||
@@ -278,11 +283,17 @@ func (r *resourceAdapter) Title() string {
|
||||
}
|
||||
|
||||
func (r resourceAdapter) Transform(t ...ResourceTransformation) (ResourceTransformer, error) {
|
||||
return r.TransformWithContext(context.Background(), t...)
|
||||
}
|
||||
|
||||
func (r resourceAdapter) TransformWithContext(ctx context.Context, t ...ResourceTransformation) (ResourceTransformer, error) {
|
||||
|
||||
r.resourceTransformations = &resourceTransformations{
|
||||
transformations: append(r.transformations, t...),
|
||||
}
|
||||
|
||||
r.resourceAdapterInner = &resourceAdapterInner{
|
||||
ctx: ctx,
|
||||
spec: r.spec,
|
||||
publishOnce: &publishOnce{},
|
||||
target: r.target,
|
||||
@@ -377,6 +388,7 @@ func (r *resourceAdapter) transform(publish, setContent bool) error {
|
||||
defer bp.PutBuffer(b2)
|
||||
|
||||
tctx := &ResourceTransformationCtx{
|
||||
Ctx: r.ctx,
|
||||
Data: make(map[string]any),
|
||||
OpenResourcePublisher: r.target.openPublishFileForWriting,
|
||||
}
|
||||
@@ -599,6 +611,9 @@ func (r *resourceAdapter) initTransform(publish, setContent bool) {
|
||||
}
|
||||
|
||||
type resourceAdapterInner struct {
|
||||
// The context that started this transformation.
|
||||
ctx context.Context
|
||||
|
||||
target transformableResource
|
||||
|
||||
spec *Spec
|
||||
|
Reference in New Issue
Block a user