A syntax-example directive that shows markup as source and rendered result.
Project description
sphinx-syntax-example
A Sphinx extension adding the
syntax-exampledirective, which shows a block of markup as both its raw source and its rendered result.
The directive renders its content twice, stacked inside one framed box: a syntax-highlighted source pane showing what to type, and a render pane showing what it produces. It is useful for documentation that teaches a markup syntax, where readers need to see the input alongside the output.
Installation
Requires Python 3.11+ and Sphinx 7.2+.
pip install sphinx-syntax-example
Then add it to the extensions list in your conf.py:
extensions = [
# ...
"sphinx_syntax_example",
]
The extension ships a small, theme-agnostic stylesheet that is registered automatically on HTML builds (it defines its own colours with fallbacks that pick up the active theme's variables when present, and looks right on Furo in both light and dark modes).
Usage
Write a syntax-example directive with the markup you want to demonstrate
as its content. An optional argument sets the title shown above the block
(defaulting to Example):
.. syntax-example:: An optional title
Any **reStructuredText** or MyST markup here.
The block above produces a framed box titled An optional title, containing
the highlighted source Any **reStructuredText** or MyST markup here.
and, below it, that markup rendered (with reStructuredText in bold).
The output is a pure function of the directive instance — there is no auto-numbering and no cross-document state — so the block is reproducible and safe under parallel builds.
Options and arguments
| Part | Description |
|---|---|
| (argument) | The title shown as a rubric above the block. Defaults to Example; inline markup is parsed. |
:highlight: |
Override the source pane's Pygments highlight language (for example :highlight: python). |
If :highlight: is omitted, the language is inferred from the document's format
via the project's configured source-suffix mapping
(sphinx.util.get_filetype over source_suffix — the same knowledge Sphinx's
router uses to pick the parser). A Markdown document yields a myst lexer when
one is registered (as myst-parser registers)
else markdown, and everything else yields rst.
An unknown :highlight: value falls back to the inferred language
(with a verbose-level note) rather than failing a strict -W build.
Subclassing
Downstream packages that want an aliased directive
(for example .. need-example:: with auto-numbering, a different class prefix,
or no title at all) subclass SyntaxExampleDirective
and register the subclass under the alias name in a setup(app) of their own.
Behaviour is customised through small named seams and class attributes,
never by re-implementing run:
- the class attributes
default_title,wrapper_classes,source_classesandrender_classes; default_language— how the source language is inferred;format_title— the title/numbering seam (returnNoneto drop the title, as a numbering-freemyst-examplewould);build_title_node— how the resolved title becomes a node (returnNoneto suppress the rubric element entirely);source_text— what the source pane shows;render_into— what is nested-parsed into the render pane.
The last two are the seam for a "shown source differs from rendered output"
directive: override source_text to return the shown text
and render_into to render the alternative output.
from sphinx_syntax_example import SyntaxExampleDirective
class NumberedExample(SyntaxExampleDirective):
def format_title(self, raw_title):
meta = self.env.metadata[self.env.docname]
number = meta.setdefault("_ex_number", 1)
meta["_ex_number"] = number + 1
return f"{raw_title or 'Example'} {number}"
def setup(app):
app.add_directive("numbered-example", NumberedExample)
The full override surface is documented in the module docstring.
The bespoke "example" directives shipped by sphinx-needs and myst-parser (each rendering source next to output) can be replaced by a subclass of this directive.
Contributing
See CONTRIBUTING.md.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sphinx_syntax_example-0.1.1.tar.gz.
File metadata
- Download URL: sphinx_syntax_example-0.1.1.tar.gz
- Upload date:
- Size: 17.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f4c95c9b8cc291bdfec179a2073b67564f36988712f0b0df252aee1a5fe82ca7
|
|
| MD5 |
e42c916f07c86e59e763716b1fbd57ec
|
|
| BLAKE2b-256 |
350c5aa46b865450b2fee9e8fa3a4cce42b53a64201ed412b9282489c8998f3c
|
Provenance
The following attestation bundles were made for sphinx_syntax_example-0.1.1.tar.gz:
Publisher:
tests.yml on sphinx-extensions2/sphinx-syntax-example
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sphinx_syntax_example-0.1.1.tar.gz -
Subject digest:
f4c95c9b8cc291bdfec179a2073b67564f36988712f0b0df252aee1a5fe82ca7 - Sigstore transparency entry: 2258015582
- Sigstore integration time:
-
Permalink:
sphinx-extensions2/sphinx-syntax-example@fc0c5398b5604b2e0af125e424ea4911cb55e692 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/sphinx-extensions2
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
tests.yml@fc0c5398b5604b2e0af125e424ea4911cb55e692 -
Trigger Event:
push
-
Statement type:
File details
Details for the file sphinx_syntax_example-0.1.1-py3-none-any.whl.
File metadata
- Download URL: sphinx_syntax_example-0.1.1-py3-none-any.whl
- Upload date:
- Size: 10.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f245c0c3cb75b5d178c516183b34a2569288ec8936699e6378f6b1482859f8be
|
|
| MD5 |
9bfc50a725141ff2d9ad986346e5298e
|
|
| BLAKE2b-256 |
bb06281804dfeeb83699d30bccb36dea6f3f2ea32da2f5fe18c16386bc118deb
|
Provenance
The following attestation bundles were made for sphinx_syntax_example-0.1.1-py3-none-any.whl:
Publisher:
tests.yml on sphinx-extensions2/sphinx-syntax-example
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sphinx_syntax_example-0.1.1-py3-none-any.whl -
Subject digest:
f245c0c3cb75b5d178c516183b34a2569288ec8936699e6378f6b1482859f8be - Sigstore transparency entry: 2258015816
- Sigstore integration time:
-
Permalink:
sphinx-extensions2/sphinx-syntax-example@fc0c5398b5604b2e0af125e424ea4911cb55e692 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/sphinx-extensions2
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
tests.yml@fc0c5398b5604b2e0af125e424ea4911cb55e692 -
Trigger Event:
push
-
Statement type: