Skip to content

Commit d0d5076

Browse files
Docs: Convert the README before Sphinx compilation
1 parent d430c01 commit d0d5076

File tree

5 files changed

+109
-8
lines changed

5 files changed

+109
-8
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

README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,10 @@ import dir_content_diff
6666
dir_content_diff.compare_trees("reference_dir", "compared_dir")
6767
```
6868

69-
::::{important}
70-
The order of the parameters is important: the first path is considered as the `reference`
71-
directory while the second one is the `compared` directory. Inverting the parameters may return
72-
a different result (in this example it would return that the file `sub_file_3.b` is missing).
73-
::::
69+
> [!WARNING]
70+
> The order of the parameters is important: the first path is considered as the `reference`
71+
> directory while the second one is the `compared` directory. Inverting the parameters may return
72+
> a different result (in this example it would return that the file `sub_file_3.b` is missing).
7473
7574
If all the files are identical, this code will return an empty dictionary because no difference
7675
was detected. As mentioned previously, this is because `dir-content-diff` is only looking for files

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: 102 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,107 @@
101102
}
102103

103104
# MyST parser settings
104-
myst_enable_extensions = ["colon_fence"]
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+
if line.strip().startswith("> "):
141+
# Content of admonition - remove the "> " prefix
142+
content = line[line.find("> ") + 2 :]
143+
converted_lines.append(content)
144+
elif line.strip() == ">":
145+
# Empty line in admonition
146+
converted_lines.append("")
147+
elif line.strip() == "":
148+
# Empty line - could be end of admonition or just spacing
149+
converted_lines.append("")
150+
else:
151+
# End of admonition
152+
converted_lines.append(":::")
153+
converted_lines.append("")
154+
converted_lines.append(line)
155+
in_admonition = False
156+
else:
157+
# Regular line
158+
converted_lines.append(line)
159+
160+
# Close any remaining open admonition
161+
if in_admonition:
162+
converted_lines.append(":::")
163+
164+
return "\n".join(converted_lines)
165+
166+
167+
def preprocess_readme_for_sphinx(app, config):
168+
"""Preprocess README.md to convert GitHub admonitions before Sphinx build."""
169+
# Calculate paths relative to the docs directory
170+
docs_dir = Path(app.srcdir)
171+
project_root = docs_dir.parent.parent
172+
readme_path = project_root / "README.md"
173+
docs_readme_path = docs_dir / "README_processed.md"
174+
175+
# Remove existing processed README to ensure fresh generation
176+
if docs_readme_path.exists():
177+
docs_readme_path.unlink()
178+
print(f"[sphinx-hook] Removed existing {docs_readme_path}")
179+
180+
# Check if README exists
181+
if not readme_path.exists():
182+
print(f"[sphinx-hook] README.md not found at {readme_path}")
183+
return
184+
185+
# Read original README
186+
try:
187+
with open(readme_path, "r", encoding="utf-8") as f:
188+
content = f.read()
189+
190+
# Convert admonitions
191+
converted_content = convert_github_admonitions(content)
192+
193+
# Write processed README to docs directory
194+
with open(docs_readme_path, "w", encoding="utf-8") as f:
195+
f.write(converted_content)
196+
197+
print(f"[sphinx-hook] Processed README saved to {docs_readme_path}")
198+
199+
except Exception as e:
200+
print(f"[sphinx-hook] Error processing README: {e}")
201+
raise
202+
203+
204+
def setup(app):
205+
"""Setup Sphinx app with custom handlers."""
206+
# Connect the preprocessing function to config-inited event
207+
# This runs after configuration is loaded but before any documents are read
208+
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)