Skip to content
Jennifer Programming Language

doctree API reference

A format-neutral document tree and its renderers. A Node is one element of a document - a heading, a paragraph, a list, a table, a link, a run of text - with its children; a tree of them renders to HTML, XHTML, styled terminal text (ANSI) or a paginated PDF. Readers build trees (markdown.parse, rst.parse, myst.parse), programs build them by hand, and the accessors (typeOf / children / text / level / attr / get / findAll / has) walk and inspect them - the same vocabulary as xml / html.

Rendering is safe by default: text is escaped, link and image URLs go through an allow-list (javascript: becomes #), attribute names outside a small allow-list are dropped, and raw HTML blocks are escaped unless renderHtml is asked to pass them through.

Import with import "doctree.j" as doctree;. See the doctree guide for prose and examples.

Contents

NameWhat it is
attrA named string attribute of a node: "href" / "title" (a link / image), "lang" (a code block),...
childrenA node's direct children.
findAllEvery node matching a /-separated selector relative to node: each step is a kind name, * (any kind)...
getThe first node matching selector (see findAll), or an empty node (typeOf "") if there is no match.
grayA grey fill of the given 0-255 level (0 black, 255 white).
hasWhether any node matches selector (see findAll).
headingStyleA heading style with the given background fill.
layoutPdfLay a parsed (or hand-built / transformed) markdown document tree out to a pdf.Document and report what...
levelA heading's level (1-6); 0 for any other node.
noFillNo fill (a transparent background).
pageBreakA page-break node.
pdfDefaultsThe default options: US Letter, 54-point margins, the Helvetica family for body / bold / italic / headings,...
renderRender a document tree (a parse result, or a hand-built one) to a string.
renderHtmlRender a document (or a single block) to HTML with explicit options.
renderPdfRender a parsed (or hand-built / transformed) markdown document tree to PDF bytes with the given options.
renderPdfDocLay a parsed (or hand-built / transformed) markdown document tree out to a pdf.Document, ready for any ...
rgbAn RGB fill (each component 0-255).
textThe text content of a node: a leaf's own literal text, else the concatenation of its descendants' text - so...
typeOfThe kind of a node ("document", "heading", "link", "text", ...).
Fillstruct: A fill colour (0-255 RGB) that may be off.
HeadingStylestruct: The style for a heading level - currently just a background fill painted behind the heading text (a shade...
HtmlOptionsstruct: Options for renderHtml.
MissingGlyphstruct: One character a PDF render could not draw: no font of its role has a glyph for it (for a role without an em...
Nodestruct: One element of a document tree.
PdfOptionsstruct: Page geometry and fonts for a render.
PdfResultstruct: What layoutPdf returns: the laid-out document and the characters it could not draw.

Functions

attr

jennifer
doctree.attr(node as Node, name as string)

A named string attribute of a node: "href" / "title" (a link / image), "lang" (a code block), "kind" and "title" (an admonition: the alert kind, and the custom title or ""), "align" (a table cell), "ordered" ("true" / "false", a list), or "level" (a heading, as a string). Returns "" for an absent attribute.

node
the node
name
the attribute name
Returns string
the attribute value, or ""

children

jennifer
doctree.children(node as Node)

A node's direct children.

node
the node
Returns list of Node
the child nodes

findAll

jennifer
doctree.findAll(node as Node, selector as string)

Every node matching a /-separated selector relative to node: each step is a kind name, * (any kind), or name[k] (the k-th such child, 1-based). Steps match direct children. A malformed step (name[0], name[x], an unclosed [) is a kind "doctree" error.

node
the node to search under
selector
the selector path (e.g. "list/item")
Returns list of Node
the matching nodes (empty if none)

get

jennifer
doctree.get(node as Node, selector as string)

The first node matching selector (see findAll), or an empty node (typeOf "") if there is no match.

node
the node to search under
selector
the selector path
Returns Node
the first match, or an empty node

gray

jennifer
doctree.gray(level as int)

A grey fill of the given 0-255 level (0 black, 255 white).

level
the grey level, 0-255
Returns Fill
the fill

has

jennifer
doctree.has(node as Node, selector as string)

Whether any node matches selector (see findAll).

node
the node to search under
selector
the selector path
Returns bool
true if at least one node matches

headingStyle

jennifer
doctree.headingStyle(background as Fill)

A heading style with the given background fill.

background
the background fill (off for none)
Returns HeadingStyle
the style

layoutPdf

jennifer
doctree.layoutPdf(doc as Node, opts as PdfOptions)

Lay a parsed (or hand-built / transformed) markdown document tree out to a pdf.Document and report what could not be drawn. doc is the document, ready for any document-level work (a running header or footer with pdf.setHeader / pdf.setFooter, extra pdf.info metadata) before pdf.render; missing lists every character no font of its role has (drawn as unencodable instead), with its page and baseline, so a caller can warn or refuse the document. renderPdfDoc and renderPdf lay out the same way and drop the list.

Every crossing into pdf that carries the document happens once per render: the bookmarks, the embedded faces a page drew from, the images and the pages are each handed over as one list at the end.

doc
the document root node from parse
opts
the page geometry and fonts
Returns PdfResult
the laid-out document and the characters it could not draw
Throws Error
kind "doctree" for two images or two different faces under one resource name, a face without a name or named like a standard-14 font
Throws Error
kind "limit" for nesting or a table too wide for the page

level

jennifer
doctree.level(node as Node)

A heading's level (1-6); 0 for any other node.

node
the node
Returns int
the heading level

noFill

jennifer
doctree.noFill()

No fill (a transparent background).

Returns Fill
an off fill

pageBreak

jennifer
doctree.pageBreak()

A page-break node. Placed in a document tree (typically between the blocks of a hand-assembled book), it makes renderPdf start the following content on a fresh page - a lever independent of the level-one-heading page break. The HTML and ANSI renderers ignore it. In Markdown source, a lone <!-- pagebreak --> comment parses to the same node.

Returns Node
a page_break node

pdfDefaults

jennifer
doctree.pdfDefaults()

The default options: US Letter, 54-point margins, the Helvetica family for body / bold / italic / headings, Courier for code, 11-point body. Copy and tweak fields (value semantics) to customise.

images is empty by default, so ![alt](url) renders as its [alt] text (as before). To draw pictures, load each with pdf.loadImage(name, bytes) and put it in images keyed by the image URL exactly as it appears in the Markdown source (markdown never touches the filesystem - the caller supplies the bytes); each image name must be unique. imageDpi (default 96) converts a picture's pixel size to points; a picture wider than the text column shrinks to it, keeping aspect ratio. Only a paragraph that is a single image is drawn; an image inline in a sentence keeps its [alt] text.

No role has an embedded face: set bodyFace and the others (and their fallback lists) to draw text the standard-14 fonts cannot.

Returns PdfOptions
the default options

render

jennifer
doctree.render(format as string, doc as Node)

Render a document tree (a parse result, or a hand-built one) to a string. format is "html" (block elements concatenated, no indentation), "xhtml" (the same, but well-formed XML for an EPUB / XHTML content document: void elements self-close and boolean attributes expand), or "ansi" (styled terminal text, blocks separated by a blank line; control characters in the source text other than tab and newline become U+FFFD). A "document" node renders its children; any other node renders as a single block. An unknown format is a catchable "doctree" error.

format
"html", "xhtml", or "ansi"
doc
the document (or block) node to render
Returns string
the rendered document

renderHtml

jennifer
doctree.renderHtml(doc as Node, opts as HtmlOptions)

Render a document (or a single block) to HTML with explicit options. render with "html" / "xhtml" is this with the default options.

doc
the document (or block) node to render
opts
the render options
Returns string
the rendered HTML

renderPdf

jennifer
doctree.renderPdf(doc as Node, opts as PdfOptions)

Render a parsed (or hand-built / transformed) markdown document tree to PDF bytes with the given options. A one-liner over renderPdfDoc + pdf.render; call those two directly when you need to touch the pdf.Document in between.

doc
the document root node from parse
opts
the page geometry and fonts
Returns bytes
the PDF document

renderPdfDoc

jennifer
doctree.renderPdfDoc(doc as Node, opts as PdfOptions)

Lay a parsed (or hand-built / transformed) markdown document tree out to a pdf.Document, ready for any document-level work - a running header or footer (pdf.setHeader / pdf.setFooter, with %page% / %pages%), extra pdf.info metadata, more bookmarks - before it is serialised with pdf.render. This is the seam renderPdf renders through; use it directly when you need the document itself (a page number in a book's footer needs the total page count, which only exists once the whole document is laid out). layoutPdf is the same with the list of characters that could not be drawn.

doc
the document root node from parse
opts
the page geometry and fonts
Returns pdf.Document
the laid-out document, not yet serialised

rgb

jennifer
doctree.rgb(r as int, g as int, b as int)

An RGB fill (each component 0-255).

r
red 0-255
g
green 0-255
b
blue 0-255
Returns Fill
the fill

text

jennifer
doctree.text(node as Node)

The text content of a node: a leaf's own literal text, else the concatenation of its descendants' text - so text of a heading (or any container) is its full flattened text, handy for a table of contents.

node
the node
Returns string
the flattened text

typeOf

jennifer
doctree.typeOf(node as Node)

The kind of a node ("document", "heading", "link", "text", ...).

node
the node
Returns string
the node kind

Structs

Fill

A fill colour (0-255 RGB) that may be off. on: false means "no fill" (draw nothing); an on: true fill paints a background behind a heading or a table header row. Build one with gray / rgb, or noFill for none.

jennifer
def struct Fill {
    on as bool,
    r as int,
    g as int,
    b as int
};
on
whether the fill is applied at all
r
red 0-255
g
green 0-255
b
blue 0-255

HeadingStyle

The style for a heading level - currently just a background fill painted behind the heading text (a shaded bar). A struct so more style knobs can be added later.

jennifer
def struct HeadingStyle {
    background as Fill
};
background
the background fill (off for none)

HtmlOptions

Options for renderHtml.

jennifer
def struct HtmlOptions {
    allowRawHtml as bool,
    xhtml as bool
};
allowRawHtml
pass html_block nodes through verbatim (trusted input only). Default (zero value) is false: raw HTML is escaped.
xhtml
write well-formed XHTML (void elements self-close, boolean attributes expand) instead of HTML

MissingGlyph

One character a PDF render could not draw: no font of its role has a glyph for it (for a role without an embedded face: WinAnsi cannot encode it), so the page shows the unencodable substitute in its place. layoutPdf lists every such character in the order the layout met it.

jennifer
def struct MissingGlyph {
    char as string,
    codepoint as int,
    role as string,
    pageNr as int,
    y as int,
    context as string
};
char
the character
codepoint
its Unicode code point
role
the font role it was set in: "body", "bold", "italic", "mono" or "heading"
pageNr
the 1-based number of the page it falls on
y
the baseline of its line (a table row's first line), in PDF points from the bottom of the page
context
the word, line, heading or table cell it appears in, as written in the source

Node

One element of a document tree. Its kind is a block - "document", "heading", "paragraph", "code", "list" (of "item"s), "definition_list" (of "def_term" / "def_desc"), "table" (of "row"s of "cell"s), "quote", "admonition", "thematic_break", "html_block", "page_break", "line_block" (of "line"s), "footnote" / "citation", "figure" (an image, "caption", "legend"), "topic", "rubric", "container" - or an inline - "text", "codespan", "strong", "emphasis", "strikethrough", "highlight", "subscript", "superscript", "link", "image", "line_break", "html_inline", "footnote_ref" / "citation_ref", "span", "cite", "abbr". An item, def_desc, cell, footnote or admonition may hold blocks as well as inline nodes.

jennifer
def struct Node {
    kind as string,
    level as int,
    text as string,
    lang as string,
    ordered as bool,
    url as string,
    title as string,
    align as string,
    attrs as map of string to string,
    children as list of Node
};
kind
the node kind
level
a heading's level (1-6); 0 otherwise
text
literal text (a text / codespan / code node, an image's alt, a footnote_ref / citation_ref's label); "" for a container (read its content with the text accessor)
lang
a fenced code block's language ("" if none)
ordered
whether a list is ordered
url
a link / image target, or a note reference's #id
title
a link / image title, a topic's title, or an abbr's expansion ("" if none)
align
a table cell's alignment ("left" / "right" / "center" / "")
attrs
extra named attributes: a heading / link / image / code's {#id .class key="value"} attribute list (id / class / any key, on any node in a hand-built tree), a fenced block's info string, a task item's task / checked, an ordered list's start and enumtype (1 / a / A / i / I), a table's headerRows, a cell's colspan / rowspan, a footnote's label and backref. Read with attr(node, name).
children
the child nodes

PdfOptions

Page geometry and fonts for a render. margin is applied to all four sides; bodyFont / boldFont / italicFont / monoFont are standard-14 font names for body text and its inline styles, headingFont for headings. bodySize is the body point size; heading sizes derive from the heading level.

Each of the five font roles may instead draw in an embedded TrueType face (bodyFace ... headingFace, from pdf.loadFont), with a list of fallback faces for the characters the first one lacks (bodyFallbacks ...). A role with a face draws and measures in it; a role with only fallbacks keeps its standard-14 font for what WinAnsi covers and draws the rest in the fallbacks; a role with neither draws exactly as before. A character no font of its role has is drawn as unencodable and listed by layoutPdf.

jennifer
def struct PdfOptions {
    pageWidth as int,
    pageHeight as int,
    margin as int,
    bodyFont as string,
    boldFont as string,
    italicFont as string,
    monoFont as string,
    headingFont as string,
    bodySize as int,
    tablePad as int,
    tableHeaderFill as Fill,
    headingStyles as list of HeadingStyle,
    title as string,
    author as string,
    subject as string,
    keywords as string,
    bookmarkLevel as int,
    unencodable as string,
    codeFill as Fill,
    codeBorder as Fill,
    quoteFill as Fill,
    quoteRule as Fill,
    creator as string,
    producer as string,
    admonitionLabels as map of string to string,
    images as map of string to pdf.Image,
    imageDpi as int,
    bodyFace as pdf.LoadedFont,
    boldFace as pdf.LoadedFont,
    italicFace as pdf.LoadedFont,
    monoFace as pdf.LoadedFont,
    headingFace as pdf.LoadedFont,
    bodyFallbacks as list of pdf.LoadedFont,
    boldFallbacks as list of pdf.LoadedFont,
    italicFallbacks as list of pdf.LoadedFont,
    monoFallbacks as list of pdf.LoadedFont,
    headingFallbacks as list of pdf.LoadedFont
};
pageWidth
page width in points (Letter 612, A4 595)
pageHeight
page height in points (Letter 792, A4 842)
margin
margin on every side, in points
bodyFont
standard-14 font for body text
boldFont
standard-14 font for **strong**
italicFont
standard-14 font for *emphasis*
monoFont
standard-14 font for inline / block code
headingFont
standard-14 font for headings
bodySize
body text point size
tablePad
padding inside a table cell, in points (table density)
tableHeaderFill
background fill behind a table's header row (off for none)
headingStyles
per-level heading style; index 0 is h1, 1 is h2, and so on (missing / off entries render with no background)
title
PDF document Title metadata ("" = unset)
author
PDF document Author metadata ("" = unset)
subject
PDF document Subject metadata ("" = unset)
keywords
PDF document Keywords metadata ("" = unset)
bookmarkLevel
bookmark headings up to this level (0 = none; 2 = h1 + h2)
unencodable
substitute for a character no font of its role can draw: outside WinAnsi for a standard-14 font, missing from every embedded face ("?" default; "" drops it); layoutPdf lists each one
codeFill
background fill behind a code block (off for none)
codeBorder
border stroke around a code block (off for none)
quoteFill
background fill behind a blockquote (off for none)
quoteRule
colour of the vertical bar down a blockquote's left edge (off for none)
creator
PDF document Creator metadata ("" = unset)
producer
PDF document Producer metadata ("" = keep the pdf default)
admonitionLabels
what to call each admonition kind - {"note": "Hinweis", ...} - for a book laid out in a language other than English. A kind that is missing is called by its own name; a marker that carried a title is called by that, since the title is the author's own words rather than the renderer's.
images
pictures to draw, keyed by the image URL exactly as written in the source (empty = every ![alt](url) stays [alt] text); load each with pdf.loadImage, each with a unique resource name
imageDpi
pixels-to-points scale for a drawn image (default 96)
bodyFace
embedded face for body text (no name = use bodyFont)
boldFace
embedded face for **strong** (no name = use boldFont)
italicFace
embedded face for *emphasis* (no name = use italicFont)
monoFace
embedded face for code (no name = use monoFont)
headingFace
embedded face for headings (no name = use headingFont)
bodyFallbacks
faces tried in order for a body character the body font lacks
boldFallbacks
the same for bold text
italicFallbacks
the same for italic text
monoFallbacks
the same for code
headingFallbacks
the same for headings

PdfResult

What layoutPdf returns: the laid-out document and the characters it could not draw.

jennifer
def struct PdfResult {
    doc as pdf.Document,
    missing as list of MissingGlyph
};
doc
the laid-out document, not yet serialised
missing
every character drawn as the unencodable substitute, in layout order (empty when nothing was lost)