|
39 | 39 | 'sphinx_copybutton', # add a copy button to code blocks |
40 | 40 | ] |
41 | 41 |
|
| 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 | + |
42 | 47 | # Add any paths that contain templates here, relative to this directory. |
43 | 48 | # templates_path = ['_templates'] |
44 | 49 |
|
|
94 | 99 | # disable epub mimetype warnings |
95 | 100 | # https://github.com/readthedocs/readthedocs.org/blob/eadf6ac6dc6abc760a91e1cb147cc3c5f37d1ea8/docs/conf.py#L235-L236 |
96 | 101 | 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) |
0 commit comments