The engine
spellbook-engine splits a markdown document into plain markdown and block segments, and refuses a document the syntax forbids. It renders nothing. A host takes the segments, renders the markdown with its own markdown library and the blocks with its own templates. dj-spellbook is one host. One engine means every host agrees on what a document means.
What it does not do
- Render markdown or HTML.
- Know which block names exist. An unknown name is the host's error to raise.
- Depend on anything. The crate has no dependencies and no
unsafe.
From Rust
use spellbook_engine::{parse, Node};
let doc = parse("Intro.\n\n{~ alert type=\"info\" ~}\nBody.\n{~~}\n").unwrap();
let Node::Block(alert) = &doc.nodes[1] else { panic!() };
assert_eq!(alert.name, "alert");
assert_eq!(alert.get("type"), Some("info"));
assert_eq!(alert.body.as_ref().unwrap().source, "\nBody.\n");
let err = parse("{~ a ~}\n{~ b ~}\n{~~}\n{~~}").unwrap_err();
assert_eq!(
err.to_string(),
"line 2, column 1: block `b` opens inside `a`: only a self-closing block may sit inside a block"
);
parse returns the document's nodes in source order: Markdown slices, exactly as written, and Blocks. An empty slice is dropped. A block's body.source is its body exactly as written; body.nodes is the same body split.
A Block keeps its attributes in the order written, and offers the merged readings every host should share:
get(key): the last pair namedkey.classes():.nameshorthands first, then each word of everyclass="...", each class once.id():id="..."if written, else the first#name.
Errors
A refused document gives an Error with three fields:
kind: anErrorKind, one ofUnclosedTag,StrayClose,UnclosedBlock,NestedBlock,MissingName,UnexpectedCharacter,MissingValueandUnterminatedValue. The enum is#[non_exhaustive], so amatchon it needs a wildcard arm.line: 1-based.column: 1-based, counted in characters.
Its Display is the message, line N, column C: reason. Refusals and the check lists what each kind says.
From Python
The spellbook_engine module is the same engine, glued to Python with PyO3. It holds no logic of its own: every answer is the Rust engine's.
import spellbook_engine
nodes = spellbook_engine.parse('Intro.\n\n{~ alert type="info" ~}\nBody.\n{~~}\n')
intro, alert, rest = nodes
assert intro == "Intro.\n\n" # markdown comes back as str
assert alert.name == "alert"
assert alert.get("type") == "info"
assert alert.body.source == "\nBody.\n"
try:
spellbook_engine.parse("{~ a ~}\n{~ b ~}\n{~~}\n{~~}")
except spellbook_engine.SpellbookSyntaxError as error:
assert (error.kind, error.line, error.column) == ("NestedBlock", 2, 1)
parse(source)returns a list:strfor markdown,Blockfor a block.Blockhasname,line,attrs,body, and the shared readingsget(),has_flag(),classes()andid().Bodyhassource, the body as written, andnodes, the same body split. A block inside a body is always self-closing.SpellbookSyntaxErroris aValueErrorwithkind,lineandcolumn.
The wheel is for Linux on x86-64, glibc 2.17 or newer. Anywhere else pip builds from the source archive, which needs a Rust toolchain.
Licence
AGPL-3.0, like dj-spellbook.