| 1 |
// Copyright 2019 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 template |
| 15 |
|
| 16 |
import ( |
| 17 |
"fmt" |
| 18 |
"iter" |
| 19 |
|
| 20 |
"github.com/gohugoio/hugo/common/types" |
| 21 |
template "github.com/gohugoio/hugo/tpl/internal/go_templates/texttemplate" |
| 22 |
) |
| 23 |
|
| 24 |
/* |
| 25 |
|
| 26 |
This files contains the Hugo related addons. All the other files in this |
| 27 |
package is auto generated. |
| 28 |
|
| 29 |
*/ |
| 30 |
|
| 31 |
// Export it so we can populate Hugo's func map with it, which makes it faster. |
| 32 |
var GoFuncs = funcMap |
| 33 |
|
| 34 |
// Prepare returns a template ready for execution. |
| 35 |
func (t *Template) Prepare() (*template.Template, error) { |
| 36 |
if err := t.escape(); err != nil { |
| 37 |
return nil, err |
| 38 |
} |
| 39 |
return t.text, nil |
| 40 |
} |
| 41 |
|
| 42 |
func (t *Template) All() iter.Seq[*Template] { |
| 43 |
return func(yield func(t *Template) bool) { |
| 44 |
ns := t.nameSpace |
| 45 |
ns.mu.Lock() |
| 46 |
defer ns.mu.Unlock() |
| 47 |
for _, v := range ns.set { |
| 48 |
if !yield(v) { |
| 49 |
return |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
// See https://github.com/golang/go/issues/5884 |
| 56 |
func StripTags(html string) string { |
| 57 |
return stripTags(html) |
| 58 |
} |
| 59 |
|
| 60 |
func indirect(a any) any { |
| 61 |
in := doIndirect(a) |
| 62 |
|
| 63 |
// We have a special Result type that we want to unwrap when printed. |
| 64 |
if pp, ok := in.(types.PrintableValueProvider); ok { |
| 65 |
return pp.PrintableValue() |
| 66 |
} |
| 67 |
|
| 68 |
return in |
| 69 |
} |
| 70 |
|
| 71 |
// CloneShallow creates a shallow copy of the template. It does not clone or copy the nested templates. |
| 72 |
func (t *Template) CloneShallow() (*Template, error) { |
| 73 |
t.nameSpace.mu.Lock() |
| 74 |
defer t.nameSpace.mu.Unlock() |
| 75 |
if t.escapeErr != nil { |
| 76 |
return nil, fmt.Errorf("html/template: cannot Clone %q after it has executed", t.Name()) |
| 77 |
} |
| 78 |
textClone, err := t.text.Clone() |
| 79 |
if err != nil { |
| 80 |
return nil, err |
| 81 |
} |
| 82 |
ns := &nameSpace{set: make(map[string]*Template)} |
| 83 |
ns.esc = makeEscaper(ns) |
| 84 |
ret := &Template{ |
| 85 |
nil, |
| 86 |
textClone, |
| 87 |
textClone.Tree, |
| 88 |
ns, |
| 89 |
} |
| 90 |
ret.set[ret.Name()] = ret |
| 91 |
|
| 92 |
// Return the template associated with the name of this template. |
| 93 |
return ret.set[ret.Name()], nil |
| 94 |
} |