Skip to the content
Spellbook

Refusals and the check

A wrong document is refused, never partly rendered.

SpellbookRefusal

spellbook_render raises SpellbookRefusal, a ValueError with line, column and reason. It raises the document's first refusal, before any template runs. A document is refused for:

  • what the engine's syntax refuses;
  • a block name nothing registered;
  • an argument a block does not list;
  • a value a block's vet() refuses.

Only a syntax refusal names a column. Elsewhere column is None, and the message reads line N: reason in place of line N, column C: reason.

The check

spellbook_check returns every refusal a document would get, in document order, and renders nothing. It returns an empty list when the document renders. Run it where a document is saved or imported: a document that passes the check is never refused at render.

from dj_spellbook import spellbook_check

for refusal in spellbook_check(document):
    print(refusal)

Here it finds two refusals in one document:

{~ steps here="three" ~}
- Plan
{~~}

{~ nosuch /~}
line 1: `steps`: `here` is a step number
line 5: no SpellBlock is named `nosuch`

A syntax refusal is the exception: the engine cannot split a document it refuses, so the check returns that one refusal alone.

Syntax refusals

The engine refuses a document its syntax forbids, naming a line and a column. The column is counted in characters.

KindCause
UnclosedTagA tag is opened and never ended.
MissingNameA tag has no name.
StrayCloseA close tag with no block open.
UnclosedBlockA block is never closed. Reported at its opening tag.
NestedBlockA block with a body opens inside another block.
UnexpectedCharacterA character fits no attribute form.
MissingValueA key= with nothing after it.
UnterminatedValueA quoted value has no closing quote.

Each kind, with a one-line document that gets it and the engine's message, in the order of the table:

{~ alert type="info"
  line 1, column 1: `{~` is never closed with `~}`

{~ ~}
  line 1, column 1: tag has no block name

Text. {~~}
  line 1, column 7: `{~~}` closes nothing: no block is open

{~ alert ~} Body.
  line 1, column 1: block `alert` is never closed with `{~~}`

{~ card ~} {~ alert ~} x {~~} {~~}
  line 1, column 12: block `alert` opens inside `card`: only a self-closing block may sit inside a block

{~ card ! ~} x {~~}
  line 1, column 9: unexpected `!` in a tag

{~ card title= ~} x {~~}
  line 1, column 16: attribute `title` has no value

{~ card title="Pricing ~} x {~~}
  line 1, column 15: value of `title` has no closing quote

An unclosed block, as dj-spellbook reports it:

{~ alert type="warning" ~}
Take a backup first.
line 1, column 1: block `alert` is never closed with `{~~}`

A block with a body inside another block:

{~ card title="Outer" ~}
{~ alert type="info" ~}
A body inside a body.
{~~}
{~~}
line 2, column 1: block `alert` opens inside `card`: only a self-closing block may sit inside a block

Block refusals

These come from dj-spellbook once the syntax holds, and name a line only.

A name nothing registered

{~ nosuch /~}
line 1: no SpellBlock is named `nosuch`

Register a block with that name, or fix the spelling. See writing your own block.

An argument the block does not list

{~ card title="Pricing" colour="red" ~}
One block, one price.
{~~}
line 1: `card` takes no argument `colour`; it takes `title`, `footer`

A block that lists its arguments refuses any other. A class and an id are always accepted.

A value vet() refuses

{~ steps here="three" ~}
- Plan
{~~}
line 1: `steps`: `here` is a step number

The reason after the block's name is the block's own ValueError message.