pypi-rstobj
API reference for rstobj 2.0.1, a Python library that builds reStructuredText markup and directives from dataclasses and renders them to text: Header, BulletList, EnumList, URI, Reference, ListTable,
Files2
View on GitHubSKILL.md.claude/skills/pypi-rstobj/SKILL.md
GitHubrstobj 2.0.1
rstobj builds reStructuredText from Python objects: create a dataclass such as ListTable(data=...), call .render(), and get the reST text. Typical use is generating Sphinx pages or .rst files from data, often inside a jinja2 template ({{ table.render() }}) or with sphinx-jinja.
Import
import rstobj.api as rstobj # then rstobj.markup.Header, rstobj.directives.ListTable
from rstobj.api import markup, directives
Rules that apply to every class
- All fields are keyword-only:
Header(title="Intro"), neverHeader("Intro"). obj.render(indent=None, first_line_indent=None) -> str.indent=nindents every line by n tabs (4 spaces);first_line_indent=msets the first line's indent separately. Blank lines stay empty.- Strings are raw reST, never escaped.
"*x*"renders as emphasis; a backtick in a link title breaks the link. Escape yourself if needed. - A list item or table cell may be a string, a number, or another rstobj object (it is rendered and indented to fit). One item holds one thing; for "text with a nested list under it", put both in one string:
"Title\n\n" + rstobj.markup.BulletList(items=[...]).render().strip("\n"). - In a string, a single
\ninside a paragraph is a space in reST; use\n\nfor a new paragraph. - Output framing differs by class:
Header*,BulletList,EnumListstart with"\n";Image,ListTable,TableOfContent,Includeend with"\n". Join pieces with"\n\n".join(...)and strip if you need exact spacing.
Public API
rstobj.markup
Header(title: str, header_level: int = 1, ref_label: str | None = None, auto_label: bool = False, bar_length: int | None = None): section title with an underline (=,-,~,+,*,#,^for levels 1-7).auto_label=Trueadds a.. _<slug>:label from the title. The underline is at least the title's display width (CJK counts double).ValueErrorifheader_levelis not 1-7.Header1...Header7(title, ref_label=None, auto_label=False, bar_length=None):Headerwith a fixed level.BulletList(items: list):- itemlines.EnumList(items: list, start_num: int = 1):1. itemlines, numbered fromstart_num.URI(title: str, link: str), aliasURL:`title <link>`_.Reference(title: str, label: str), aliasRef: Sphinx:ref:`title <label>`.
rstobj.directives
Directives also take class_: str | None and name: str | None (:class:, :name:).
ListTable(data: list[list], title: str = "", index: bool = False, header: bool = True, widths: list[int] | None = None, align: str | None = None):.. list-table::.headermakes row 1 the header;indexmakes column 1 a stub column;alignis"left","center", or"right"(ListTable.AlignOptions).Image(uri: str, height: int | None = None, width: int | None = None, scale: int | None = None, alt_text: str | None = None, align: str | None = None):.. image::.height/widthin px,scalein %.align:Image.AlignOptions(left/center/rightfor a standalone image).Code(text: str): plain code text, the content of a code block.CodeBlock(code: Code):.. code-block::without a language.CodeBlock.from_string(text) -> CodeBlock.CodeBlockPython,CodeBlockBash,CodeBlockSQL,CodeBlockJavaScript,CodeBlockHTML,CodeBlockCSS,CodeBlockC,CodeBlockCPP,CodeBlockRuby,CodeBlockR,CodeBlockPerl,CodeBlockScala,CodeBlockMake,CodeBlockLua,CodeBlockRst,CodeBlockMarkdown: same fields andfrom_string, with that language.CodeBlockBase: base class of the above;CodeBlockBase.LangOptionslists the language names.CodeBlockEmpty(code: Code): a::literal block;from_string(text). It has no options, soclass_/nameare ignored.TableOfContent(title: str | None = None, depth: int | None = None, local: bool = False, backlinks: str | None = None):.. contents::.backlinks:"entry","top","none"(TableOfContent.BacklinksOptions).nameis ignored (contentshas no:name:).Include(path: str, start_line=None, end_line=None, start_after=None, end_before=None, literal: bool | None = None, code: str | None = None, number_lines=None, encoding=None, tab_width=None):.. include::with docutils' options (:start-line:...).
Example
import rstobj.api as rstobj
header = rstobj.markup.Header(title="Users", header_level=2, auto_label=True)
table = rstobj.directives.ListTable(
data=[["id", "name"], [1, "Alice"], [2, "Bob"]],
title="Users",
)
rst = header.render().strip("\n") + "\n\n" + table.render()
More detail
For every field, the rendered output of each class, and more examples, see ref/public-api.md.