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,15 +9,20 @@ type Namespace struct {
vars map[string]any
tags map[string]Tag
escapeHTML *bool
// 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
}
// New returns a new template namespace
func New() *Namespace {
return &Namespace{
tmpls: map[string]Template{},
vars: map[string]any{},
tags: map[string]Tag{},
tmpls: map[string]Template{},
vars: map[string]any{},
tags: map[string]Tag{},
WhitespaceMutations: true,
}
}
@ -49,7 +54,7 @@ func (n *Namespace) WithTagMap(m map[string]Tag) *Namespace {
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 {
n.mu.Lock()
defer n.mu.Unlock()
@ -57,6 +62,14 @@ func (n *Namespace) WithEscapeHTML(b bool) *Namespace {
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.
// If it finds the template, it returns the template and true. If it
// 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{},
}
performWhitespaceMutations(t.ast)
if n.WhitespaceMutations {
performWhitespaceMutations(t.ast)
}
n.mu.Lock()
defer n.mu.Unlock()