Skip to content

Commit 55f4d9f

Browse files
docs: render github admonitions in markdown files (#527)
1 parent c642786 commit 55f4d9f

7 files changed

Lines changed: 70 additions & 6 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
```{include} ../../../COMMUNITY_RULES.md
2+
```

docs/source/community/community_rules.rst

Lines changed: 0 additions & 2 deletions
This file was deleted.

docs/source/conf.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@
3939
'sphinx_copybutton', # add a copy button to code blocks
4040
]
4141

42+
# https://myst-parser.readthedocs.io/en/latest/syntax/optional.html
43+
myst_enable_extensions = [
44+
"colon_fence", # Only enabled so we can implement am adhoc fix to render GitHub admonitions
45+
]
46+
4247
# Add any paths that contain templates here, relative to this directory.
4348
# templates_path = ['_templates']
4449

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

docs/source/developers/code_of_conduct.rst

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
```{include} ../../../CONTRIBUTING.md
2+
```

docs/source/developers/contributing.rst

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)