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