diff options
author | Bjørn Erik Pedersen <[email protected]> | 2023-02-25 09:24:59 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2023-02-25 19:53:18 +0100 |
commit | ce524d0b5ebaef05d29fa368465f31358f26dcda (patch) | |
tree | e5df54a5deeefacbff4916d3619f85c2cb341b01 /resources | |
parent | 2662faf61ff0240be1ee0d6c496b6b4a6ed55fb4 (diff) | |
download | hugo-ce524d0b5ebaef05d29fa368465f31358f26dcda.tar.gz hugo-ce524d0b5ebaef05d29fa368465f31358f26dcda.zip |
Add a page template func
Fixes #9339
Diffstat (limited to 'resources')
-rw-r--r-- | resources/errorResource.go | 4 | ||||
-rw-r--r-- | resources/page/pagegroup.go | 7 | ||||
-rw-r--r-- | resources/resource.go | 1 | ||||
-rw-r--r-- | resources/resource_transformers/templates/execute_as_template.go | 7 | ||||
-rw-r--r-- | resources/transform.go | 15 |
5 files changed, 25 insertions, 9 deletions
diff --git a/resources/errorResource.go b/resources/errorResource.go index 42edb0bd0..c8c32dfc3 100644 --- a/resources/errorResource.go +++ b/resources/errorResource.go @@ -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) +} diff --git a/resources/page/pagegroup.go b/resources/page/pagegroup.go index bac5d8327..99f1af3ff 100644 --- a/resources/page/pagegroup.go +++ b/resources/page/pagegroup.go @@ -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 diff --git a/resources/resource.go b/resources/resource.go index 8a524247a..7ccc5da39 100644 --- a/resources/resource.go +++ b/resources/resource.go @@ -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 { diff --git a/resources/resource_transformers/templates/execute_as_template.go b/resources/resource_transformers/templates/execute_as_template.go index 5fe4230f1..4d4aba396 100644 --- a/resources/resource_transformers/templates/execute_as_template.go +++ b/resources/resource_transformers/templates/execute_as_template.go @@ -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, diff --git a/resources/transform.go b/resources/transform.go index 3477c710f..4ab51485e 100644 --- a/resources/transform.go +++ b/resources/transform.go @@ -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 |