github.com/neohugo/neohugo@v0.123.8/resources/resource_transformers/templates/execute_as_template.go (about)

     1  // Copyright 2018 The Hugo Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  // Package templates contains functions for template processing of Resource objects.
    15  package templates
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  
    21  	"github.com/neohugo/neohugo/common/paths"
    22  	"github.com/neohugo/neohugo/helpers"
    23  	"github.com/neohugo/neohugo/resources"
    24  	"github.com/neohugo/neohugo/resources/internal"
    25  	"github.com/neohugo/neohugo/resources/resource"
    26  	"github.com/neohugo/neohugo/tpl"
    27  )
    28  
    29  // Client contains methods to perform template processing of Resource objects.
    30  type Client struct {
    31  	rs *resources.Spec
    32  	t  tpl.TemplatesProvider
    33  }
    34  
    35  // New creates a new Client with the given specification.
    36  func New(rs *resources.Spec, t tpl.TemplatesProvider) *Client {
    37  	if rs == nil {
    38  		panic("must provide a resource Spec")
    39  	}
    40  	if t == nil {
    41  		panic("must provide a template provider")
    42  	}
    43  	return &Client{rs: rs, t: t}
    44  }
    45  
    46  type executeAsTemplateTransform struct {
    47  	rs         *resources.Spec
    48  	t          tpl.TemplatesProvider
    49  	targetPath string
    50  	data       any
    51  }
    52  
    53  func (t *executeAsTemplateTransform) Key() internal.ResourceTransformationKey {
    54  	return internal.NewResourceTransformationKey("execute-as-template", t.targetPath)
    55  }
    56  
    57  func (t *executeAsTemplateTransform) Transform(ctx *resources.ResourceTransformationCtx) error {
    58  	tplStr := helpers.ReaderToString(ctx.From)
    59  	templ, err := t.t.TextTmpl().Parse(ctx.InPath, tplStr)
    60  	if err != nil {
    61  		return fmt.Errorf("failed to parse Resource %q as Template:: %w", ctx.InPath, err)
    62  	}
    63  
    64  	ctx.OutPath = t.targetPath
    65  
    66  	return t.t.Tmpl().ExecuteWithContext(ctx.Ctx, templ, ctx.To, t.data)
    67  }
    68  
    69  func (c *Client) ExecuteAsTemplate(ctx context.Context, res resources.ResourceTransformer, targetPath string, data any) (resource.Resource, error) {
    70  	return res.TransformWithContext(ctx, &executeAsTemplateTransform{
    71  		rs:         c.rs,
    72  		targetPath: paths.ToSlashTrimLeading(targetPath),
    73  		t:          c.t,
    74  		data:       data,
    75  	})
    76  }