| 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/common/paths" |
| 22 |
"github.com/gohugoio/hugo/helpers" |
| 23 |
"github.com/gohugoio/hugo/resources" |
| 24 |
"github.com/gohugoio/hugo/resources/internal" |
| 25 |
"github.com/gohugoio/hugo/resources/resource" |
| 26 |
"github.com/gohugoio/hugo/tpl/tplimpl" |
| 27 |
) |
| 28 |
|
| 29 |
// Client contains methods to perform template processing of Resource objects. |
| 30 |
type Client struct { |
| 31 |
rs *resources.Spec |
| 32 |
t tplimpl.TemplateStoreProvider |
| 33 |
} |
| 34 |
|
| 35 |
// New creates a new Client with the given specification. |
| 36 |
func New(rs *resources.Spec, t tplimpl.TemplateStoreProvider) *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 tplimpl.TemplateStoreProvider |
| 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 |
th := t.t.GetTemplateStore() |
| 60 |
ti, err := th.TextParse(ctx.InPath, tplStr) |
| 61 |
if err != nil { |
| 62 |
return fmt.Errorf("failed to parse Resource %q as Template:: %w", ctx.InPath, err) |
| 63 |
} |
| 64 |
ctx.OutPath = t.targetPath |
| 65 |
return th.ExecuteWithContext(ctx.Ctx, ti, 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: paths.ToSlashTrimLeading(targetPath), |
| 72 |
t: c.t, |
| 73 |
data: data, |
| 74 |
}) |
| 75 |
} |