Share variable scope and the walk over a document's nodes

Flatten and Validate read nodes through one walk, and the parser, flattener,
validator and writer set variables through one type.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-01 18:20:35 +00:00
co-authored by Claude Sonnet 5.5
parent cc484ac3ef
commit 1bb842589e
5 changed files with 129 additions and 104 deletions
+4 -4
View File
@@ -46,7 +46,7 @@ type FileWriter func(path string, data []byte) error
// defines, so it is an error unless no include was read; use [Document.WriteFiles]
// then.
func (d *Document) Write(w io.Writer, opts WriteOptions) error {
r := &renderer{opts: opts, defines: map[string]string{}, outputs: map[string]string{}}
r := &renderer{opts: opts, defines: defines{}, outputs: map[string]string{}}
text := r.file(d.Root)
if r.err != nil {
return r.err
@@ -63,7 +63,7 @@ func (d *Document) Write(w io.Writer, opts WriteOptions) error {
// and returns [ErrInvalid] if an include's path is absolute or leaves the
// folder.
func (d *Document) WriteFiles(root string, fw FileWriter, opts WriteOptions) error {
r := &renderer{opts: opts, defines: map[string]string{}, outputs: map[string]string{}}
r := &renderer{opts: opts, defines: defines{}, outputs: map[string]string{}}
text := r.file(d.Root)
if r.err != nil {
return r.err
@@ -93,7 +93,7 @@ func (d *Document) WriteFiles(root string, fw FileWriter, opts WriteOptions) err
// renderer writes files as the options say.
type renderer struct {
opts WriteOptions
defines map[string]string
defines defines
// outputs are the included files that stand apart, by path.
outputs map[string]string
err error
@@ -118,7 +118,7 @@ func (r *renderer) file(f *File) string {
switch n.Kind {
case KindDefine:
if r.opts.ExpandVariables {
r.defines[n.Name] = strings.TrimSpace(substitute(r.defines, n.Value))
r.defines.define(n)
dropBreak = true
continue
}