Debian Codegraph @

hugolib/pagesfromdata/pagesfromgotmpl.go

raw
1 // Copyright 2024 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 pagesfromdata
15
16 import (
17 "context"
18 "fmt"
19 "io"
20 "path/filepath"
21
22 "github.com/gohugoio/hugo/common/hashing"
23 "github.com/gohugoio/hugo/common/hmaps"
24 "github.com/gohugoio/hugo/common/hstore"
25 "github.com/gohugoio/hugo/common/paths"
26 "github.com/gohugoio/hugo/helpers"
27 "github.com/gohugoio/hugo/hugofs"
28 "github.com/gohugoio/hugo/identity"
29 "github.com/gohugoio/hugo/resources/page"
30 "github.com/gohugoio/hugo/resources/page/pagemeta"
31 "github.com/gohugoio/hugo/resources/resource"
32 "github.com/gohugoio/hugo/tpl"
33 "github.com/gohugoio/hugo/tpl/tplimpl"
34 "github.com/mitchellh/mapstructure"
35 "github.com/spf13/cast"
36 )
37
38 type PagesFromDataTemplateContext interface {
39 // AddPage adds a new page to the site.
40 // The first return value will always be an empty string.
41 AddPage(any) (string, error)
42
43 // AddResource adds a new resource to the site.
44 // The first return value will always be an empty string.
45 AddResource(any) (string, error)
46
47 // The site to which the pages will be added.
48 Site() page.Site
49
50 // The same template may be executed multiple times for multiple languages.
51 // The Store can be used to store state between these invocations.
52 Store() *hstore.Scratch
53
54 // By default, the template will be executed for the language
55 // defined by the _content.gotmpl file (e.g. its mount definition).
56 // This method can be used to activate the template for all languages.
57 // The return value will always be an empty string.
58 EnableAllLanguages() string
59 }
60
61 var _ PagesFromDataTemplateContext = (*pagesFromDataTemplateContext)(nil)
62
63 type pagesFromDataTemplateContext struct {
64 p *PagesFromTemplate
65 }
66
67 func (p *pagesFromDataTemplateContext) toPathSitesMap(v any) (string, map[string]any, map[string]any, error) {
68 m, err := hmaps.ToStringMapE(v)
69 if err != nil {
70 return "", nil, nil, err
71 }
72
73 path, err := cast.ToStringE(m["path"])
74 if err != nil {
75 return "", nil, nil, fmt.Errorf("invalid path %q", path)
76 }
77
78 sites := hmaps.ToStringMap(m["sites"])
79
80 return path, sites, m, nil
81 }
82
83 func (p *pagesFromDataTemplateContext) AddPage(v any) (string, error) {
84 path, sites, m, err := p.toPathSitesMap(v)
85 if err != nil {
86 return "", err
87 }
88
89 hash, hasChanged := p.p.buildState.checkHasChangedAndSetSourceInfo(path, sites, m)
90 if !hasChanged {
91 return "", nil
92 }
93
94 pe := &pagemeta.PageConfigEarly{
95 IsFromContentAdapter: true,
96 Frontmatter: m,
97 SourceEntryHash: hash,
98 }
99
100 // The rest will be handled after the cascade is calculated and applied.
101 if err := mapstructure.WeakDecode(pe.Frontmatter, pe); err != nil {
102 err = fmt.Errorf("failed to decode page map: %w", err)
103 return "", err
104 }
105
106 if err := pe.Init(true); err != nil {
107 return "", err
108 }
109
110 p.p.buildState.NumPagesAdded++
111
112 return "", p.p.HandlePage(p.p, pe)
113 }
114
115 func (p *pagesFromDataTemplateContext) AddResource(v any) (string, error) {
116 path, sites, m, err := p.toPathSitesMap(v)
117 if err != nil {
118 return "", err
119 }
120
121 hash, hasChanged := p.p.buildState.checkHasChangedAndSetSourceInfo(path, sites, m)
122 if !hasChanged {
123 return "", nil
124 }
125
126 var rd pagemeta.ResourceConfig
127 if err := mapstructure.WeakDecode(m, &rd); err != nil {
128 return "", err
129 }
130 rd.ContentAdapterSourceEntryHash = hash
131
132 p.p.buildState.NumResourcesAdded++
133
134 if err := rd.Validate(); err != nil {
135 return "", err
136 }
137
138 return "", p.p.HandleResource(p.p, &rd)
139 }
140
141 func (p *pagesFromDataTemplateContext) Site() page.Site {
142 return p.p.Site
143 }
144
145 func (p *pagesFromDataTemplateContext) Store() *hstore.Scratch {
146 return p.p.store
147 }
148
149 func (p *pagesFromDataTemplateContext) EnableAllLanguages() string {
150 p.p.buildState.EnableAllLanguages = true
151 return ""
152 }
153
154 func (p *pagesFromDataTemplateContext) EnableAllDimensions() string {
155 p.p.buildState.EnableAllDimensions = true
156 return ""
157 }
158
159 func NewPagesFromTemplate(opts PagesFromTemplateOptions) *PagesFromTemplate {
160 return &PagesFromTemplate{
161 PagesFromTemplateOptions: opts,
162 PagesFromTemplateDeps: opts.DepsFromSite(opts.Site),
163 buildState: &BuildState{
164 sourceInfosCurrent: hmaps.NewCache[string, *sourceInfo](),
165 },
166 store: hstore.NewScratch(),
167 }
168 }
169
170 type PagesFromTemplateOptions struct {
171 Site page.Site
172 DepsFromSite func(page.Site) PagesFromTemplateDeps
173
174 DependencyManager identity.Manager
175
176 Watching bool
177
178 HandlePage func(pt *PagesFromTemplate, p *pagemeta.PageConfigEarly) error
179 HandleResource func(pt *PagesFromTemplate, p *pagemeta.ResourceConfig) error
180
181 GoTmplFi hugofs.FileMetaInfo
182 }
183
184 type PagesFromTemplateDeps struct {
185 TemplateStore *tplimpl.TemplateStore
186 }
187
188 var _ resource.Staler = (*PagesFromTemplate)(nil)
189
190 type PagesFromTemplate struct {
191 PagesFromTemplateOptions
192 PagesFromTemplateDeps
193 buildState *BuildState
194 store *hstore.Scratch
195 }
196
197 func (b *PagesFromTemplate) AddChange(id identity.Identity) {
198 b.buildState.ChangedIdentities = append(b.buildState.ChangedIdentities, id)
199 }
200
201 func (b *PagesFromTemplate) MarkStale() {
202 b.buildState.StaleVersion++
203 }
204
205 func (b *PagesFromTemplate) StaleVersion() uint32 {
206 return b.buildState.StaleVersion
207 }
208
209 type BuildInfo struct {
210 NumPagesAdded uint64
211 NumResourcesAdded uint64
212 EnableAllLanguages bool
213 EnableAllDimensions bool
214 ChangedIdentities []identity.Identity
215 DeletedPaths []PathHashes
216 Path *paths.Path
217 }
218
219 type BuildState struct {
220 StaleVersion uint32
221
222 EnableAllLanguages bool
223 EnableAllDimensions bool
224
225 // PathHashes deleted in the current build.
226 DeletedPaths []PathHashes
227
228 // Changed identities in the current build.
229 ChangedIdentities []identity.Identity
230
231 NumPagesAdded uint64
232 NumResourcesAdded uint64
233
234 sourceInfosCurrent *hmaps.Cache[string, *sourceInfo]
235 sourceInfosPrevious *hmaps.Cache[string, *sourceInfo]
236 }
237
238 func (b *BuildState) hash(v any) uint64 {
239 return hashing.HashUint64(v)
240 }
241
242 type sourceInfo struct {
243 siteHashes map[uint64]uint64
244 }
245
246 func (b *BuildState) checkHasChangedAndSetSourceInfo(changedPath string, sites map[string]any, v any) (uint64, bool) {
247 hv := b.hash(v)
248 hsites := b.hash(sites)
249
250 si, _ := b.sourceInfosCurrent.GetOrCreate(changedPath, func() (*sourceInfo, error) {
251 return &sourceInfo{
252 siteHashes: make(map[uint64]uint64),
253 }, nil
254 })
255
256 if h, found := si.siteHashes[hsites]; found && h == hv {
257 return hv, false
258 }
259
260 if psi, found := b.sourceInfosPrevious.Get(changedPath); found {
261 if h, found := psi.siteHashes[hsites]; found && h == hv {
262 // Not changed.
263 si.siteHashes[hsites] = hv
264 return hv, false
265 }
266 }
267
268 // It has changed.
269 si.siteHashes[hsites] = hv
270 return hv, true
271 }
272
273 type PathHashes struct {
274 Path string
275 Hashes map[uint64]struct{}
276 }
277
278 func (b *BuildState) resolveDeletedPaths() {
279 if b.sourceInfosPrevious == nil {
280 b.DeletedPaths = nil
281 return
282 }
283 var pathsHashes []PathHashes
284 b.sourceInfosPrevious.ForEeach(func(k string, pv *sourceInfo) bool {
285 if cv, found := b.sourceInfosCurrent.Get(k); !found {
286 pathsHashes = append(pathsHashes, PathHashes{Path: k, Hashes: map[uint64]struct{}{}})
287 } else {
288 deleted := map[uint64]struct{}{}
289 for k, ph := range pv.siteHashes {
290 ch, found := cv.siteHashes[k]
291 if !found || ch != ph {
292 deleted[ph] = struct{}{}
293 }
294 }
295 if len(deleted) > 0 {
296 pathsHashes = append(pathsHashes, PathHashes{Path: k, Hashes: deleted})
297 }
298 }
299 return true
300 })
301
302 b.DeletedPaths = pathsHashes
303 }
304
305 func (b *BuildState) PrepareNextBuild() {
306 b.sourceInfosPrevious = b.sourceInfosCurrent
307 b.sourceInfosCurrent = hmaps.NewCache[string, *sourceInfo]()
308 b.StaleVersion = 0
309 b.DeletedPaths = nil
310 b.ChangedIdentities = nil
311 b.NumPagesAdded = 0
312 b.NumResourcesAdded = 0
313 }
314
315 func (p PagesFromTemplate) CloneForSite(s page.Site) *PagesFromTemplate {
316 // We deliberately make them share the same DependencyManager and Store.
317 p.PagesFromTemplateOptions.Site = s
318 p.PagesFromTemplateDeps = p.PagesFromTemplateOptions.DepsFromSite(s)
319 p.buildState = &BuildState{
320 sourceInfosCurrent: hmaps.NewCache[string, *sourceInfo](),
321 }
322 return &p
323 }
324
325 func (p PagesFromTemplate) CloneForGoTmpl(fi hugofs.FileMetaInfo) *PagesFromTemplate {
326 p.PagesFromTemplateOptions.GoTmplFi = fi
327 return &p
328 }
329
330 func (p *PagesFromTemplate) GetDependencyManagerForScope(scope int) identity.Manager {
331 return p.DependencyManager
332 }
333
334 func (p *PagesFromTemplate) GetDependencyManagerForScopesAll() []identity.Manager {
335 return []identity.Manager{p.DependencyManager}
336 }
337
338 func (p *PagesFromTemplate) Execute(ctx context.Context) (BuildInfo, error) {
339 defer func() {
340 p.buildState.PrepareNextBuild()
341 }()
342
343 f, err := p.GoTmplFi.Meta().Open()
344 if err != nil {
345 return BuildInfo{}, err
346 }
347 defer f.Close()
348
349 tmpl, err := p.TemplateStore.TextParse(filepath.ToSlash(p.GoTmplFi.Meta().Filename), helpers.ReaderToString(f))
350 if err != nil {
351 return BuildInfo{}, err
352 }
353
354 data := &pagesFromDataTemplateContext{
355 p: p,
356 }
357
358 ctx = tpl.Context.DependencyManagerScopedProvider.Set(ctx, p)
359
360 if err := p.TemplateStore.ExecuteWithContext(ctx, tmpl, io.Discard, data); err != nil {
361 return BuildInfo{}, err
362 }
363
364 if p.Watching {
365 p.buildState.resolveDeletedPaths()
366 }
367
368 bi := BuildInfo{
369 NumPagesAdded: p.buildState.NumPagesAdded,
370 NumResourcesAdded: p.buildState.NumResourcesAdded,
371 EnableAllLanguages: p.buildState.EnableAllLanguages,
372 EnableAllDimensions: p.buildState.EnableAllDimensions,
373 ChangedIdentities: p.buildState.ChangedIdentities,
374 DeletedPaths: p.buildState.DeletedPaths,
375 Path: p.GoTmplFi.Meta().PathInfo,
376 }
377
378 return bi, nil
379 }