dj-spellbook
Renders markdown that contains SpellBlocks into HTML, inside a Django project. A SpellBlock is a named block an author writes in the markdown. A Django template renders it. The markdown around it renders as CommonMark, with one change: a new line inside a paragraph is a line break.
## Before you deploy
{~ alert type="warning" ~}
Take a backup **first**.
{~~}
from dj_spellbook import spellbook_render
html = spellbook_render(document)
<h2>Before you deploy</h2>
<aside class="spell-callout spell-callout--warning">
<div class="spell-callout-body">
<p>Take a backup <strong>first</strong>.</p>
</div>
</aside>
html is a SafeString, so a template prints it with {{ html }} and no
|safe.
Install
pip install dj-spellbook
Python 3.12 or newer, Django 5.2 or newer. It depends on Django, spellbook-engine, which decides where each block starts and ends, and markdown-it-py. The engine's wheel is built for Linux x86-64. Anywhere else pip builds it from source and needs a Rust toolchain.
Add "dj_spellbook" to INSTALLED_APPS. That gives you the three built-in
blocks and their stylesheet, and finds the blocks your own apps define.
The syntax
A block with a body opens with its name and closes alone on a line. A block
with no body closes itself. Arguments are key="value" pairs and bare flags.
.name adds a class and #name sets the id.
{~ card title="Pricing" footer="Billed monthly in CAD" .wide #pricing ~}
One block, one price.
{~~}
{~ accordion title="Why a block?" open ~}
So the words and the markup stay apart.
{~~}
{~ alert type="info" /~}
A block with a body cannot sit inside another block. A block that closes itself can. The full grammar is the engine's SPEC.md.
Writing a SpellBlock
A SpellBlock is a class and a template. Put the class in a spellblocks.py in
any installed app. It is found at startup, with no setting and no URL config. A
spellblocks.py that fails to import stops startup with its error.
from dj_spellbook import BasicSpellBlock, SpellBlockRegistry
@SpellBlockRegistry.register()
class StepsBlock(BasicSpellBlock):
name = "steps"
arguments = ("here",)
template = "myapp/steps.html"
def vet(self):
if not self.kwargs.get("here", "1").isdigit():
raise ValueError("`here` is a step number")
def get_context(self):
here = self.kwargs.get("here", "")
items = [line[2:] for line in self.content.splitlines() if line.startswith("- ")]
steps = [{"n": f"{n:02d}", "text": text, "here": here == str(n)} for n, text in enumerate(items, 1)]
return {"steps": steps}
myapp/templates/myapp/steps.html:
<ol class="steps">{% for s in steps %}<li class="step{% if s.here %} step--here{% endif %}"><span class="step-n">{{ s.n }}</span>{{ s.text }}</li>{% endfor %}</ol>
{~ steps here="3" ~}
- Tested locally
- Tested on staging
- The alpha
- Customer nodes
{~~}
<ol class="steps"><li class="step"><span class="step-n">01</span>Tested locally</li><li class="step"><span class="step-n">02</span>Tested on staging</li><li class="step step--here"><span class="step-n">03</span>The alpha</li><li class="step"><span class="step-n">04</span>Customer nodes</li></ol>
What a block has to work with:
nameis what the author types. Two blocks cannot share one.argumentslists the arguments the block accepts, and it refuses any other. A block that lists none accepts anything..classand#idare always accepted.self.contentis the body exactly as written, blocks inside it unrendered, orNonefor a block that closes itself.self.kwargsholds every argument as a string: a pair under its key, a flag under its own name (opengives"open"), the classes joined by spaces underclass, and the id underid.process_content()returns the body rendered to HTML, blocks inside it included.get_context()returns what the template gets. By default that iscontent, the rendered body, plus every key ofself.kwargs.vet()refuses a bad value by raisingValueError. It runs before any template does. By default it accepts everything.
The built-ins
| Block | Arguments | Renders |
|---|---|---|
alert |
type: info (the default), warning, success or danger |
<aside> |
card |
title, footer, each printed only when given |
<article> |
accordion |
title, and the flag open to start it open |
<details> |
Their class names are stable, so your own CSS can hang off them:
spell-callout,spell-callout--info,spell-callout--warning,spell-callout--success,spell-callout--danger,spell-callout-bodyspell-card,spell-card-head,spell-card-body,spell-card-footspell-accordion,spell-accordion-summary,spell-accordion-body
An author's .class names are added after the built-in class, and #id
becomes the element's id.
The stylesheet
One small file styles the three built-ins and nothing else.
{% load static %}
<link rel="stylesheet" href="{% static 'dj_spellbook/spellbook.css' %}">
It reads these variables: --spell-radius, --spell-gap, --spell-border,
--spell-surface, --spell-info, --spell-warning, --spell-success,
--spell-danger. Set them, or override the rules, in your own CSS loaded after
it. Or leave the file out: a block you write yourself carries your own classes
and your own CSS, and needs nothing from this one.
Refusals and the check
A wrong document is refused, never partly rendered. spellbook_render raises
SpellbookRefusal, a ValueError with line, column and reason. It is
raised for what the engine's syntax refuses, a block name nothing registered,
an argument a block does not list, and a value a block's vet() refuses. Only
a syntax refusal names a column.
spellbook_check returns every refusal a document would get, and renders
nothing. Run it where a document is saved or imported. A document that passes
the check is never refused at render.
{~ steps here="three" ~}
- Plan
{~~}
{~ nosuch /~}
from dj_spellbook import spellbook_check
for refusal in spellbook_check(document):
print(refusal)
line 1: `steps`: `here` is a step number
line 5: no SpellBlock is named `nosuch`
Trust
Raw HTML in a document passes through unchanged. A document is trusted: it is written by your own people. Never render a stranger's markdown with this. Values an author passes to a block reach its template as ordinary context, and Django escapes them as it escapes any other.
Next
The generator: a folder of markdown files becomes pages, with views and URLs. It is not built yet. Today dj-spellbook takes one document and returns HTML.
Licence
AGPL-3.0. Maintained by Storm Developments.
Metadata
Release files for dj-spellbook 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| dj_spellbook-0.1.0.tar.gz | 30.9 kB | Without provenance | Details for dj_spellbook-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| dj_spellbook-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for dj_spellbook-0.1.0-py3-none-any.whl |
Total release size: 54.6 kB
Release files / dj_spellbook-0.1.0.tar.gz
| Download URL | dj_spellbook-0.1.0.tar.gz |
|---|---|
| Size | 30.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fb98e19ff8a754c3cb5fe6d1bff8ef7b60670943cc379e7503a086abe17eabed
|
|
BLAKE2b-256 checksum How to use checksums |
bf2d900038762584d8082c6d011c55644b8a07e1891ad496ee05bf9a8a784fd0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.9 {"installer":{"name":"uv","version":"0.12.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Arch Linux","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / dj_spellbook-0.1.0-py3-none-any.whl
| Download URL | dj_spellbook-0.1.0-py3-none-any.whl |
|---|---|
| Size | 23.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7d0e4ca8bc7e38b90c188be0d0148836fa521640f642303feecf2b946d8772ac
|
|
BLAKE2b-256 checksum How to use checksums |
a0f3b9d720752afba3c432aad4a6de174988d4286fa9c18d06847114709900b6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.9 {"installer":{"name":"uv","version":"0.12.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Arch Linux","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Security for dj-spellbook 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.