Add the ability to disable whitespace mutations
This commit is contained in:
parent
da0b1ebb52
commit
f4307541c4
23
namespace.go
23
namespace.go
@ -9,15 +9,20 @@ type Namespace struct {
|
|||||||
vars map[string]any
|
vars map[string]any
|
||||||
tags map[string]Tag
|
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
|
// New returns a new template namespace
|
||||||
func New() *Namespace {
|
func New() *Namespace {
|
||||||
return &Namespace{
|
return &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.
|
||||||
|
4
parse.go
4
parse.go
@ -39,7 +39,9 @@ func (n *Namespace) ParseWithName(name string, r io.Reader) (Template, error) {
|
|||||||
vars: map[string]any{},
|
vars: map[string]any{},
|
||||||
}
|
}
|
||||||
|
|
||||||
performWhitespaceMutations(t.ast)
|
if n.WhitespaceMutations {
|
||||||
|
performWhitespaceMutations(t.ast)
|
||||||
|
}
|
||||||
|
|
||||||
n.mu.Lock()
|
n.mu.Lock()
|
||||||
defer n.mu.Unlock()
|
defer n.mu.Unlock()
|
||||||
|
Loading…
Reference in New Issue
Block a user