click_extra.markdown.myst module¶
Convert reST to MyST: the docstrings of Python sources, and sphinx-apidoc pages.
convert_source() transforms the reST markup of docstrings and comments
to MyST markdown. The companion Sphinx extension
click_extra.sphinx.myst_docstrings converts the MyST back to reST at
build time, so sphinx.ext.autodoc still works.
Conversions applied (in order):
Cross-references: a reST role, its name between colons, becomes a MyST role, its name in braces.
Named links: a reST hyperlink embedding its URL becomes a Markdown link.
Inline code: double-backtick literals become single-backtick code spans.
#:comment blocks: strip prefix, convert directives, re-wrap.Directives: a reST directive and its indented body become a backtick directive fence.
Only docstrings (bare string-expression statements, located with ast)
and comments (located with tokenize) are transformed. String
literals, f-strings, and every other piece of runtime code pass through
byte-for-byte: a regex pattern or an error message that happens to contain
reST markup is not documentation and must not be rewritten.
Safe to re-run: already-converted MyST syntax does not match the reST patterns, so the script is idempotent.
Note
f-string exclusion: Cross-reference and inline-code regexes exclude
targets containing { so that interpolation-style placeholders (like
{self.id} in a documented format template) are untouched.
Note
Nested fences stay as reST: A directive whose body already contains a triple-backtick fence is left in reST. Converting it would nest two same-level fences, which markdown cannot delimit, and the build-time extension passes reST through unchanged anyway.
Note
Nested directives stay as reST: A .. code-block:: inside a
converted backtick-fenced warning directive is emitted as-is. The
hook handles this correctly because it re-indents the body when
converting back to reST.
Note
Link labels lose backticks: reST has no nested markup, so the hook drops the backticks of a code span in a link label before emitting the reST link.
convert_apidoc_rst_to_myst() handles only the narrow reST subset that
sphinx-apidoc generates: section headings (title + underline), automodule
directives with indented options, and structural headers like Submodules.
Note
Autodoc directives cannot be used as native MyST directives because they
perform internal rST nested parsing that requires an rST parser context
only {eval-rst} provides. See
executablebooks/MyST-Parser#587.
The patterns are compiled on first use, through the cache of re.
- click_extra.markdown.myst.convert_xrefs(text)[source]¶
Convert reST cross-references to MyST syntax.
- Return type:
- click_extra.markdown.myst.convert_links(text)[source]¶
Convert reST named hyperlinks to markdown links.
- Return type:
- click_extra.markdown.myst.convert_inline_code(text)[source]¶
Convert reST double-backtick literals to single-backtick.
- Return type:
- click_extra.markdown.myst.convert_directives(text)[source]¶
Convert reST directives to MyST backtick fences in a single pass.
Body lines are collected by indentation (deeper than the
..line) and dedented to the fence level. Trailing blank lines between consecutive directives are preserved as a single separator.Nested reST directives (like
.. code-block::inside.. warning::) are emitted as-is in the fence body. The hook re-indents them during the reST round-trip.A directive whose body contains a triple-backtick fence is left in reST entirely: converting it would produce two same-level fences that markdown cannot tell apart.
Symmetrically, existing fences are opaque: a reST directive inside a fence body is exactly what a previous conversion of nested directives produces (the inner one stays reST by design), so re-scanning it would break idempotency and nest same-level fences.
Todo
Convert a directive whose body holds a fence to a longer outer fence, instead of leaving it in reST: the build-time extension
click_extra.sphinx.myst_docstringsreads fences of any length.- Return type:
- click_extra.markdown.myst.convert_comment_blocks(text)[source]¶
Convert
#:Sphinx comment docstrings.Consecutive
#:lines are collected, the prefix is stripped, all conversions are applied to the extracted content, and the prefix is re-added.- Return type:
- click_extra.markdown.myst.convert_source(source)[source]¶
Convert reST markup to MyST in a Python module’s docstrings and comments.
Docstrings get the full pipeline, in an order that matters: inline constructs (cross-references, links, inline code) run before directives so that directive bodies are already converted when they are dedented into fences. Comments get the inline conversions only, except consecutive full-line
#:comments, whose directives are also converted throughconvert_comment_blocks(). Everything outside docstrings and comments passes through byte-for-byte.- Return type:
- click_extra.markdown.myst.convert_file(filepath)[source]¶
Apply all conversions to a single Python file.
- click_extra.markdown.myst.convert_directory(directory)[source]¶
Convert all Python files in a directory from reST to MyST docstrings.
- click_extra.markdown.myst.convert_apidoc_rst_to_myst(content)[source]¶
Convert
sphinx-apidocRST to MyST markdown with{eval-rst}blocks.
- click_extra.markdown.myst.convert_rst_files_in_directory(directory)[source]¶
Convert
sphinx-apidocRST files to MyST markdown in the given directory.For each
.rstfile containing.. automodule::directives:If a
.mdfile with the same stem exists, delete the.rst(the existing markdown takes precedence).Otherwise, convert the RST content to MyST and write a
.mdfile, then delete the.rst.