Doctree and Markdown
The bundled doctree is the tree markdown, rst and myst parse into and the renderers draw from. fromDoctree turns one into a document; toDoctree turns a document back, so a document renders to HTML and PDF.
jennifer
import "@deckyard/docs/" as docs;
use fs;
def d as docs.Document init docs.fromMarkdownWith(fs.readString("README.md"), {"logo.png": fs.readBytes("logo.png")});
docs.save($d, "README.docx");Mapping
| doctree | document |
|---|---|
heading | heading paragraph, level 1 to 6 |
paragraph, line | paragraph |
code, html_block | one "code" paragraph, lines as line ends in the text |
list / item | a ListDef per list; items as list paragraphs, a nested list one level deeper; an item's later paragraphs as body text; task items with a ballot box |
table / row / cell | table with colspan, rowspan, alignment and headerRows; a table title as a "caption" paragraph before it |
quote, admonition, topic | "quote" paragraphs, a title first in bold |
definition_list | "term" and "definition" paragraphs |
thematic_break | "rule" paragraph |
page_break | page break |
footnote / footnote_ref | a note and its reference |
figure | the picture and a "caption" paragraph |
strong, emphasis, strikethrough, highlight, subscript, superscript, codespan | the matching RunStyle flags, nested |
link, image, line_break | link target, picture, line break |
html_inline, span, abbr, cite | their text |
Pictures need their data: fromDoctreeWith and fromMarkdownWith take it by the URL the tree writes. An image whose URL the map lacks is an error, not a silent omission.
Back to a doctree
toDoctree maps the other way; toHtml and toPdf render through it.
| document | doctree |
|---|---|
| list paragraphs | nested list / item by listLevel, with start and enumtype |
consecutive "quote" paragraphs | one quote |
consecutive "code" paragraphs | one code, lines joined by line ends |
"term" / "definition" | definition_list |
"rule" | thematic_break |
"title" | heading 1 with class title |
"subtitle" | rubric |
"caption" | paragraph with class caption |
| notes | footnote nodes at the end, footnote_ref where they are referred to |
| pictures | image with the media name as URL; toHtml embeds them as data: URIs, toPdf loads them |
Deleted tracked text, comments and underline (which doctree has no kind for) are left out.
jennifer
import "@deckyard/docs/" as docs;
use fs;
def d as docs.Document init docs.open("report.docx");
fs.writeString("report.html", docs.toHtml($d));
fs.writeBytes("report.pdf", docs.toPdf($d));