Skip to content

Commit 15264bf

Browse files
Docs: Fix format in Sphinx (#85)
1 parent 0b65571 commit 15264bf

File tree

4 files changed

+106
-3
lines changed

4 files changed

+106
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ test_venv
3030
docs/build
3131
docs/source/generated
3232
docs/source/examples
33+
docs/source/README_processed.md
3334

3435
# reports
3536
reports

docs/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ help:
2121
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
2222

2323
clean:
24-
@rm -rf $(BUILDDIR) $(SOURCEDIR)/examples $(SOURCEDIR)/generated
24+
@rm -rf $(BUILDDIR) $(SOURCEDIR)/examples $(SOURCEDIR)/generated $(SOURCEDIR)/README_processed.md

docs/source/conf.py

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
# documentation root, use os.path.abspath to make it absolute, like shown here.
2121

2222
from importlib import metadata
23+
from pathlib import Path
2324

2425
# -- Project information -----------------------------------------------------
2526

@@ -101,7 +102,108 @@
101102
}
102103

103104
# MyST parser settings
104-
myst_enable_extensions = []
105+
myst_enable_extensions = [
106+
"colon_fence",
107+
]
105108
myst_heading_anchors = 5
106109
myst_all_links_external = True
107110
suppress_warnings = ["myst.header"]
111+
112+
113+
def convert_github_admonitions(text):
114+
"""Convert GitHub-style admonitions to MyST format."""
115+
lines = text.split("\n")
116+
github_to_myst = {
117+
"> [!NOTE]": ":::{note}",
118+
"> [!TIP]": ":::{tip}",
119+
"> [!IMPORTANT]": ":::{important}",
120+
"> [!WARNING]": ":::{warning}",
121+
"> [!CAUTION]": ":::{caution}",
122+
}
123+
124+
converted_lines = []
125+
in_admonition = False
126+
127+
for line in lines:
128+
# Check if this line starts a GitHub admonition
129+
admonition_found = False
130+
for github_style, myst_style in github_to_myst.items():
131+
if line.strip().startswith(github_style):
132+
# Start of admonition
133+
converted_lines.append(myst_style)
134+
in_admonition = True
135+
admonition_found = True
136+
break
137+
138+
if not admonition_found:
139+
if in_admonition:
140+
strip_line = line.strip()
141+
if strip_line.startswith("> "):
142+
# Content of admonition - remove the "> " prefix
143+
content = strip_line[2:]
144+
converted_lines.append(content)
145+
elif strip_line == ">":
146+
# Empty line in admonition
147+
converted_lines.append("")
148+
elif strip_line == "":
149+
# Empty line - could be end of admonition or just spacing
150+
converted_lines.append("")
151+
else:
152+
# End of admonition
153+
converted_lines.append(":::")
154+
converted_lines.append("")
155+
converted_lines.append(line)
156+
in_admonition = False
157+
else:
158+
# Regular line
159+
converted_lines.append(line)
160+
161+
# Close any remaining open admonition
162+
if in_admonition:
163+
converted_lines.append(":::")
164+
165+
return "\n".join(converted_lines)
166+
167+
168+
def preprocess_readme_for_sphinx(app, config): # pylint: disable=unused-argument
169+
"""Preprocess README.md to convert GitHub admonitions before Sphinx build."""
170+
# Calculate paths relative to the docs directory
171+
docs_dir = Path(app.srcdir)
172+
project_root = docs_dir.parent.parent
173+
readme_path = project_root / "README.md"
174+
docs_readme_path = docs_dir / "README_processed.md"
175+
176+
# Remove existing processed README to ensure fresh generation
177+
if docs_readme_path.exists():
178+
docs_readme_path.unlink()
179+
print(f"[sphinx-hook] Removed existing {docs_readme_path}")
180+
181+
# Check if README exists
182+
if not readme_path.exists():
183+
print(f"[sphinx-hook] README.md not found at {readme_path}")
184+
return
185+
186+
# Read original README
187+
try:
188+
with open(readme_path, "r", encoding="utf-8") as f:
189+
content = f.read()
190+
191+
# Convert admonitions
192+
converted_content = convert_github_admonitions(content)
193+
194+
# Write processed README to docs directory
195+
with open(docs_readme_path, "w", encoding="utf-8") as f:
196+
f.write(converted_content)
197+
198+
print(f"[sphinx-hook] Processed README saved to {docs_readme_path}")
199+
200+
except Exception as e:
201+
print(f"[sphinx-hook] Error processing README: {e}")
202+
raise
203+
204+
205+
def setup(app):
206+
"""Setup Sphinx app with custom handlers."""
207+
# Connect the preprocessing function to config-inited event
208+
# This runs after configuration is loaded but before any documents are read
209+
app.connect("config-inited", preprocess_readme_for_sphinx)

docs/source/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.. include:: ../../README.md
1+
.. include:: README_processed.md
22
:parser: myst_parser.sphinx_
33

44

0 commit comments

Comments
 (0)