Examples¶
Live demos¶
This page intentionally uses directive and role autodoc to document the documentation helpers themselves. If that feels a little recursive, that is the point: roles and directives should be documentable the same way fixtures are.
Document one demo directive¶
-
.. demobadge::.. demobadge::
Render a short badge-like paragraph for directive demos.
Options:
- Python path:
- Required arguments:
1- Optional arguments:
0- Final argument whitespace:
True- Has content:
False
Document one demo role¶
-
:demo-badge::demo-badge:
Return a literal node with badge-style classes.
- Python path:
- Accepts role content:
True
Bulk directives demo¶
Renders all directive classes in a module at once:
-
.. demobadge::.. demobadge::
Render a short badge-like paragraph for directive demos.
Options:
- Python path:
- Required arguments:
1- Optional arguments:
0- Final argument whitespace:
True- Has content:
False
-
.. democallout::.. democallout::
Render a simple titled container for directive demos.
Options:
- Python path:
- Required arguments:
0- Optional arguments:
0- Final argument whitespace:
False- Has content:
True
Bulk roles demo¶
Renders all role callables in a module at once:
-
:demo-badge::demo-badge:
Return a literal node with badge-style classes.
- Python path:
- Accepts role content:
True
Document one demo transform¶
The single form imports the class directly and surfaces its
default_priority
and registration phase:
Bulk transforms demo¶
Renders every transform a module registers via docutils_demo_components.setup()
— here the demo module’s add_transform()
call:
-
docutils_demo_components.DemoReorderTransformdocutils_demo_components.DemoReorderTransform
Move demo-badge paragraphs ahead of their sibling paragraphs.
- Python path:
- Default priority:
760- Registered via:
Document one demo reader¶
Readers have no Sphinx registration call, so the single form imports the class and surfaces its formats, config section, and transform set:
-
docutils_demo_components.DemoArticleReader¶docutils_demo_components.DemoArticleReader¶
Read standalone article sources with the demo transform applied.
- Python path:
- Supported formats:
demo-article- Config section:
demo article reader- Transforms:
Decorations,ExposeInternals,StripComments,Substitutions,PropagateTargets,DocTitle,SectionSubTitle,DocInfo,AnonymousHyperlinks,IndirectHyperlinks,Footnotes,ExternalTargets,InternalTargets,DanglingReferences,Transitions,DemoReorderTransform
Bulk readers demo¶
Renders every reader class a module defines:
-
docutils_demo_components.DemoArticleReaderdocutils_demo_components.DemoArticleReader
Read standalone article sources with the demo transform applied.
- Python path:
- Supported formats:
demo-article- Config section:
demo article reader- Transforms:
Decorations,ExposeInternals,StripComments,Substitutions,PropagateTargets,DocTitle,SectionSubTitle,DocInfo,AnonymousHyperlinks,IndirectHyperlinks,Footnotes,ExternalTargets,InternalTargets,DanglingReferences,Transitions,DemoReorderTransform
Document one demo parser¶
Parsers surface their alias tuple and, when the module’s
docutils_demo_components.setup() calls
add_source_parser(), the Sphinx
registration:
Bulk parsers demo¶
-
docutils_demo_components.DemoLineParserdocutils_demo_components.DemoLineParser
Parse line-oriented demo sources into one paragraph per line.
- Python path:
- Supported aliases:
demo-lines,demolines- Config section:
demo line parser- Registered via:
Document one demo writer¶
Writers surface their output formats and translator class — resolved
defensively, since writers commonly assign translator_class inside
__init__:
-
docutils_demo_components.DemoPlainWriter¶docutils_demo_components.DemoPlainWriter¶
Write documents as plain text lines, one paragraph per line.
- Python path:
- Supported formats:
demo-plain- Translator class:
- Config section:
demo plain writer- Transforms:
Messages,FilterMessages,StripClassesAndElements
Bulk writers demo¶
-
docutils_demo_components.DemoPlainWriterdocutils_demo_components.DemoPlainWriter
Write documents as plain text lines, one paragraph per line.
- Python path:
- Supported formats:
demo-plain- Translator class:
- Config section:
demo plain writer- Transforms:
Messages,FilterMessages,StripClassesAndElements
Document one demo node¶
Custom node classes surface their base classes, docutils element
categories, and the builders their visit/depart handlers were
registered for via add_node():
Bulk nodes demo¶
-
docutils_demo_components.demo_markerdocutils_demo_components.demo_marker
Inline marker node rendered as a highlighted
<mark>span.- Python path:
- Base classes:
General,Inline,Element- Categories:
Body,General,Inline- Visit/depart handlers:
html
Document one demo translator¶
Translators surface their base class, the visit/depart methods the class
itself defines, and the builder the module’s
docutils_demo_components.setup() registers them for via
set_translator() — including an override
badge:
Bulk translators demo¶
-
docutils_demo_components.DemoTextTranslatordocutils_demo_components.DemoTextTranslator
Translate paragraphs into plain text lines for the demo writer.
- Python path:
- Base class:
NodeVisitor- Overrides:
- Registered for builder:
demo-plain
Cross-referencing components¶
Component entries register targets in the docutils domain, so prose
can link to them: DemoReorderTransform resolves
to the entry above, and docutils_demo_components.DemoReorderTransform
spells out the full path. Every component type has a matching role —
DemoArticleReader links the reader entry the same
way.
The extension itself registers directives, not docutils roles or Sphinx config
values. The generated package reference below lists its registered surface from
the live docutils_demo_components.setup() calls.
Copyable config snippet
extensions = [
"sphinx_autodoc_docutils",
]
Demo module reference¶
The demo objects above, as plain Python API — the targets the
entries’ Python path facts link to:
Synthetic directives and roles for live autodoc-docutils demos.
Examples
>>> DemoBadgeDirective.required_arguments
1
>>> demo_badge_role.content
True
-
class docutils_demo.DemoBadgeDirective¶class docutils_demo.DemoBadgeDirective¶
Bases:
DirectiveRender a short badge-like paragraph for directive demos.
-
class docutils_demo.DemoCalloutDirective¶class docutils_demo.DemoCalloutDirective¶
Bases:
DirectiveRender a simple titled container for directive demos.
-
docutils_demo.demo_badge_role(name, rawtext, text, lineno, inliner, options=None, content=None)¶docutils_demo.demo_badge_role(name, rawtext, text, lineno, inliner, options=None, content=None)¶
Return a literal node with badge-style classes.
Examples
>>> nodes_, messages = demo_badge_role( ... "demo-badge", ... ":demo-badge:`Alpha`", ... "Alpha", ... 1, ... None, ... ) >>> nodes_[0].astext() 'Alpha' >>> messages []
Synthetic docutils components for live component-autodoc demos.
Grows one demo class per component type so the
docs/packages/sphinx-autodoc-docutils examples page can exercise
every auto* directive against realistic metadata.
Examples
>>> DemoReorderTransform.default_priority
760
-
class docutils_demo_components.DemoReorderTransform¶class docutils_demo_components.DemoReorderTransform¶
Bases:
TransformMove demo-badge paragraphs ahead of their sibling paragraphs.
Runs late in the read phase (priority 760) so it sees the fully parsed document but still precedes reference resolution.
-
class docutils_demo_components.DemoArticleReader¶class docutils_demo_components.DemoArticleReader¶
Bases:
ReaderRead standalone article sources with the demo transform applied.
Extends the stock standalone reader’s transform set with
DemoReorderTransformso demo badges surface first.
-
class docutils_demo_components.DemoLineParser¶class docutils_demo_components.DemoLineParser¶
Bases:
ParserParse line-oriented demo sources into one paragraph per line.
-
class docutils_demo_components.demo_marker¶class docutils_demo_components.demo_marker¶
Bases:
General,Inline,ElementInline marker node rendered as a highlighted
<mark>span.
-
docutils_demo_components.visit_demo_marker(translator, node)¶docutils_demo_components.visit_demo_marker(translator, node)¶
Open the
<mark>wrapper for a demo marker node.- Parameters:
translator (
NodeVisitor)node (
demo_marker)
- Return type:
-
docutils_demo_components.depart_demo_marker(translator, node)¶docutils_demo_components.depart_demo_marker(translator, node)¶
Close the
<mark>wrapper for a demo marker node.- Parameters:
translator (
NodeVisitor)node (
demo_marker)
- Return type:
-
class docutils_demo_components.DemoTextTranslator¶class docutils_demo_components.DemoTextTranslator¶
Bases:
NodeVisitorTranslate paragraphs into plain text lines for the demo writer.
-
class docutils_demo_components.DemoPlainWriter¶class docutils_demo_components.DemoPlainWriter¶
Bases:
WriterWrite documents as plain text lines, one paragraph per line.
Assigns
translator_classin__init__(the django-docutils style) rather than as a class attribute, which exercises the defensive resolution theautowriterdirective performs.
-
docutils_demo_components.setup(app)¶docutils_demo_components.setup(app)¶
Register the demo components with Sphinx.
Examples
>>> class FakeApp: ... def __init__(self) -> None: ... self.calls: list[tuple[str, object]] = [] ... def add_transform(self, cls: object) -> None: ... self.calls.append(("add_transform", cls)) ... def add_source_parser(self, cls: object) -> None: ... self.calls.append(("add_source_parser", cls)) ... def add_node(self, cls: object, **kwargs: object) -> None: ... self.calls.append(("add_node", cls)) ... def set_translator(self, name: str, cls: object, **kwargs: object) -> None: ... self.calls.append(("set_translator", cls)) >>> fake = FakeApp() >>> metadata = setup(fake) # type: ignore[arg-type] >>> ("add_transform", DemoReorderTransform) in fake.calls True >>> ("add_source_parser", DemoLineParser) in fake.calls True >>> ("add_node", demo_marker) in fake.calls True >>> ("set_translator", DemoTextTranslator) in fake.calls True >>> metadata["parallel_read_safe"] True
- Parameters:
app (
Sphinx)- Return type: