-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroup.go
More file actions
409 lines (365 loc) · 12.6 KB
/
group.go
File metadata and controls
409 lines (365 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
package templar
import (
"fmt"
htmpl "html/template"
"io"
"log/slog"
"maps"
"path/filepath"
ttmpl "text/template"
"text/template/parse"
)
// TemplateGroup manages a collection of templates and their dependencies,
// providing methods to process and render them.
type TemplateGroup struct {
templates map[string]*Template
// Underlying html and text template that map to given names (NOT PATHS)
// Funcs contains template functions available to all templates in this group.
Funcs map[string]any
// Loader is used to resolve and load template dependencies.
Loader TemplateLoader
htmlTemplates map[string]*htmpl.Template
textTemplates map[string]*ttmpl.Template
dependencies map[string]map[string]bool
}
// NewTemplateGroup creates a new empty template group with initialized internals.
func NewTemplateGroup() *TemplateGroup {
return &TemplateGroup{
Funcs: make(map[string]any),
htmlTemplates: make(map[string]*htmpl.Template),
textTemplates: make(map[string]*ttmpl.Template),
templates: make(map[string]*Template),
dependencies: make(map[string]map[string]bool),
}
}
// Calls the underlying Loader to load templates matching a pattern and optional using a cwd for relative paths.
// Panics if an error is encountered.
// Returns matching templates or an error if no templates were found.
func (t *TemplateGroup) MustLoad(pattern string, cwd string) []*Template {
out, err := t.Loader.Load(pattern, cwd)
if err != nil {
panic(err)
}
return out
}
// AddFuncs adds template functions to this group, making them available
// to all templates. Returns the template group for method chaining.
func (t *TemplateGroup) AddFuncs(funcs map[string]any) *TemplateGroup {
maps.Copy(t.Funcs, funcs)
return t
}
// NewHtmlTemplate creates a new HTML template with the given name.
// The template will have access to the group's functions and any additional
// functions provided.
func (t *TemplateGroup) NewHtmlTemplate(name string, funcs map[string]any) (out *htmpl.Template) {
out = htmpl.New(name).Funcs(t.Funcs)
if funcs != nil {
out = out.Funcs(funcs)
}
return out
}
// NewTextTemplate creates a new TEXT template with the given name.
// The template will have access to the group's functions and any additional
// functions provided.
func (t *TemplateGroup) NewTextTemplate(name string, funcs map[string]any) (out *ttmpl.Template) {
out = ttmpl.New(name).Funcs(t.Funcs)
if funcs != nil {
out = out.Funcs(funcs)
}
return out
}
// PreProcessTextTemplate processes a template and its dependencies, creating a text/template
// that can be used for rendering. It handles template dependencies recursively.
// Returns the processed template and any error encountered.
func (t *TemplateGroup) PreProcessTextTemplate(root *Template, funcs ttmpl.FuncMap) (out *ttmpl.Template, err error) {
name := root.Name
if name == "" {
name = root.Path
}
if name != "" {
out = t.textTemplates[name]
}
if true || out == nil {
// try and load it
out = t.NewTextTemplate(name, funcs)
err = root.WalkTemplate(t.Loader, func(t *Template) error {
if t.Path == "" {
out, err = out.Parse(t.ParsedSource)
return panicOrError(err)
} else {
x, err := out.Parse(t.ParsedSource)
if err != nil {
return panicOrError(err)
}
// TODO - is this really necessary to add the parsed source back to out
// Should the parsing already do that for "out" anyway?
base := filepath.Base(t.Path)
out, err = out.AddParseTree(base, x.Tree)
return panicOrError(err)
}
})
if err == nil && name != "" {
t.textTemplates[name] = out
}
}
return out, err
}
// PreProcessHtmlTemplate processes a HTML template and its dependencies, creating an html/template
// that can be used for rendering. It handles template dependencies recursively.
// Returns the processed template and any error encountered.
func (t *TemplateGroup) PreProcessHtmlTemplate(root *Template, funcs htmpl.FuncMap) (out *htmpl.Template, err error) {
name := root.Name
if name == "" {
name = root.Path
}
if name != "" {
out = t.htmlTemplates[name]
}
if true || out == nil {
// try and load it
out = htmpl.New(name).Funcs(t.Funcs)
if funcs != nil {
out = out.Funcs(funcs)
}
// Collect all extensions from all processed templates
var allExtensions []Extension
w := Walker{Loader: t.Loader,
ProcessedTemplate: func(curr *Template) error {
// Collect extensions from this template
allExtensions = append(allExtensions, curr.Extensions...)
// Skip non-root templates that don't have a namespace and no entry points
// (they will be processed via normal include mechanism)
if curr != root && curr.Namespace == "" && len(curr.NamespaceEntryPoints) == 0 {
return nil
}
if curr.Path == "" {
out, err = out.Parse(curr.ParsedSource)
return panicOrError(err)
}
// If namespace is set, parse into a temporary template and apply namespacing
if curr.Namespace != "" {
return t.processNamespacedTemplate(curr, out, funcs)
}
// If entry points are set (selective include), apply tree-shaking
if len(curr.NamespaceEntryPoints) > 0 {
return t.processSelectiveInclude(curr, out, funcs)
}
// Normal case: parse and add with original name
base := filepath.Base(curr.Path)
x, err := out.Parse(curr.ParsedSource)
if err != nil {
return panicOrError(err)
}
out, err = out.AddParseTree(base, x.Tree)
return panicOrError(err)
}}
err = w.Walk(root)
if err != nil {
return out, err
}
// Process all collected extensions after all templates are parsed
err = t.processExtensionsList(allExtensions, out)
if err != nil {
return out, err
}
if name != "" {
t.htmlTemplates[name] = out
}
}
return out, err
}
// processNamespacedTemplate handles templates that should be added to a namespace.
// It parses the template, applies tree-shaking if entry points are specified,
// and adds all reachable templates with namespaced names.
func (t *TemplateGroup) processNamespacedTemplate(curr *Template, out *htmpl.Template, funcs htmpl.FuncMap) error {
slog.Debug("processNamespacedTemplate", "path", curr.Path, "namespace", curr.Namespace)
// Parse into a fresh temporary template to avoid name collisions
temp := htmpl.New("temp").Funcs(t.Funcs)
if funcs != nil {
temp = temp.Funcs(funcs)
}
temp, err := temp.Parse(curr.ParsedSource)
if err != nil {
return panicOrError(err)
}
// Build map of all templates for tree-shaking
allTemplates := make(map[string]*htmpl.Template)
var allNames []string
for _, tmpl := range temp.Templates() {
if tmpl.Tree != nil && tmpl.Name() != "temp" {
allTemplates[tmpl.Name()] = tmpl
allNames = append(allNames, tmpl.Name())
}
}
// slog.Debug("processNamespacedTemplate: found templates", "path", curr.Path, "templates", allNames)
// Determine which templates to include
var templatesToInclude map[string]bool
if len(curr.NamespaceEntryPoints) > 0 {
// Tree-shaking: only include reachable templates
treesMap := make(map[string]*parse.Tree)
for name, tmpl := range allTemplates {
treesMap[name] = tmpl.Tree
}
templatesToInclude = ComputeReachableTemplates(treesMap, curr.NamespaceEntryPoints)
} else {
// Include all templates
templatesToInclude = make(map[string]bool)
for _, name := range allNames {
templatesToInclude[name] = true
}
}
// Build rewrite map for all templates being included
rewrites := make(map[string]string)
for name := range templatesToInclude {
rewrites[name] = TransformName(name, curr.Namespace)
}
// Add namespaced templates to output
var createdNames []string
for name := range templatesToInclude {
tmpl := allTemplates[name]
if tmpl == nil || tmpl.Tree == nil {
continue
}
// Copy tree and apply namespace rewrites
copiedTree := tmpl.Tree.Copy()
WalkParseTree(copiedTree.Root, func(node *parse.TemplateNode) {
// Apply full namespace transformation rules
node.Name = TransformName(node.Name, curr.Namespace)
})
namespacedName := rewrites[name]
copiedTree.Name = namespacedName
out, err = out.AddParseTree(namespacedName, copiedTree)
if err != nil {
return panicOrError(err)
}
createdNames = append(createdNames, namespacedName)
}
// slog.Debug("processNamespacedTemplate: created templates", "path", curr.Path, "created", createdNames)
return nil
}
// processSelectiveInclude handles templates with entry points but no namespace.
// It applies tree-shaking to only include the specified templates and their dependencies.
func (t *TemplateGroup) processSelectiveInclude(curr *Template, out *htmpl.Template, funcs htmpl.FuncMap) error {
// Parse into a fresh temporary template
temp := htmpl.New("temp").Funcs(t.Funcs)
if funcs != nil {
temp = temp.Funcs(funcs)
}
temp, err := temp.Parse(curr.ParsedSource)
if err != nil {
return panicOrError(err)
}
// Build map of all templates for tree-shaking
treesMap := make(map[string]*parse.Tree)
templatesMap := make(map[string]*htmpl.Template)
for _, tmpl := range temp.Templates() {
if tmpl.Tree != nil && tmpl.Name() != "temp" {
treesMap[tmpl.Name()] = tmpl.Tree
templatesMap[tmpl.Name()] = tmpl
}
}
// Compute reachable templates
templatesToInclude := ComputeReachableTemplates(treesMap, curr.NamespaceEntryPoints)
// Add only reachable templates to output
for name := range templatesToInclude {
tmpl := templatesMap[name]
if tmpl == nil || tmpl.Tree == nil {
continue
}
out, err = out.AddParseTree(name, tmpl.Tree)
if err != nil {
return panicOrError(err)
}
}
return nil
}
// processExtensions processes all extend directives recorded on the root template.
// For each extension, it copies the source template and rewires references.
func (t *TemplateGroup) processExtensions(root *Template, out *htmpl.Template) error {
return t.processExtensionsList(root.Extensions, out)
}
// processExtensionsList processes a list of extensions.
// For each extension, it copies the source template and rewires references.
func (t *TemplateGroup) processExtensionsList(extensions []Extension, out *htmpl.Template) error {
if false && len(extensions) > 0 {
// Log available templates for debugging
var availableNames []string
for _, tmpl := range out.Templates() {
if tmpl.Tree != nil {
availableNames = append(availableNames, tmpl.Name())
}
}
slog.Debug("processExtensionsList: available templates", "count", len(availableNames), "templates", availableNames)
}
for _, ext := range extensions {
slog.Debug("processExtensionsList: processing extension", "source", ext.SourceTemplate, "dest", ext.DestTemplate)
// Find the source template
sourceTmpl := out.Lookup(ext.SourceTemplate)
if sourceTmpl == nil || sourceTmpl.Tree == nil {
return fmt.Errorf("extend: source template not found: %s", ext.SourceTemplate)
}
// Copy the tree and apply rewrites
copiedTree := CopyTreeWithRewrites(sourceTmpl.Tree, ext.Rewrites)
copiedTree.Name = ext.DestTemplate
// Add the new template
var err error
out, err = out.AddParseTree(ext.DestTemplate, copiedTree)
if err != nil {
return panicOrError(err)
}
}
return nil
}
// RenderHtmlTemplate renders a template as HTML to the provided writer.
//
// It processes the template with its dependencies, executes it with the given data,
// and applies any additional template functions provided.
//
// If entry is specified, it executes that specific template within the processed template.
func (t *TemplateGroup) RenderHtmlTemplate(w io.Writer, root *Template, entry string, data any, funcs map[string]any) (err error) {
out, err := t.PreProcessHtmlTemplate(root, funcs)
if err != nil {
return panicOrError(err)
}
tmpl := htmpl.Must(out, err)
name := entry
if name == "" {
name = root.Name
}
if name == "" {
err = tmpl.Execute(w, data)
} else {
err = tmpl.ExecuteTemplate(w, name, data)
}
if err != nil {
slog.Error("error rendering template as html: ", "name", name, "error", err)
return panicOrError(err)
}
return
}
// RenderTextTemplate renders a template as plain text to the provided writer.
//
// It processes the template with its dependencies, executes it with the given data,
// and applies any additional template functions provided.
//
// If entry is specified, it executes that specific template within the processed template.
func (t *TemplateGroup) RenderTextTemplate(w io.Writer, root *Template, entry string, data any, funcs map[string]any) (err error) {
out, err := t.PreProcessTextTemplate(root, funcs)
if err != nil {
return panicOrError(err)
}
tmpl := ttmpl.Must(out, err)
name := entry
if name == "" {
name = root.Name
}
if name == "" {
err = tmpl.Execute(w, data)
} else {
err = tmpl.ExecuteTemplate(w, name, data)
}
if err != nil {
slog.Error("error rendering template as text: ", "name", name, "error", err)
}
return
}