Skip to content
@deckyard/docs

Cheatsheet

Every exported name, written bare. In your code they carry the alias you import the deck under:

jennifer
import "@deckyard/docs/" as docs;

Functions that change a document return a new one: $d = addList($d, ...). A struct is changed in place through its fields: $d.blocks[] = ....

Errors and limits

ConstantValueMeaning
ERROR_KIND"docs"The kind of every error this deck raises, except a reached cap ("limit")
MAX_NESTING100Nesting depth (tables, lists, RTF groups) past which a reader stops
MAX_INFLATED268435456Bytes a DOCX or ODT container may declare in total
MAX_ENTRIES4096Members a DOCX or ODT container may hold
MAX_BLOCKS200000Blocks one document may hold: body, cells, notes, comments, header and footer together
MAX_SPAN1000Columns or rows one table cell may span
MAX_CELLS200000Table cells one document may hold, all tables together, a spanned cell counted as the slots it covers; a reader counts repeated and spanned rows before building them
MAX_INLINES100000Inlines one paragraph may hold
MAX_TEXT67108864Characters of text one document may hold, counted as a reader expands it (repeated spaces, tabs, field instructions included)

Every reader also caps an XML part at 400 levels of nesting and about 2 million nodes (core xml), and readRtf at 4 Mi tokens and 256 MiB of input.

Any format

FunctionReturnsDoes
formatOf(b)string"docx", "odt" or "rtf", from the content
read(b)DocumentRead any format, detected from the content
write(d, format)bytesWrite "docx", "odt" or "rtf"
open(file)DocumentRead a file; the format comes from the content
save(d, file)-Write a file in the format its extension names

DOCX

FunctionReturnsDoes
readDocx(b)DocumentRead a DOCX: roles from style names, lists, tables with spans, links, pictures, notes, comments, tracked changes, header and footer, page, properties
writeDocx(d)bytesThe document as a .docx

ODT

FunctionReturnsDoes
readOdt(b)DocumentRead an .odt package (a flat .fodt is refused)
writeOdt(d)bytesThe document as an ODF 1.3 .odt; comments and deleted tracked text are not written

RTF

FunctionReturnsDoes
readRtf(b)DocumentRead an RTF file: roles from the stylesheet, lists, tables with spans and nesting, links, notes, comments, PNG and JPEG pictures; code pages 1250 to 1258 and Mac Roman
writeRtf(d)bytesThe document as 7-bit RTF 1.9.1; the same document gives the same bytes

Doctree and Markdown

FunctionReturnsDoes
fromDoctree(tree)DocumentA doctree (Markdown, reST, MyST) as a document; no images
fromDoctreeWith(tree, images)DocumentThe same, with picture data by image URL
fromMarkdown(md)DocumentMarkdown through the bundled markdown module; no images
fromMarkdownWith(md, images)DocumentThe same, with picture data by image URL
toDoctree(d)doctree.NodeThe document as a doctree: grouped lists, quotes, code and definitions, footnotes at the end
toHtml(d)stringHTML through doctree.renderHtml, pictures as data: URIs
toPdf(d)bytesPDF through doctree.renderPdf: page size, properties, stand-alone pictures; throws only "docs" and "limit"

Templates

FunctionReturnsDoes
fillDocx(b, values)bytesFill the {{name}} placeholders of a DOCX
fillDocxWith(b, values, rows)bytesThe same, repeating table rows that hold {{key.field}}
fillOdt(b, values)bytesFill the placeholders of an ODT
fillOdtWith(b, values, rows)bytesThe same, repeating table rows
placeholders(b)list of stringThe placeholder names a DOCX or ODT holds, in order of first use

Building a document

FunctionReturnsDoes
newDocument()DocumentAn empty document on an A4 page
paragraph(inlines)BlockA body-text paragraph
styledParagraph(style, inlines)BlockA paragraph with a role ("title", "quote", "code", ...)
heading(level, inlines)BlockA heading, level 1 to 6
listItem(listId, level, inlines)BlockAn item of list listId (1-based) at level 0 to 8
pageBreak()BlockA page break
table(rows, headerRows)BlockA table; headerRows is 0 to the number of rows
textTable(cells, headerRows)BlockA table of plain-text cells
cell(blocks)CellA cell spanning one row and one column
text(s)InlineUnformatted text
styled(s, style)InlineFormatted text
bold(s)InlineBold text
italic(s)InlineItalic text
link(s, target)InlineHyperlinked text; #name for a bookmark
lineBreak()InlineA line break inside a paragraph
tab()InlineA tab
image(media, alt)InlineA picture added with addMedia
noteRef(note)InlineA footnote reference, by index into notes
plain()RunStyleFormatting with nothing set
bullets()ListDefA bulleted list
numbering(format, start)ListDefA numbered list counting from start (1 or more)
addList(d, numbered)DocumentAdd a list; its id is the new length of lists
addNote(d, blocks)DocumentAdd a footnote; its index is the old length of notes
addMedia(d, name, data, width, height)DocumentAdd a PNG, JPEG or GIF; 0 for a size takes the picture's own
emptyMeta()MetaProperties all ""
a4()PageA4, 2.5 cm margins
letter()PageUS Letter, 1 inch margins
plainText(d)stringThe document's text, a line per paragraph or table row
checkDocument(d)-Throw "docs" (or "limit" past a cap) for anything a writer cannot hold; every reader and writer calls it, so a document that passes converts to any format

The model

Document

FieldMeaning
blocks as list of BlockThe body
lists as list of ListDefList definitions, referred to 1-based by Block.listId
notes as list of NoteFootnotes, referred to by Inline.note
media as list of MediaPictures, referred to by Inline.media
header as list of BlockThe page header
footer as list of BlockThe page footer
comments as list of CommentComments, as read
meta as MetaDocument properties
page as PagePage size and margins

Block

FieldMeaning
kind as string"paragraph", "table" or "pagebreak"
style as string"", "heading", "title", "subtitle", "quote", "code", "caption", "term", "definition", "rule"
level as intA heading's level, 1 to 6
listId as intThe list, 1-based; 0 for none
listLevel as intThe level in the list, 0 to 8
align as string"", "center", "right", "justify"
inlines as list of InlineA paragraph's content
rows as list of RowA table's rows
headerRows as intA table's header rows

Inline

FieldMeaning
kind as string"text", "tab", "break", "image", "note", "comment"
text as stringThe text; an image's alt text
style as RunStyleCharacter formatting
link as stringHyperlink target, "" for none
media as stringAn image's Media.name
note as intA note's or comment's index
change as stringTracked change as read: "", "insert", "delete"

RunStyle

FieldMeaning
bold as boolBold
italic as boolItalic
underline as boolUnderline
strike as boolStrikethrough
code as boolMonospace
superscript as boolRaised
subscript as boolLowered
highlight as boolHighlighted
color as stringSix hex digits in either case, "" for automatic; readers return and writers write upper case
size as intHalf-points, 0 to inherit
font as stringFont family, "" to inherit

Row

FieldMeaning
cells as list of CellIts cells; one covered by a span is left out

Cell

FieldMeaning
blocks as list of BlockIts content
colspan as intColumns it spans
rowspan as intRows it spans

ListDef

FieldMeaning
ordered as boolNumbered rather than bulleted
start as intThe first number
format as string"bullet", "decimal", "lowerLetter", "upperLetter", "lowerRoman", "upperRoman"

Note

FieldMeaning
blocks as list of BlockThe note's content

Media

FieldMeaning
name as stringUnique name with the format's extension
contentType as string"image/png", "image/jpeg", "image/gif"
data as bytesThe encoded picture
width as intDisplay width in pixels at 96 dpi
height as intDisplay height in pixels at 96 dpi

Comment

FieldMeaning
author as stringWho wrote it
date as stringWhen, ISO 8601
text as stringIts text

Meta

FieldMeaning
title as stringTitle
subject as stringSubject
author as stringCreator
keywords as stringKeywords
description as stringDescription
language as stringBCP 47 tag
created as stringCreation time, ISO 8601
modified as stringModification time, ISO 8601

Page

FieldMeaning
width as intPage width in twips
height as intPage height in twips
marginTop as intTop margin
marginBottom as intBottom margin
marginLeft as intLeft margin
marginRight as intRight margin