Add the ability to disable whitespace mutations

This commit is contained in:
Elara 2023-12-18 11:59:39 -08:00
parent da0b1ebb52
commit f4307541c4
2 changed files with 21 additions and 6 deletions

View File

@ -9,6 +9,10 @@ type Namespace struct {
vars map[string]any vars map[string]any
tags map[string]Tag tags map[string]Tag
// WhitespaceMutations enables postprocessing to remove whitespace where it isn't needed
// to make the resulting document look better. Postprocessing is only done oncewhen the
// template is parsed, so it will not affect performance. (default: true)
WhitespaceMutations bool
escapeHTML *bool escapeHTML *bool
} }
@ -18,6 +22,7 @@ func New() *Namespace {
tmpls: map[string]Template{}, tmpls: map[string]Template{},
vars: map[string]any{}, vars: map[string]any{},
tags: map[string]Tag{}, tags: map[string]Tag{},
WhitespaceMutations: true,
} }
} }
@ -49,7 +54,7 @@ func (n *Namespace) WithTagMap(m map[string]Tag) *Namespace {
return n return n
} }
// WithVarMap turns HTML escaping on or off for the namespace // WithEscapeHTML turns HTML escaping on or off for the namespace
func (n *Namespace) WithEscapeHTML(b bool) *Namespace { func (n *Namespace) WithEscapeHTML(b bool) *Namespace {
n.mu.Lock() n.mu.Lock()
defer n.mu.Unlock() defer n.mu.Unlock()
@ -57,6 +62,14 @@ func (n *Namespace) WithEscapeHTML(b bool) *Namespace {
return n return n
} }
// WithWhitespaceMutations turns whitespace mutations on or off for the namespace
func (n *Namespace) WithWhitespaceMutations(b bool) *Namespace {
n.mu.Lock()
defer n.mu.Unlock()
n.WhitespaceMutations = true
return n
}
// GetTemplate tries to get a template from the namespace's template map. // GetTemplate tries to get a template from the namespace's template map.
// If it finds the template, it returns the template and true. If it // If it finds the template, it returns the template and true. If it
// doesn't find it, it returns nil and false. // doesn't find it, it returns nil and false.

View File

@ -39,7 +39,9 @@ func (n *Namespace) ParseWithName(name string, r io.Reader) (Template, error) {
vars: map[string]any{}, vars: map[string]any{},
} }
if n.WhitespaceMutations {
performWhitespaceMutations(t.ast) performWhitespaceMutations(t.ast)
}
n.mu.Lock() n.mu.Lock()
defer n.mu.Unlock() defer n.mu.Unlock()