Documentation ¶
Overview ¶
Package tmplz provides a templating engine with nested variables and dynamic variable assignment. It includes utilities for handling templates.
Index ¶
- Constants
- type Assignments
- type MultiTemplate
- func (mt MultiTemplate) Clone() *MultiTemplate
- func (mt MultiTemplate) Debug() string
- func (mt MultiTemplate) Dot() string
- func (mt MultiTemplate) Execute(assignments Assignments) (string, *MultiTemplate)
- func (mt *MultiTemplate) ExecuteInPlace(assignments Assignments) (anyChange bool)
- func (mt MultiTemplate) String() string
- type Node
- func (n *Node) AppendAlpha()
- func (n *Node) AppendOmega(real bool)
- func (n *Node) AppendRunes(s ...rune)
- func (n Node) Clone() *Node
- func (n Node) Debug() string
- func (n Node) DeepStart() int
- func (n Node) Dot() string
- func (n Node) Execute(assignments Assignments) (result string, clone *Node)
- func (n *Node) ExecuteInPlace(assignments Assignments) (anyChange bool)
- func (n Node) String() string
- func (n Node) Verify()
- type ParseState
- type String
- type Template
- func (t Template) Clone() *Template
- func (t Template) Debug() string
- func (t Template) Dot() string
- func (t Template) Execute(assignments Assignments) (result string, clone *Template)
- func (t *Template) ExecuteInPlace(assignments Assignments) (anyChange bool)
- func (t Template) String() string
- func (t Template) Verify()
- type Token
- type TokenKind
- type Tokens
Constants ¶
const ( ALPHA_RUNE = '@' ALPHA = "@" OMEGA_RUNE = '_' OMEGA = "_" OMICRON_RUNE = '‗' OMICRON = "‗" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Assignments ¶
Assignments holds key-value pairs for template variable assignments.
func (Assignments) Assign ¶
func (a Assignments) Assign(key string, value any)
Assign sets a value for a key in Assignments. If the key already exists, its value is updated.
func (Assignments) AssignMap ¶
func (a Assignments) AssignMap(kvps map[string]any)
AssignMap assigns multiple key-value pairs from a given map to the Assignments.
func (Assignments) AssignObj ¶
func (a Assignments) AssignObj(obj any)
AssignObj uses the exported fields of the provided object to assign key-value pairs to the Assignments. Each field name is used as a key.
func (Assignments) Clone ¶
func (a Assignments) Clone() Assignments
Clone creates a deep copy of the Assignments and returns it.
type MultiTemplate ¶
func ParseTemplates ¶
func ParseTemplates(templateMap map[string]string) *MultiTemplate
ParseTemplates is similar to ParseTemplates but includes logging support.
func (MultiTemplate) Clone ¶
func (mt MultiTemplate) Clone() *MultiTemplate
Clone creates a deep copy of the MultiTemplate, including all its templates.
func (MultiTemplate) Debug ¶
func (mt MultiTemplate) Debug() string
Debug returns a detailed string representation of all templates in MultiTemplate, including template names and their debug information.
func (MultiTemplate) Dot ¶
func (mt MultiTemplate) Dot() string
Dot generates a Graphviz dot graph of the MultiTemplate, illustrating the relationships between templates.
func (MultiTemplate) Execute ¶
func (mt MultiTemplate) Execute(assignments Assignments) (string, *MultiTemplate)
Execute clones the MultiTemplate and executes the Assignments on the clone.
Returns the result as well as the clone. By default, the result will be the value that has the key ".". If missing, an empty string is returned.
func (*MultiTemplate) ExecuteInPlace ¶
func (mt *MultiTemplate) ExecuteInPlace(assignments Assignments) (anyChange bool)
ExecuteInPlace modifies the MultiTemplate using the data from the Assignments.
To select how "@_" gets evaluated, set:
assignments.Assign("", "_") // default value
Returns when no more assignments are left to assign.
func (MultiTemplate) String ¶
func (mt MultiTemplate) String() string
String returns a string representation of all templates in MultiTemplate, formatted with indentation for readability. Each template's name and content are included.
type Node ¶
type Node struct { // Parent points to the node's parent in the variable tree, if any. Parent *Node // Start is the index of the first character of the node text in the parent template. Start int // End is the index of the last character of the node text in the parent template. End int // Text contains the literal text of the node or the resolved value of a variable. Text String // Children holds any child nodes, representing nested variables. Children []*Node }
Node represents a single variable or literal within a template. It can have child nodes, allowing for nested variable resolution.
func (*Node) AppendAlpha ¶
func (n *Node) AppendAlpha()
AppendAlpha adds the ALPHA_RUNE to the node's text, indicating the start of a variable.
func (*Node) AppendOmega ¶
AppendOmega adds the OMEGA_RUNE to the node's text, indicating the end of a variable.
func (*Node) AppendRunes ¶
AppendRunes adds runes to the node's text, updating the node and its ancestors accordingly.
func (Node) Debug ¶
Debug returns a detailed string representation of the node, including indices and child nodes.
func (Node) DeepStart ¶
DeepStart calculates the absolute start index of the node's text within the root template.
func (Node) Execute ¶
func (n Node) Execute(assignments Assignments) (result string, clone *Node)
Execute clones the Node and executes the Assignments on the clone.
Returns the result as well as the clone.
func (*Node) ExecuteInPlace ¶
func (n *Node) ExecuteInPlace(assignments Assignments) (anyChange bool)
ExecuteInPlace modifies the Node using the data from the Assignments.
To select how "@_" gets evaluated, set:
assignments.Assign("", "_") // default value
Returns when no more assignments are left to assign.
type ParseState ¶
type ParseState struct { Tokens Tokens // Tokens represents the lexed tokens of the template string. Cursor int // Cursor represents the current position in the token slice. }
ParseState represents the state of parsing a template string, tracking tokens and cursor position.
func NewState ¶
func NewState(s string) *ParseState
NewState initializes a new ParseState for parsing a template string.
func (ParseState) Debug ¶
func (s ParseState) Debug() string
Debug returns a detailed string representation of the parse state, indicating the cursor position.
func (ParseState) NextToken ¶
func (s ParseState) NextToken() Token
NextToken returns the next token in the sequence, or EOF if at the end.
func (*ParseState) Parse ¶
Parse processes tokens from the current cursor position to construct a Node tree.
func (ParseState) PeekNext ¶
func (s ParseState) PeekNext(kind TokenKind) (int, bool)
PeekNext searches for the next occurrence of a token of the specified kind and returns its position.
func (ParseState) String ¶
func (s ParseState) String() string
String returns a string representation of the current parse state, including tokens.
type String ¶
type String = stringutil.String
type Template ¶
func ParseTemplate ¶
ParseTemplate is similar to ParseTemplate but includes logging support for parsing.
func (Template) Debug ¶
Debug returns a detailed string representation of the template for debugging purposes.
func (Template) Dot ¶
Dot generates a Graphviz dot graph representation of the template and its variables.
func (Template) Execute ¶
func (t Template) Execute(assignments Assignments) (result string, clone *Template)
Execute clones the Node and executes the Assignments on the clone.
Returns the result as well as the clone.
func (*Template) ExecuteInPlace ¶
func (t *Template) ExecuteInPlace(assignments Assignments) (anyChange bool)
ExecuteInPlace modifies the Template using the data from the Assignments.
To select how "@_" gets evaluated, set:
assignments.Assign("", "_") // default value
Returns when no more assignments are left to assign.
type TokenKind ¶
type TokenKind uint
TokenKind represents the type of lexical token identified in template strings.
type Tokens ¶
type Tokens []Token
Tokens is a slice of Token instances, representing a sequence of tokens lexed from a template string.