Metadata-Version: 2.4
Name: spellbook-engine
Version: 0.1.0
Classifier: Programming Language :: Rust
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Text Processing :: Markup :: Markdown
Classifier: Typing :: Typed
License-File: LICENSE
Summary: The SpellBlock syntax engine, for Python.
Keywords: markdown,parser,spellbook,django
License-Expression: AGPL-3.0-only
Requires-Python: >=3.10
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: Repository, https://git.stormdevelopments.ca/official-public/spellbook-engine

<img src="icon.svg" alt="" width="96" align="right">

# spellbook-engine

[![CI](https://git.stormdevelopments.ca/official-public/spellbook-engine/actions/workflows/test.yml/badge.svg)](https://git.stormdevelopments.ca/official-public/spellbook-engine/actions)
[![License: AGPL-3.0](https://img.shields.io/badge/license-AGPL--3.0-blue.svg)](https://git.stormdevelopments.ca/official-public/spellbook-engine/src/branch/main/LICENSE)
![33 tests](https://img.shields.io/badge/tests-33-blue.svg)
![4 source files](https://img.shields.io/badge/source_files-4-blue.svg)
![0 dependencies](https://img.shields.io/badge/dependencies-0-blue.svg)

The SpellBlock syntax engine. It splits a markdown document into plain
markdown and `{~ block ~}` segments, and refuses a document the syntax forbids.

It renders nothing. A host (Django Spellbook, a static site generator) takes
the segments, renders the markdown with its own markdown library and the blocks
with its own templates. One engine means every host agrees on what a document
means.

## The syntax

```text
{~ alert type="warning" ~}
Markdown goes **here**.
{~~}

{~ hr .divider /~}
```

A block opens with `{~ name ~}` and closes with `{~~}`. A block with no body
closes itself: `{~ name /~}`. Attributes are `key="value"`, `key='value'`,
`key=value`, a bare `flag`, `.class` and `#id`. Tags inside a code fence are
left alone. The whole grammar is in [SPEC.md](SPEC.md).

## One rule about nesting

A block with a body cannot sit inside another block. A self-closing block can.

```text
{~ cards ~}
{~ tile name="Admin" /~}
{~ tile name="Read-Only" /~}
{~~}
```

That is the whole of nesting, on purpose. A document that breaks the rule is an
error with a line number, never broken output.

## Use

```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"
);
```

## 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 Python

The `python/` folder is the Python glue: the same engine as the module
`spellbook_engine`, with no logic of its own. See `python/README.md`.

## Development

`make gates` runs what CI runs: formatting, clippy with every warning an error,
the tests, the docs build, the advisory scan and a packaging check. `make python`
builds the Python glue and runs its tests, and `make python-dist` builds what is
uploaded and tests an install of each file.

## License

AGPL-3.0. The full text is in the `LICENSE` file.

Maintained by [Storm Developments](https://stormdevelopments.ca).

