# Bunt teaching plan file (`.buntplan`)

A `.buntplan` file carries one or more **base plans** for the Bunt teacher app. A base plan orders
the subject matter of a school year into folders and units; Bunt makes a copy of it for every course
that works with it. Teachers share plans with colleagues this way, and AI agents can write a plan
that a teacher then opens in Bunt.

This folder is the published contract:

| File | Purpose |
|---|---|
| `teachingplan-1.schema.json` | JSON Schema (draft 2020-12), `$id` `https://bunt-app.com/teaching-plan-file/teachingplan-1.schema.json` |
| `example.buntplan` | A complete, valid file |
| this README | How to write a file, and what Bunt does with it |

## The file in one look

```json
{
  "format": "bunt.teachingplans",
  "schemaVersion": 1,
  "plans": [
    {
      "title": "Mathematik 7",
      "descriptionText": "Stoffverteilung für das Schuljahr.",
      "items": [
        {
          "kind": "group",
          "title": "Terme und Gleichungen",
          "color": "#4A90D9",
          "children": [
            {
              "kind": "unit",
              "title": "Terme aufstellen",
              "text": {
                "schemaVersion": 1,
                "blocks": [
                  { "style": { "type": "heading", "level": 2 }, "runs": [{ "text": "Ziele" }] },
                  { "style": { "type": "list", "list": "bullet" }, "runs": [{ "text": "Terme aus Sachsituationen aufstellen" }] },
                  { "runs": [{ "text": "Einstieg", "traits": 1 }, { "text": " mit Streichholzmustern." }] }
                ]
              },
              "plannedLessonCount": 3,
              "curriculumReference": "KLP 3.2",
              "links": [{ "title": "Material", "url": "https://example.org/material" }],
              "homework": [{ "schemaVersion": 1, "blocks": [{ "runs": [{ "text": "S. 42, Aufgaben 1–4" }] }] }]
            }
          ]
        }
      ]
    }
  ]
}
```

- **File**: UTF-8 JSON, extension `.buntplan`, media type `application/vnd.bunt.teachingplan+json`.
  With attached files, the same extension holds a ZIP archive instead (see below).
- **Envelope**: `format` is always `"bunt.teachingplans"`, `schemaVersion` is `1`.
- **Plan**: only `title` is required. `descriptionText` and `items` are optional.
- **Items**: an ordered tree. A `group` is a folder with a `title`, an optional `color`
  (`#RRGGBB`, never transparent) and `children`; a `unit` is what is
  taught, with `title`, `text`, `plannedLessonCount`, `curriculumReference`, `links` and `homework`.
  A unit has no `children`. The order in the file is the order in the plan.
- **Text** (`text`, every entry of `homework`) is a formatted text object — see below.

## Files attached to a unit

A `.buntplan` that carries files is a **ZIP archive** with the extension `.buntplan` (media type
`application/vnd.bunt.teachingplan+zip`). Bunt tells the two forms apart by the first bytes, never
by the name: `PK\x03\x04` is an archive, anything else is read as JSON (a UTF-8 byte order mark and
leading white space are fine).

```
plan.json                    the JSON described here
assets/arbeitsblatt-1.pdf    the files the units name
assets/tafelbild.png
```

- The archive holds exactly **one** `plan.json` (in lower case), with `assets/` beside it, at the
  top of the archive or inside **one** folder, as the Finder or the Windows Explorer compress a
  folder. Other entries (`__MACOSX/`, `.DS_Store`, folders, files no unit names) are never read, but
  count towards the size of the archive.
- A unit names its files in `files`:
  `{ "title": "Arbeitsblatt", "fileName": "Arbeitsblatt Brüche.pdf", "path": "assets/arbeitsblatt-1.pdf" }`.
  `path` is `assets/` and a name of up to 100 ASCII letters, digits, `.`, `_` and `-` that begins
  with a letter or a digit, so every ZIP tool writes it alike. It must match the name of an entry
  byte for byte, with `/` between folder and name (never `\`), counted from the folder of
  `plan.json`. `fileName` is the name the file is saved under, in any language; left out, it is the
  last part of `path`. Where `title` is empty, the unit shows the attachment under `fileName`. Bunt
  saves a file under a name every file system takes: no folders, control or direction characters,
  `: < > " | ? *`, dots or spaces at the start or end, a `_` in front of names Windows reserves
  (`CON`, `NUL`, `COM1` …). Several units may name the same file; it is saved once.
- The archive is a ZIP file within **ISO/IEC 21320-1**: entries stored or deflated, no encryption,
  one volume. Python's `zipfile` with `ZIP_DEFLATED`, `java.util.zip` and the Windows Explorer write
  such files. Bunt refuses an archive it could read in two ways: anything before the first entry (a
  self-extracting stub), gaps between entries, data after a compressed entry, a local header or data
  descriptor that differs from the central directory, an end record hidden in the comment or
  contradicting the ZIP64 one, bytes after the end record, ZIP64 version 2, a name with a NUL byte,
  two entries whose names differ only in case or Unicode normalization.
- Every file a unit names must be in the archive; a plain JSON `.buntplan` whose units name files
  is refused.

An archive written in Python, with the standard library only:

```python
import json
import zipfile

plan_file = {
    "format": "bunt.teachingplans",
    "schemaVersion": 1,
    "plans": [{
        "title": "Mathematik 7",
        "items": [{
            "kind": "unit",
            "title": "Terme aufstellen",
            "files": [{
                "title": "Arbeitsblatt",
                "fileName": "Arbeitsblatt Terme.pdf",
                "path": "assets/arbeitsblatt-terme.pdf",
            }],
        }],
    }],
}

with zipfile.ZipFile("Mathematik 7.buntplan", "w", zipfile.ZIP_DEFLATED) as archive:
    archive.writestr("plan.json", json.dumps(plan_file, ensure_ascii=False, indent=2))
    archive.write("arbeitsblatt-terme.pdf", arcname="assets/arbeitsblatt-terme.pdf")
```

`writestr` stores a string as UTF-8. The name in the archive is the `path` of the unit, never
`fileName`: for a file named in another language, make `path` from its name with every character
that is not an ASCII letter, a digit, `.`, `_` or `-` replaced by `-`. Bunt's own export also puts
a number in front, which keeps two such paths apart (`assets/1-Arbeitsblatt-Br-che.pdf`).

## Formatted text in `text`

Every `text` and every entry of `homework` is an **object**, never a string: a list of blocks, as
the Bunt editor holds it. The schema describes every part of it under `$defs/richTextDocument`.

```json
{
  "schemaVersion": 1,
  "blocks": [
    { "style": { "type": "heading", "level": 2 }, "runs": [{ "text": "Stationen" }] },
    {
      "style": {
        "type": "table",
        "table": {
          "hasHeaderRow": true,
          "rows": [
            { "cells": [{ "runs": [{ "text": "Station" }] }, { "runs": [{ "text": "Aufgabe" }] }] },
            { "cells": [{ "runs": [{ "text": "1" }] }, { "runs": [{ "text": "Terme " }, { "text": "vereinfachen", "traits": 1 }] }] },
            { "cells": [{ "runs": [{ "text": "2" }] }, { "runs": [] }] }
          ]
        }
      }
    },
    { "style": { "type": "list", "list": "checklist" }, "runs": [{ "text": "Material kopiert" }] },
    { "alignment": "center", "runs": [{ "text": "Wichtig", "highlightColor": "#FFE066" }] }
  ]
}
```

| Block | `style` |
|---|---|
| Paragraph | absent, or `{"type":"paragraph"}` |
| Heading | `{"type":"heading","level":1}` … `3` |
| List item | `{"type":"list","list":"bullet"}`, `"ordered"` or `"checklist"` (`"isChecked"` is allowed, but Bunt imports every checklist unchecked and never exports a check) |
| Table | `{"type":"table","table":{…}}` with `rows`, each with `cells`, each with `runs` |

- A block may also carry `indent` (0–8: the nesting of a list item, or the indentation of a
  paragraph) and `alignment` (`"natural"`, `"center"`, `"trailing"`).
- A **run** is text with one formatting: `text`, optional `traits` (added up: 1 bold, 2 italic,
  4 underline, 8 strikethrough), `foregroundColor` and `highlightColor` (`#RRGGBB` or `#RRGGBBAA`)
  and `link` (`http`/`https` only; any other address is removed, the words stay). A run never holds
  a line break: a new line is a new block. Leave the text colour out for ordinary text — it then
  follows light and dark mode.
- An ordered list is numbered by its order; no number is written.
- A **table** has up to 10 columns and 50 rows. `hasHeaderRow` (default `true`) shows the first row
  as a header; `columnWidths` (points, `null` for automatic) and a row's `minimumHeight` are
  optional. A cell holds runs only — no headings or lists; a new line inside a cell is `\n`. An
  empty cell is `{"runs":[]}`.
- Blocks carry no id: Bunt gives every block its own.
- **Validate against the schema** before you hand the file over: it lists every key, and any other
  is an error. Bunt itself reads less strictly. A value it cannot read at all — a string where a
  text object belongs, a value of the wrong type, a malformed colour, an alignment that does not
  exist — makes it **refuse the whole file**; nothing is imported. What it can read leniently it
  takes: unknown keys are dropped, a missing list is empty, an indent beyond 8 becomes 8, a run with
  a line break is cut into a block per line, a table of more than 50 rows is kept whole. Only a valid file is sure to look as written.

## Limits

A file that breaks one of these is refused as a whole; nothing is imported. The exceptions are
`descriptionText` and `fileName`, which are cut to their limit instead.

| What | Limit |
|---|---|
| File size | 5 MB (JSON; `plan.json` in an archive) |
| Archive size | 510 MB; at most 5000 entries; 520 MB unpacked, every entry counted |
| Attached files, unpacked, together | 500 MB; at most 100 per unit |
| `fileName` | 200 bytes (UTF-8); a longer one is cut, its extension kept |
| Plans per file | 50 |
| Items (folders + units) per plan | 2000 |
| Nesting depth | 12 levels |
| Items, links, homework and attached files per file, added up | 10 000 |
| `title`, `curriculumReference`, link `title` | 500 characters, one line |
| `descriptionText` | 10 000 characters (longer text is cut, not refused) |
| `text`, each `homework` entry | 256 KB (UTF-8) as Bunt stores it: every block with an id (about 50 bytes) and every left-out default written out |
| Links per unit, homework entries per unit | 100 each |
| Link `url` | 2048 characters, `http`/`https`, a host, no user name |
| `plannedLessonCount` | 0 … 32 767 |

## Recommendations for agents

- **One plan per file**, well under 500 KB. A teacher looks at a preview of the whole file before
  it is imported and confirms it; one plan is what they expect to see.
- **At most three levels**: folders for the big topics, units inside them, and — rarely — a
  sub-folder. Deeper trees are hard to read on a phone.
- **Texts of up to about 1000 characters per unit.** Goals, steps, materials — in headings, lists
  and tables, not in prose. Long texts belong into linked material.
- **Say what a unit takes** with `plannedLessonCount`, in lessons (one calendar lesson = 1). It is a
  guide for every course: Bunt shows it on the unit, and once a course has taught the unit, how
  many lessons it took where that differs. Whether a unit is done follows from the lessons a
  teacher links to it in her course, never from this number.
- **Use `curriculumReference`** for the curriculum's own identifier, if there is one.
- **Homework** goes into `homework`, one entry per assignment, in the order it is given. In a
  course plan the teacher gives each a lesson it is due in, and Bunt makes a reminder of it.
- **Links** are web addresses only. Files travel only in the archive form.
- **Short, telling titles** for folders and units: a few words, about 40 characters at most, such
  as "Lineare Gleichungen", not "Einführung in das Lösen linearer Gleichungen mit einer Variablen".
  Titles are read in the outline beside the plan, where a long one is cut off, and the title of a
  unit can become the name of a grading. Put what explains the topic into the unit's `text`.
- Set `title` on every unit. It may be empty, but an empty title shows as a placeholder.
- Write the texts in the teacher's language. The file carries no language field.

## What Bunt does with the file

1. The user opens the file (Mail, Files, AirDrop, Safari) or picks it in Bunt's plan library.
2. Bunt reads it, brings every text into its stored form and checks the limits. A file that fails is
   refused with a message; nothing is written.
3. Bunt shows a **preview**: every plan with its description and how many folders,
   units, links and homework it holds — and how many links in texts were removed because they open
   no browser. For an archive it also says how many files it carries and how large they are, and
   where they will be saved: in Bunt's own documents or in a folder the user chooses. On iOS and
   iPadOS the Files app does not show Bunt's documents: the files are opened through their units,
   and a file there is removed once no unit names it any more.
4. Only after the user confirms, the plans are created as **new base plans** at the end of the
   library. Nothing is matched against existing plans: a file imported twice is there twice.
5. The files are saved into a **new folder per plan**, named after the plan and numbered where a
   folder of that name exists. A file is saved once, however many units name it — in the folder of
   the first plan that names it —, under its `fileName`, numbered where that name is taken; nothing
   is replaced. A file that cannot be saved is left out and counted; the plans are created all the
   same.

Bunt's own export writes the same format, one plan at a time, texts included, and valid against the
schema of its version. Where files attached to the plan's units lie on the device, it asks whether
they go along and then writes the archive form, each file once: `path` is `assets/`, a number and
the name with every character outside the pattern as `-`, `fileName` the name composed (NFC). Files
that are not on the device (not downloaded from iCloud, moved, deleted) stay behind and are
reported; where the files come to more than 500 MB, only the plan without them is offered. What only
a newer Bunt version could show (a block type, a character format) is written as plain text. The one
exception is a table of more than 50 rows that once came in from a file; it goes out as it came.

## Versioning

`schemaVersion` is bumped only for a change an older app could not read. Fields may be added
without a bump; the schema is then extended in place. An older Bunt ignores fields it does not know,
and writers should not rely on unknown fields being kept.
