2023-10-29 22:47:47 +00:00
|
|
|
package salix
|
|
|
|
|
2023-11-01 19:00:44 +00:00
|
|
|
import "sync"
|
2023-10-29 22:47:47 +00:00
|
|
|
|
|
|
|
// Namespace represents a collection of templates that can include each other
|
|
|
|
type Namespace struct {
|
2023-10-31 04:06:17 +00:00
|
|
|
mu sync.Mutex
|
2023-10-31 04:18:35 +00:00
|
|
|
tmpls map[string]Template
|
2023-11-01 19:00:44 +00:00
|
|
|
vars map[string]any
|
2023-10-31 04:06:17 +00:00
|
|
|
tags map[string]Tag
|
2023-11-01 18:39:28 +00:00
|
|
|
|
2023-12-18 19:59:39 +00:00
|
|
|
// 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
|
2023-10-29 22:47:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new template namespace
|
|
|
|
func New() *Namespace {
|
|
|
|
return &Namespace{
|
2023-12-18 19:59:39 +00:00
|
|
|
tmpls: map[string]Template{},
|
|
|
|
vars: map[string]any{},
|
|
|
|
tags: map[string]Tag{},
|
|
|
|
WhitespaceMutations: true,
|
2023-10-29 22:47:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithVarMap sets the namespace's variable map to m
|
|
|
|
func (n *Namespace) WithVarMap(m map[string]any) *Namespace {
|
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
2023-11-01 19:04:54 +00:00
|
|
|
|
|
|
|
if m == nil {
|
|
|
|
n.vars = map[string]any{}
|
|
|
|
} else {
|
|
|
|
n.vars = m
|
|
|
|
}
|
|
|
|
|
2023-10-29 22:47:47 +00:00
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithTagMap sets the namespace's tag map to m
|
|
|
|
func (n *Namespace) WithTagMap(m map[string]Tag) *Namespace {
|
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
|
|
|
|
2023-11-01 19:04:54 +00:00
|
|
|
if m == nil {
|
2023-10-29 22:47:47 +00:00
|
|
|
n.tags = map[string]Tag{}
|
2023-11-01 19:04:54 +00:00
|
|
|
} else {
|
|
|
|
n.tags = m
|
2023-10-29 22:47:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2023-12-18 19:59:39 +00:00
|
|
|
// WithEscapeHTML turns HTML escaping on or off for the namespace
|
2023-11-01 18:39:28 +00:00
|
|
|
func (n *Namespace) WithEscapeHTML(b bool) *Namespace {
|
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
|
|
|
n.escapeHTML = &b
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2023-12-18 19:59:39 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2023-10-29 22:47:47 +00:00
|
|
|
// 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.
|
2023-10-31 04:18:35 +00:00
|
|
|
func (n *Namespace) GetTemplate(name string) (Template, bool) {
|
2023-10-29 22:47:47 +00:00
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
|
|
|
t, ok := n.tmpls[name]
|
|
|
|
return t, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// getVar tries to get a variable from the namespace's variable map
|
2023-11-01 19:00:44 +00:00
|
|
|
func (n *Namespace) getVar(name string) (any, bool) {
|
2023-10-29 22:47:47 +00:00
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
|
|
|
v, ok := n.vars[name]
|
|
|
|
return v, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// getTag tries to get a tag from the namespace's tag map
|
|
|
|
func (n *Namespace) getTag(name string) (Tag, bool) {
|
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
|
|
|
t, ok := n.tags[name]
|
|
|
|
return t, ok
|
|
|
|
}
|
2023-11-01 18:39:28 +00:00
|
|
|
|
|
|
|
// getEscapeHTML returns the namespace's escapeHTML value
|
|
|
|
func (n *Namespace) getEscapeHTML() *bool {
|
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
|
|
|
return n.escapeHTML
|
|
|
|
}
|