Skip to content

Commit 9751cdc

Browse files
committed
Add github admonition translation
1 parent 1c89781 commit 9751cdc

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

docs/src/configuration.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,26 @@ graph TD;
168168
B-->D;
169169
C-->D;
170170
```
171+
172+
173+
## GitHub Admonitions
174+
175+
GitHub admonitions are automatically translated to sphinx.
176+
177+
> [!NOTE]
178+
> Note content
179+
180+
> [!TIP]
181+
> Tip content
182+
183+
> [!IMPORTANT]
184+
> Important content
185+
186+
> [!WARNING]
187+
> Warning content
188+
189+
> [!CAUTION]
190+
> Caution content
191+
192+
193+

yardang/conf.py.j2

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,45 @@ def run_add_version_links_to_toctree(app, doctree):
147147
nodes[-1]["entries"].append((None, toc_entry))
148148
nodes[-1]["includefiles"].append(toc_entry)
149149

150+
151+
_GITHUB_ADMONITIONS = {
152+
"> [!NOTE]": "note",
153+
"> [!TIP]": "tip",
154+
"> [!IMPORTANT]": "important",
155+
"> [!WARNING]": "warning",
156+
"> [!CAUTION]": "caution",
157+
}
158+
159+
def run_convert_github_admonitions_to_rst(app, filename, lines):
160+
# loop through lines, replace github admonitions
161+
for i, orig_line in enumerate(lines):
162+
orig_line_splits = orig_line.split("\n")
163+
replacing = False
164+
for j, line in enumerate(orig_line_splits):
165+
# look for admonition key
166+
for admonition_key in _GITHUB_ADMONITIONS:
167+
if admonition_key in line:
168+
line = line.replace(admonition_key, "\n```{}\n.. {}::\n".format("{eval-rst}", _GITHUB_ADMONITIONS[admonition_key]))
169+
# start replacing quotes in subsequent lines
170+
replacing = True
171+
break
172+
else:
173+
# replace indent to match directive
174+
if replacing and "> " in line:
175+
line = line.replace("> ", " ")
176+
elif replacing:
177+
# missing "> ", so stop replacing and terminate directive
178+
line = f"\n```\n{line}"
179+
replacing = False
180+
# swap line back in splits
181+
orig_line_splits[j] = line
182+
# swap line back in original
183+
lines[i] = "\n".join(orig_line_splits)
184+
185+
150186
def setup(app):
151187
{# app.connect("builder-inited", run_create_previous_version_markdown) #}
152188
app.connect("builder-inited", run_copyreadme)
153189
app.connect("builder-inited", run_copycname)
190+
app.connect("source-read", run_convert_github_admonitions_to_rst)
154191
{# app.connect("doctree-read", run_add_version_links_to_toctree, priority=500) #}

0 commit comments

Comments
 (0)