|
32 | 32 | # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom |
33 | 33 | # ones. |
34 | 34 | extensions = [ |
| 35 | + 'click_extra.sphinx', # convert GitHub admonitions to MyST format |
35 | 36 | 'myst_parser', # enable markdown files |
36 | 37 | 'sphinx.ext.autosectionlabel', |
37 | 38 | 'sphinx.ext.todo', # enable to-do sections |
|
41 | 42 |
|
42 | 43 | # https://myst-parser.readthedocs.io/en/latest/syntax/optional.html |
43 | 44 | 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 |
45 | 46 | ] |
46 | 47 |
|
47 | 48 | # Add any paths that contain templates here, relative to this directory. |
|
98 | 99 | # disable epub mimetype warnings |
99 | 100 | # https://github.com/readthedocs/readthedocs.org/blob/eadf6ac6dc6abc760a91e1cb147cc3c5f37d1ea8/docs/conf.py#L235-L236 |
100 | 101 | 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