Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add github admonition translation #28

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions docs/src/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,26 @@ graph TD;
B-->D;
C-->D;
```


## GitHub Admonitions

GitHub admonitions are automatically translated to sphinx.

> [!NOTE]
> Note content

> [!TIP]
> Tip content

> [!IMPORTANT]
> Important content

> [!WARNING]
> Warning content

> [!CAUTION]
> Caution content



37 changes: 37 additions & 0 deletions yardang/conf.py.j2
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,45 @@ def run_add_version_links_to_toctree(app, doctree):
nodes[-1]["entries"].append((None, toc_entry))
nodes[-1]["includefiles"].append(toc_entry)


_GITHUB_ADMONITIONS = {
"> [!NOTE]": "note",
"> [!TIP]": "tip",
"> [!IMPORTANT]": "important",
"> [!WARNING]": "warning",
"> [!CAUTION]": "caution",
}

def run_convert_github_admonitions_to_rst(app, filename, lines):
# loop through lines, replace github admonitions
for i, orig_line in enumerate(lines):
orig_line_splits = orig_line.split("\n")
replacing = False
for j, line in enumerate(orig_line_splits):
# look for admonition key
for admonition_key in _GITHUB_ADMONITIONS:
if admonition_key in line:
line = line.replace(admonition_key, "\n```{}\n.. {}::\n".format("{eval-rst}", _GITHUB_ADMONITIONS[admonition_key]))
# start replacing quotes in subsequent lines
replacing = True
break
else:
# replace indent to match directive
if replacing and "> " in line:
line = line.replace("> ", " ")
elif replacing:
# missing "> ", so stop replacing and terminate directive
line = f"\n```\n{line}"
replacing = False
# swap line back in splits
orig_line_splits[j] = line
# swap line back in original
lines[i] = "\n".join(orig_line_splits)


def setup(app):
{# app.connect("builder-inited", run_create_previous_version_markdown) #}
app.connect("builder-inited", run_copyreadme)
app.connect("builder-inited", run_copycname)
app.connect("source-read", run_convert_github_admonitions_to_rst)
{# app.connect("doctree-read", run_add_version_links_to_toctree, priority=500) #}