Skip to content

Commit 3cbb0ef

Browse files
docs: Integrate click-extra for GitHub admonitions in docs
Added click-extra[sphinx] to requirements and enabled its Sphinx extension to convert GitHub admonitions to MyST format. Removed custom workaround code for admonition conversion from conf.py, simplifying configuration.
1 parent 044c9ea commit 3cbb0ef

2 files changed

Lines changed: 3 additions & 60 deletions

File tree

docs/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
click-extra[sphinx]==7.2.0
12
furo==2025.9.25
23
myst-parser==4.0.1
34
rstcheck[sphinx]==6.2.5

docs/source/conf.py

Lines changed: 2 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
3333
# ones.
3434
extensions = [
35+
'click_extra.sphinx', # convert GitHub admonitions to MyST format
3536
'myst_parser', # enable markdown files
3637
'sphinx.ext.autosectionlabel',
3738
'sphinx.ext.todo', # enable to-do sections
@@ -41,7 +42,7 @@
4142

4243
# https://myst-parser.readthedocs.io/en/latest/syntax/optional.html
4344
myst_enable_extensions = [
44-
"colon_fence", # Only enabled so we can implement am adhoc fix to render GitHub admonitions
45+
"colon_fence", # Required to render GitHub admonitions
4546
]
4647

4748
# Add any paths that contain templates here, relative to this directory.
@@ -98,62 +99,3 @@
9899
# disable epub mimetype warnings
99100
# https://github.com/readthedocs/readthedocs.org/blob/eadf6ac6dc6abc760a91e1cb147cc3c5f37d1ea8/docs/conf.py#L235-L236
100101
suppress_warnings = ["epub.unknown_project_files"]
101-
102-
103-
# Issue https://github.com/executablebooks/MyST-Parser/issues/845
104-
# GitHub admonitions with Sphinx/MyST
105-
# Workaround template adapted from (with some changes):
106-
# https://github.com/python-project-templates/yardang/blob/f77348d45dcf0eb130af304f79c0bfb92ab90e0c/yardang/conf.py.j2#L156-L188
107-
108-
# https://spdx.github.io/spdx-spec/v2.3/file-tags/#h3-snippet-tags-format
109-
# SPDX-SnippetBegin
110-
# SPDX-License-Identifier: Apache-2.0
111-
# SPDX-SnippetCopyrightText: Tim Paine / the yardang authors <t.paine154@gmail.com">
112-
113-
_GITHUB_ADMONITIONS = {
114-
"> [!NOTE]": "note",
115-
"> [!TIP]": "tip",
116-
"> [!IMPORTANT]": "important",
117-
"> [!WARNING]": "warning",
118-
"> [!CAUTION]": "caution",
119-
}
120-
121-
122-
def convert_gh_admonitions(app, relative_path, parent_docname, contents):
123-
# TODO handle nested directives -> recursion
124-
# loop through content lines, replace github admonitions
125-
for i, orig_content in enumerate(contents):
126-
orig_line_splits = orig_content.split("\n")
127-
replacing = False
128-
for j, line in enumerate(orig_line_splits):
129-
# look for admonition key
130-
line_roi = line.lstrip()
131-
for admonition_key in _GITHUB_ADMONITIONS:
132-
if line_roi.startswith(admonition_key):
133-
line = line.replace(admonition_key, ":::{" + _GITHUB_ADMONITIONS[admonition_key] + "}")
134-
# start replacing quotes in subsequent lines
135-
replacing = True
136-
break
137-
else: # no break
138-
if not replacing:
139-
continue
140-
# remove GH directive to match MyST directive
141-
# since we are replacing on the original line, this will preserve the right indent, if any
142-
if line_roi.startswith("> "):
143-
line = line.replace("> ", "", 1)
144-
elif line_roi.rstrip() == ">":
145-
line = line.replace(">", "", 1)
146-
else:
147-
# missing "> ", so stop replacing and terminate directive
148-
line = f":::\n{line}"
149-
replacing = False
150-
# swap line back in splits
151-
orig_line_splits[j] = line
152-
# swap line back in original
153-
contents[i] = "\n".join(orig_line_splits)
154-
155-
# SPDX-SnippetEnd
156-
157-
158-
def setup(app):
159-
app.connect("include-read", convert_gh_admonitions)

0 commit comments

Comments
 (0)