Documentation
¶
Overview ¶
Package mdma is a from-scratch Go implementation of the MDMA templating language: a typed Markdown templating format for generating one or more Markdown strings from a declared @inputs schema.
See https://dastfox.github.io/mdma/ for the full language specification. This package mirrors python-mdma and typescript-mdma module-for-module.
Index ¶
- Constants
- func ValidateInputs(source string, inputs map[string]any) (map[string]any, error)
- func WriteOutput(result *RenderResult, outputDir string, block string) ([]string, error)
- type ArrayLiteral
- type BinOp
- type Block
- type DuplicateNameError
- type Error
- type Expr
- type ExprNode
- type FilterCall
- type FilterError
- type ForNode
- type IfBranch
- type IfNode
- type InputDecl
- type Literal
- type MissingInputError
- type Node
- type Not
- type OrderedMap
- type ParsedTemplate
- type ReferenceError
- type RenderResult
- type RenderedValue
- type SyntaxError
- type TextNode
- type TypeError
- type Var
Constants ¶
const Version = "0.2.0"
Version is the current mdma-go package version, bumped alongside release tags.
Variables ¶
This section is empty.
Functions ¶
func ValidateInputs ¶
ValidateInputs checks an inputs map against an .mdma source's @inputs declarations without rendering. Returns the resolved inputs (declared defaults applied). Returns *MissingInputError when a required input is absent and *TypeError when a value does not match its declared type.
func WriteOutput ¶
func WriteOutput(result *RenderResult, outputDir string, block string) ([]string, error)
WriteOutput writes one or all rendered blocks from a Render() result to .md files.
block == "" (default) writes every top-level block, in file declaration order; block = "name" writes only that one. A string-valued block is written to {outputDir}/{block}.md. A `multiple` block ([]string, or an *OrderedMap if it also declared `name`) is written to {outputDir}/{block}/, one file per item -- {name}.md if the block declared `name`, otherwise {index}.md.
Returns a *ReferenceError if block isn't present in result.
Types ¶
type ArrayLiteral ¶
type ArrayLiteral struct{ Items []Expr }
ArrayLiteral is an `[a, b, ...]` expression.
type BinOp ¶
type BinOp struct {
Op string // "and" | "or" | "==" | "!=" | ">" | ">=" | "<" | "<="
Left, Right Expr
}
BinOp is a binary `and`/`or`/comparison expression.
type Block ¶
type Block struct {
Name string
MultipleVar string // "" if this isn't a `multiple` block
MultipleSource string
NameExpr Expr // non-nil only alongside MultipleVar
Body []Node
}
Block is a single parsed <name> block.
type DuplicateNameError ¶
type DuplicateNameError struct{ ComputedName, BlockName string }
DuplicateNameError reports two items in a `multiple:`/`name:` block that computed the same name.
func (*DuplicateNameError) Error ¶
func (e *DuplicateNameError) Error() string
type Error ¶
type Error interface {
error
// contains filtered or unexported methods
}
Error is implemented by every mdma-specific error type. Use errors.As with a concrete type (*SyntaxError, *MissingInputError, etc.), or with this interface as a catch-all for "any mdma error".
type Expr ¶
type Expr interface {
// contains filtered or unexported methods
}
Expr is an expression AST node: Literal, ArrayLiteral, Var, Not, BinOp, or FilterCall.
type FilterCall ¶
FilterCall is a `target | name(args...)` expression.
type FilterError ¶
type FilterError struct{ FilterName, ExpectedType string }
FilterError reports a filter applied to a value of the wrong type.
func (*FilterError) Error ¶
func (e *FilterError) Error() string
type IfNode ¶
type IfNode struct{ Branches []IfBranch }
IfNode is an `{% if %}...{% elif %}...{% else %}...{% endif %}` chain, flattened into an ordered list of branches evaluated top-to-bottom.
type InputDecl ¶
type InputDecl struct {
Name string
Type string // one of: string, string[], number, number[], boolean, object, object[]
HasDefault bool
Default any
}
InputDecl is a single @inputs declaration.
type MissingInputError ¶
type MissingInputError struct{ Name string }
MissingInputError reports a required input (no default) missing at render time.
func (*MissingInputError) Error ¶
func (e *MissingInputError) Error() string
type Node ¶
type Node interface {
// contains filtered or unexported methods
}
Node is a block-body AST node: TextNode, ExprNode, IfNode, or ForNode.
type OrderedMap ¶
type OrderedMap struct {
// contains filtered or unexported fields
}
OrderedMap is a string-keyed, insertion-ordered map. Go maps have no defined iteration order, but a `multiple`+`name` block's computed order is semantically part of the render result, so it's tracked explicitly here.
func (*OrderedMap) Get ¶
func (m *OrderedMap) Get(key string) (string, bool)
Get returns the value for key and whether it was present.
func (*OrderedMap) Has ¶
func (m *OrderedMap) Has(key string) bool
Has reports whether key has been set.
func (*OrderedMap) Keys ¶
func (m *OrderedMap) Keys() []string
Keys returns the keys in insertion order.
func (*OrderedMap) Set ¶
func (m *OrderedMap) Set(key, value string)
Set adds or overwrites key with value, appending key to the iteration order only the first time it's set.
type ParsedTemplate ¶
ParsedTemplate is the result of ParseFile: an @inputs schema plus the file's blocks, in declaration order.
func ParseFile ¶
func ParseFile(source string) (*ParsedTemplate, error)
ParseFile parses a full .mdma source string into its @inputs declarations and blocks.
type ReferenceError ¶
type ReferenceError struct{ Message string }
ReferenceError reports a block or variable reference that couldn't be resolved.
func ForwardBlockError ¶
func ForwardBlockError(name string) *ReferenceError
ForwardBlockError reports a reference to a block declared later in the file, which hasn't rendered yet.
func UndefinedError ¶
func UndefinedError(name string) *ReferenceError
UndefinedError reports a reference to a name that is neither a binding, a block, nor a declared input.
func (*ReferenceError) Error ¶
func (e *ReferenceError) Error() string
type RenderResult ¶
type RenderResult struct {
// contains filtered or unexported fields
}
RenderResult is the output of Render/RenderTemplate/RenderFile: each block's name mapped to its rendered value, preserving file declaration order (Go maps have no iteration order, but WriteOutput's default -- write every block -- relies on that order, matching Python dict / JS object insertion-order behavior).
func Render ¶
func Render(source string, inputs map[string]any) (*RenderResult, error)
Render parses and renders an .mdma source string against an inputs map.
Returns a *RenderResult mapping each block name to its rendered value. A `multiple` block renders to a []string, or -- if it also declares `name` -- to an *OrderedMap keyed by each item's computed name.
func RenderFile ¶
func RenderFile(path string, inputs map[string]any) (*RenderResult, error)
RenderFile reads path as UTF-8 and renders it. Equivalent to Render(string(contents), inputs).
func RenderTemplate ¶
func RenderTemplate(tmpl *ParsedTemplate, inputs map[string]any) (*RenderResult, error)
RenderTemplate renders an already-parsed template (from ParseFile) against an inputs map. Same semantics as Render, minus the parse -- parse once, render many times.
func (*RenderResult) Get ¶
func (r *RenderResult) Get(name string) (RenderedValue, bool)
Get returns the rendered value for a block name.
func (*RenderResult) Names ¶
func (r *RenderResult) Names() []string
Names returns all block names in file declaration order.
type RenderedValue ¶
type RenderedValue = any
RenderedValue is the rendered output of a single block: a string (plain block), a []string (`multiple` block), or an *OrderedMap (`multiple` block that also declares `name`).
type SyntaxError ¶
type SyntaxError struct{ Message string }
SyntaxError reports .mdma source that does not conform to the grammar.
func (*SyntaxError) Error ¶
func (e *SyntaxError) Error() string