Skip to content
@deckyard/docs

Design

Scope

The deck reads, edits and writes Office Open XML documents (.docx), OpenDocument text (.odt) and RTF through one value-semantic Document; converts to and from the bundled doctree, so Markdown, reStructuredText and MyST become documents and a document renders to HTML or PDF; and fills {{name}} placeholders in a DOCX or ODT template. It is pure computation over bytes: no office suite, no capability.

Out of scope:

  • The binary .doc (OLE compound files) and flat XML ODT (.fodt).
  • Layout fidelity. Page layout, fonts, spacing and numbering are left to the application that opens the file; the model keeps roles, not geometry.
  • More than one section. One page size, header and footer per document.
  • Writing comments and tracked changes. They are read, never written.
  • Macros, fields beyond hyperlinks, text boxes, shapes, charts, equations.

Decisions

  • One model for every format. Paragraph roles ("heading", "quote", "code", ...) instead of style names: each format maps a role to its own style on write and back by style name on read. A conversion is a read and a write, and nothing in between knows the source format.
  • Constructors, not literals. A consumer's literal of a deck struct must name every field, so every struct has a constructor with defaults.
  • The walk returns what it built. Converting a tree returns its blocks and the lists it opened, and the caller appends them, instead of threading a growing Document through the recursion, which would copy it on every call. Writers assemble each part as a list of strings joined once.
  • Fixed relationship ids. The DOCX writer names picture i rIdImg<i+1> and external link k rIdLink<k+1> in every part, so a block deep in a table writes its references without state.
  • Templates patch, they do not rewrite. Filling a template changes only the text tokens a placeholder spans, found in the concatenated text of a paragraph, so a file from Word keeps every part the model cannot hold.
  • Roles hide their style's formatting. A reader takes alignment and run properties only from the styles below the one that defines a paragraph's role, so a centred, bold Title style does not turn into direct formatting on every title.
  • Numbering in one pass. The ODT writer marks tables, frames and notes while writing and numbers the marks in one pass over the finished part, instead of threading a counter through the writer.
  • Strict reading. Malformed input is an error: an unknown XML entity, a broken container, a reference to a part that does not exist.
  • The ZIP container is core archive. ODT needs mimetype stored first; archive.Entry.stored writes it so. Every member has mtime 0, so the same document gives the same bytes.

Errors

The deck throws Error{kind: "docs"} (ERROR_KIND) for malformed files, unsupported formats, bad arguments and documents a format cannot hold. A reached cap is kind "limit". The generic kinds ("limit", "timeout", "closed", "net", "cancelled") caught from a library pass through unchanged (rethrowAs); any other fault while reading is rewrapped as "docs" naming the format. Input quoted in a message is cut to 48 characters. The deck reads no server, so there is no error struct.

Security and limits

  • A ZIP container's central directory is summed before anything is inflated: past MAX_INFLATED bytes or MAX_ENTRIES members it is refused, and core archive's own caps hold during inflation. Path traversal is refused by archive.unpack.
  • XML is never parsed with entity expansion: only the five predefined entities and character references are decoded, so external entities and expansion bombs do not apply.
  • Every count that drives an allocation is checked before the allocation, over the whole document rather than per table or per flow: blocks (MAX_BLOCKS), table cells with their repeats and spans multiplied (MAX_CELLS), a span (MAX_SPAN), inlines per paragraph (MAX_INLINES) and expanded text (MAX_TEXT). A small file that unfolds into a huge document is refused with "limit" before it is built, so no input can run the interpreter out of memory.
  • Nesting (tables in tables, lists, fields, RTF groups) is bounded by MAX_NESTING; any XML part is first checked by core xml (well-formed, no DOCTYPE, at most 400 levels and about 2 million nodes), and the readers walk it in passes linear in its size, so deep nesting is refused at once instead of after a quadratic scan. Comments, CDATA and processing instructions are handled wherever text is read.
  • RTF is tokenized into 9 bytes a token, at most 4 Mi tokens and 256 MiB of input.
  • checkDocument holds every rule a writer applies (sizes, page, spans, references, the caps, characters XML forbids). The writers call it first, so they refuse the same documents, and the readers call it on what they return, so a document read in one format can be written in every other. Where real files legitimately differ (Word's negative top and bottom margins, a date that is not ISO 8601, a note reference inside a note), the readers normalise instead of refusing.

Testing and validation

  • One overlay per topic, all run through src/docs_test.j. Reader fixtures are built member by member in memory, not with the deck's own writers.
  • Output was checked with python-docx (DOCX) and odfpy (ODT) while developing; file(1) identifies the ODT as OpenDocument Text.

Not covered: Microsoft Word and LibreOffice are not run as oracles here.

Known limitations and open questions

  • RTF reads the code pages 1250 to 1258, Mac Roman, 20127 (ASCII) and 28591 (Latin-1); any other (437, 850, the East Asian pages, ...) is refused when a byte has to be decoded in it.
  • A list item's second paragraph becomes body text: the model has no continuation paragraph.

Dependencies on the interpreter

  • Floor >=0.29.0: archive.Entry.stored for ODT's mimetype.
  • The bundled doctree and markdown modules; the archive, xml, images and encoding libraries.