Skip to content
Jennifer Programming Language

rst API reference

A reStructuredText (rST) reader. parse turns rST source into a doctree document tree (doctree.Node), the same tree markdown.parse builds, so one set of renderers serves both formats: toHtml / toXhtml / toAnsi here, and doctree.render / doctree.renderPdf / doctree.findAll on the tree.

Covered: section titles (levels from the order the underline / overline styles first appear), transitions, paragraphs, :: literal blocks (indented and quoted), doctest blocks, block quotes with attributions, line blocks, bullet lists, enumerated lists (arabic, alphabetic and roman, 1. / 1) / (1), auto-numbered #.), definition lists with classifiers, field lists, option lists, grid and simple tables (with row and column spans) and comments. A lone top-level title is promoted to the document title (and a lone subtitle under it), and a field list at the top becomes the bibliographic docinfo. Inline: *emphasis*, **strong**, ``literal``, interpreted text with roles (emphasis, strong, literal, code, math, sub, sup, title-reference - the default -, abbreviation, pep, rfc), inline targets and backslash escapes, with rST's rules for where markup may start and end.

Directives: the admonitions (note, warning, ... and admonition), code / code-block / sourcecode, math, image, figure, topic, sidebar, rubric, epigraph / highlights / pull-quote, container, compound, class, raw (HTML, escaped unless allowed), contents, list-table and csv-table (inline data), each with :class: and :name:. include and every other way of reading a file are refused.

References: named (name_, `a phrase`_), anonymous (name__), with an embedded URI (`text <https://...>`_) or alias (`text <name_>`_), to external, internal (.. _name:) and indirect targets and to section titles; standalone URLs and email addresses; footnotes ([1]_, [#]_, [#label]_, [*]_) and citations ([CIT2002]_), with links back; and substitutions (|name|, |name|_) defined with replace, unicode or image. A reference to an unknown target stays as its source text and is reported by check.

Parsing is lenient: malformed markup is kept as text and never throws. check reports what was wrong as Diagnostics; Options{strict: true} turns the first warning or error into a catchable "rst" error. A document that nests too deep or holds too many nodes is a kind "limit" error. There is no file I/O: nothing in a document can read from the host.

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

Contents

NameWhat it is
checkThe problems in a document, in source order: markup that was read some other way than it looks ("warning"...
docinfoThe bibliographic fields of a document (the field list at its top, such as :Author: or :Version:), keye...
parseParse reStructuredText into a document tree: a doctree.Node whose typeOf is "document", walked with...
parseWithParse reStructuredText with explicit options.
titleThe document title: the text of the promoted title heading, or "" when the document has none.
toAnsiRender reStructuredText to styled terminal text.
toHtmlRender reStructuredText to HTML.
toHtmlWithRender reStructuredText to HTML with explicit options.
toXhtmlRender reStructuredText to well-formed XHTML (void elements self-close).
Diagnosticstruct: One problem found in a document.
Optionsstruct: Options for parseWith and toHtmlWith.

Functions

check

jennifer
rst.check(src as string)

The problems in a document, in source order: markup that was read some other way than it looks ("warning") or could not be read as written ("error"). An empty list means the document is clean.

src
the rST source
Returns list of Diagnostic
the problems found

docinfo

jennifer
rst.docinfo(doc as doctree.Node)

The bibliographic fields of a document (the field list at its top, such as :Author: or :Version:), keyed by lowercased field name, each value the field's flattened text. Empty when the document has no docinfo.

doc
a document from parse
Returns map of string to string
field name to text

parse

jennifer
rst.parse(src as string)

Parse reStructuredText into a document tree: a doctree.Node whose typeOf is "document", walked with doctree.findAll / doctree.text and friends and rendered with doctree.render / doctree.renderPdf. Problems in the markup are recovered from (see check); a document that nests too deep or is too large is a catchable kind "limit" error.

src
the rST source
Returns doctree.Node
the document root node

parseWith

jennifer
rst.parseWith(src as string, opts as Options)

Parse reStructuredText with explicit options.

src
the rST source
opts
the parse options
Returns doctree.Node
the document root node
Throws Error
kind "rst" on the first problem when opts.strict is set

title

jennifer
rst.title(doc as doctree.Node)

The document title: the text of the promoted title heading, or "" when the document has none.

doc
a document from parse
Returns string
the title text

toAnsi

jennifer
rst.toAnsi(src as string)

Render reStructuredText to styled terminal text.

src
the rST source
Returns string
the rendered text, with ANSI styling

toHtml

jennifer
rst.toHtml(src as string)

Render reStructuredText to HTML. Safe by default: everything is escaped and link targets go through doctree's URL filter.

src
the rST source
Returns string
the rendered HTML

toHtmlWith

jennifer
rst.toHtmlWith(src as string, opts as Options)

Render reStructuredText to HTML with explicit options.

src
the rST source
opts
the parse and render options
Returns string
the rendered HTML
Throws Error
kind "rst" on the first problem when opts.strict is set

toXhtml

jennifer
rst.toXhtml(src as string)

Render reStructuredText to well-formed XHTML (void elements self-close).

src
the rST source
Returns string
the rendered XHTML

Structs

Diagnostic

One problem found in a document.

jennifer
def struct Diagnostic {
    severity as string,
    line as int,
    message as string
};
severity
"warning" (recovered from, likely unintended) or "error" (markup that could not be read as written)
line
the 1-based source line it was found on
message
what went wrong

Options

Options for parseWith and toHtmlWith. The zero value is the default.

jennifer
def struct Options {
    strict as bool,
    allowRawHtml as bool,
    initialHeadingLevel as int,
    noTitlePromotion as bool
};
strict
throw the first warning or error as an "rst" error instead of recovering from it
allowRawHtml
pass raw HTML through when rendering (trusted input only); escaped by default
initialHeadingLevel
the HTML level of the top heading (1-6; 0 means 1). Deeper sections follow from it, capped at 6
noTitlePromotion
keep a lone top-level title as an ordinary section heading instead of promoting it to the document title