github.com/linchen2chris/hugo@v0.0.0-20230307053224-cec209389705/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/gohugoio/hugo/helpers"
    22  	"github.com/gohugoio/hugo/resources"
    23  	"github.com/gohugoio/hugo/resources/internal"
    24  	"github.com/gohugoio/hugo/resources/resource"
    25  	"github.com/gohugoio/hugo/tpl"
    26  )
    27  
    28  // Client contains methods to perform template processing of Resource objects.
    29  type Client struct {
    30  	rs *resources.Spec
    31  	t  tpl.TemplatesProvider
    32  }
    33  
    34  // New creates a new Client with the given specification.
    35  func New(rs *resources.Spec, t tpl.TemplatesProvider) *Client {
    36  	if rs == nil {
    37  		panic("must provice a resource Spec")
    38  	}
    39  	if t == nil {
    40  		panic("must provide a template provider")
    41  	}
    42  	return &Client{rs: rs, t: t}
    43  }
    44  
    45  type executeAsTemplateTransform struct {
    46  	rs         *resources.Spec
    47  	t          tpl.TemplatesProvider
    48  	targetPath string
    49  	data       any
    50  }
    51  
    52  func (t *executeAsTemplateTransform) Key() internal.ResourceTransformationKey {
    53  	return internal.NewResourceTransformationKey("execute-as-template", t.targetPath)
    54  }
    55  
    56  func (t *executeAsTemplateTransform) Transform(ctx *resources.ResourceTransformationCtx) error {
    57  	tplStr := helpers.ReaderToString(ctx.From)
    58  	templ, err := t.t.TextTmpl().Parse(ctx.InPath, tplStr)
    59  	if err != nil {
    60  		return fmt.Errorf("failed to parse Resource %q as Template:: %w", ctx.InPath, err)
    61  	}
    62  
    63  	ctx.OutPath = t.targetPath
    64  
    65  	return t.t.Tmpl().ExecuteWithContext(ctx.Ctx, templ, ctx.To, t.data)
    66  }
    67  
    68  func (c *Client) ExecuteAsTemplate(ctx context.Context, res resources.ResourceTransformer, targetPath string, data any) (resource.Resource, error) {
    69  	return res.TransformWithContext(ctx, &executeAsTemplateTransform{
    70  		rs:         c.rs,
    71  		targetPath: helpers.ToSlashTrimLeading(targetPath),
    72  		t:          c.t,
    73  		data:       data,
    74  	})
    75  }