|
20 | 20 | # documentation root, use os.path.abspath to make it absolute, like shown here. |
21 | 21 |
|
22 | 22 | from importlib import metadata |
| 23 | +from pathlib import Path |
23 | 24 |
|
24 | 25 | # -- Project information ----------------------------------------------------- |
25 | 26 |
|
|
101 | 102 | } |
102 | 103 |
|
103 | 104 | # MyST parser settings |
104 | | -myst_enable_extensions = [] |
| 105 | +myst_enable_extensions = [ |
| 106 | + "colon_fence", |
| 107 | +] |
105 | 108 | myst_heading_anchors = 5 |
106 | 109 | myst_all_links_external = True |
107 | 110 | 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) |
0 commit comments