Debian Codegraph @

tpl/tplimpl/template_funcs.go

raw
1 // Copyright 2025 The Hugo Authors. All rights reserved.
2 //
3 // Portions Copyright The Go Authors.
4
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15
16 package tplimpl
17
18 import (
19 "context"
20 "reflect"
21 "strings"
22
23 "github.com/gohugoio/hugo/common/hmaps"
24 "github.com/gohugoio/hugo/common/hreflect"
25 "github.com/gohugoio/hugo/identity"
26 "github.com/gohugoio/hugo/tpl"
27
28 texttemplate "github.com/gohugoio/hugo/tpl/internal/go_templates/texttemplate"
29 )
30
31 var (
32 _ texttemplate.ExecHelper = (*templateExecHelper)(nil)
33 zero reflect.Value
34 )
35
36 type templateExecHelper struct {
37 watching bool // whether we're in server/watch mode.
38 site reflect.Value
39 siteParams reflect.Value
40 funcs map[string]reflect.Value
41 }
42
43 func (t *templateExecHelper) GetFunc(ctx context.Context, tmpl texttemplate.Preparer, name string) (fn reflect.Value, firstArg reflect.Value, found bool) {
44 if fn, found := t.funcs[name]; found {
45 if fn.Type().NumIn() > 0 {
46 first := fn.Type().In(0)
47 if hreflect.IsContextType(first) {
48 // TODO(bep) check if we can void this conversion every time -- and if that matters.
49 // The first argument may be context.Context. This is never provided by the end user, but it's used to pass down
50 // contextual information, e.g. the top level data context (e.g. Page).
51 return fn, reflect.ValueOf(ctx), true
52 }
53 }
54
55 return fn, zero, true
56 }
57 return zero, zero, false
58 }
59
60 func (t *templateExecHelper) Init(ctx context.Context, tmpl texttemplate.Preparer) {
61 if t.watching {
62 _, ok := tmpl.(identity.IdentityProvider)
63 if ok {
64 t.trackDependencies(ctx, tmpl, "", reflect.Value{})
65 }
66
67 }
68 }
69
70 func (t *templateExecHelper) GetMapValue(ctx context.Context, tmpl texttemplate.Preparer, receiver, key reflect.Value) (reflect.Value, bool) {
71 if params, ok := receiver.Interface().(hmaps.Params); ok {
72 // Case insensitive.
73 keystr := strings.ToLower(key.String())
74 v, found := params[keystr]
75 if !found {
76 return zero, false
77 }
78 return reflect.ValueOf(v), true
79 }
80
81 v := receiver.MapIndex(key)
82
83 return v, v.IsValid()
84 }
85
86 var typeParams = reflect.TypeFor[hmaps.Params]()
87
88 func (t *templateExecHelper) GetMethod(ctx context.Context, tmpl texttemplate.Preparer, receiver reflect.Value, name string) (method reflect.Value, firstArg reflect.Value) {
89 if strings.EqualFold(name, "mainsections") && receiver.Type() == typeParams && receiver.Pointer() == t.siteParams.Pointer() {
90 // Moved to site.MainSections in Hugo 0.112.0.
91 receiver = t.site
92 name = "MainSections"
93 }
94
95 if t.watching {
96 ctx = t.trackDependencies(ctx, tmpl, name, receiver)
97 }
98
99 fn := hreflect.GetMethodByName(receiver, name)
100 if !fn.IsValid() {
101 return zero, zero
102 }
103
104 if fn.Type().NumIn() > 0 {
105 first := fn.Type().In(0)
106 if hreflect.IsContextType(first) {
107 // The first argument may be context.Context. This is never provided by the end user, but it's used to pass down
108 // contextual information, e.g. the top level data context (e.g. Page).
109 return fn, reflect.ValueOf(ctx)
110 }
111 }
112
113 return fn, zero
114 }
115
116 func (t *templateExecHelper) OnCalled(ctx context.Context, tmpl texttemplate.Preparer, name string, args []reflect.Value, result reflect.Value) {
117 if !t.watching {
118 return
119 }
120
121 // This switch is mostly for speed.
122 switch name {
123 case "Unmarshal":
124 default:
125 return
126 }
127 idm := tpl.Context.GetDependencyManagerInCurrentScope(ctx)
128 if idm == nil {
129 return
130 }
131
132 for _, arg := range args {
133 identity.WalkIdentitiesShallow(arg.Interface(), func(level int, id identity.Identity) bool {
134 idm.AddIdentity(id)
135 return false
136 })
137 }
138 }
139
140 func (t *templateExecHelper) trackDependencies(ctx context.Context, tmpl texttemplate.Preparer, name string, receiver reflect.Value) context.Context {
141 if tmpl == nil {
142 panic("must provide a template")
143 }
144
145 idm := tpl.Context.GetDependencyManagerInCurrentScope(ctx)
146 if idm == nil {
147 return ctx
148 }
149
150 if info, ok := tmpl.(identity.IdentityProvider); ok {
151 idm.AddIdentity(info.GetIdentity())
152 }
153
154 // The receive is the "." in the method execution or map lookup, e.g. the Page in .Resources.
155 if hreflect.IsValid(receiver) {
156 in := receiver.Interface()
157
158 if idlp, ok := in.(identity.ForEeachIdentityByNameProvider); ok {
159 // This will skip repeated .RelPermalink usage on transformed resources
160 // which is not fingerprinted, e.g. to
161 // prevent all HTML pages to be re-rendered on a small CSS change.
162 idlp.ForEeachIdentityByName(name, func(id identity.Identity) bool {
163 idm.AddIdentity(id)
164 return false
165 })
166 } else {
167 identity.WalkIdentitiesShallow(in, func(level int, id identity.Identity) bool {
168 idm.AddIdentity(id)
169 return false
170 })
171 }
172 }
173
174 return ctx
175 }