Documentation ¶
Overview ¶
Package generator contains functionality to generate randomized, structured text based on a mixture of randomized selection and configured selections.
Index ¶
Constants ¶
const RoundsMax = uint8(30)
RoundsMax defines the maximum number of replacement rounds allowed during Rendering.
Variables ¶
This section is empty.
Functions ¶
func Render ¶
Render generates output from the supplied instruction string using the Inventory, State and RandomSource. The instructions are rendered by replacing one element at a time, selecting the first complete Token or first complete Variable found (in that order). Tokens or Variables which include other Token or Variable references are considered invalid/incomplete and will be skipped.
Types ¶
type Generator ¶
type Generator struct {
// contains filtered or unexported fields
}
Generator is a reusable text generator which potentially produces different output each time it is used.
func CreateGenerator ¶
CreateGenerator creates a reusable text generator based on the instructions provided and the given inventory. Each time the generator is used, it can potentially produce different output.
func (*Generator) RunWithState ¶
RunWithState executes the generator with the supplied State. This function is used to execute Generators with pre-defined state for instruction sets that require variable substitution.
func (*Generator) UseRandomSource ¶
func (g *Generator) UseRandomSource(rng rng.RandomSource)
UseRandomSource assigns a RandomSource to use when picking tokens.
type Inventory ¶
type Inventory struct {
// contains filtered or unexported fields
}
Inventory acts as a collection of categorized Tokens which can be queried for both randomized and parameterized selection.
func CreateInventory ¶
func CreateInventory() *Inventory
CreateInventory creates a new, empty Inventory.
func (*Inventory) Add ¶
Add adds an existing Token to this Inventory. The added token is returned, to help support chaining and make it interchangeable with AddToken.
func (*Inventory) AddToken ¶
func (i *Inventory) AddToken(id string, content string, rarity float64, tags map[string]string) *Token
AddToken creates and adds a new Token to this Inventory. The created token is returned to support chaining and to make it interchangeable with Add.
type Properties ¶
Properties defines the structure used to store token properties.
type Selector ¶
type Selector struct { Category string Require map[string]string Exclude map[string]string Exists map[string]bool }
Selector acts as a structured query for Tokens, describing the Category and required/excluded characteristics necessary for selection.
func ParseSelector ¶
ParseSelector examines string parts to create a new Selector.
func (*Selector) IsSimple ¶
IsSimple checks to see if the Selector only selects based upon its category.
func (*Selector) MatchesToken ¶
MatchesToken checks if the selector would select the supplied Token.
type State ¶
State includes configurable state that can be used to configure individual executions of a Generator.
type Token ¶
type Token struct { Category string Content string Rarity float64 Properties map[string]string SetVars map[string]string }
Token represents a single item which can be placed into the generated output of a Generator.
func BuildToken ¶
func BuildToken(category string, content string, rarity float64, tags Properties) Token
BuildToken builds a new Token based upon the supplied information. No SetVars are defined. If you want to set State variables on render, use OnRenderSet to define them.
func (*Token) IsValid ¶
IsValid checks to see if this Token is valid and usable for generation. Invalid Tokens may become valid if Normalize is called on them, but this is not guaranteed. If you are reading or creating Tokens with non-validated input, you should call Normalize before checking validity.
func (*Token) Normalize ¶
func (t *Token) Normalize()
Normalize updates the Token to ensure that it matches required behaviors. Categories must not start or end with whitespace. Rarities must not be zero or negative. If the Rarity is invalid, it is set to a default of 1.0
func (*Token) OnRenderSet ¶
OnRenderSet defines a State variable to set when this Token is rendered to Generator output.