|
1 | 1 | import click |
2 | 2 |
|
3 | 3 | import afterpython as ap |
4 | | -from afterpython.const import CONTENT_TYPES |
| 4 | +from afterpython.const import CONTENT_TYPES, PLACEHOLDER_INDEX_MARKER |
| 5 | + |
| 6 | + |
| 7 | +def _legacy_placeholder_content(content_type: str) -> str: |
| 8 | + return f"""--- |
| 9 | +title: ← {content_type.capitalize()} |
| 10 | +--- |
| 11 | +
|
| 12 | +This is a placeholder index page. The actual {content_type} landing page is rendered by SvelteKit. |
| 13 | +""" |
| 14 | + |
| 15 | + |
| 16 | +def _legacy_welcome_content(content_type: str) -> str: |
| 17 | + return f"""# Welcome to AfterPython |
| 18 | +
|
| 19 | +Welcome to your project's {content_type}! This is a starter page to help you get started. |
| 20 | +
|
| 21 | +## Getting Started |
| 22 | +
|
| 23 | +Replace this placeholder content with your own. Here's what you can do: |
| 24 | +
|
| 25 | +- Creating new `.md` or `.ipynb` files in the `afterpython/{content_type}/` directory |
| 26 | +- Writing in MyST Markdown format |
| 27 | +- Adding images to the `afterpython/static/` directory and referencing them |
| 28 | +
|
| 29 | +## Resources |
| 30 | +
|
| 31 | +- [AfterPython's Project Website](https://afterpython.afterpython.org) |
| 32 | +- [MyST Markdown Guide](https://mystmd.org) |
| 33 | +
|
| 34 | +Start building your amazing project! 🚀 |
| 35 | +""" |
| 36 | + |
| 37 | + |
| 38 | +def _is_afterpython_placeholder_index(index_md, content_type: str) -> bool: |
| 39 | + """Return True only for non-doc index.md files generated by AfterPython.""" |
| 40 | + content = index_md.read_text(encoding="utf-8", errors="ignore") |
| 41 | + normalized_content = content.strip() |
| 42 | + |
| 43 | + return ( |
| 44 | + PLACEHOLDER_INDEX_MARKER in content |
| 45 | + or normalized_content == _legacy_placeholder_content(content_type).strip() |
| 46 | + or normalized_content == _legacy_welcome_content(content_type).strip() |
| 47 | + ) |
5 | 48 |
|
6 | 49 |
|
7 | 50 | def create_placeholder_index_md_files(): |
@@ -71,10 +114,23 @@ def delete_placeholder_index_md_files(): |
71 | 114 | myst_yml_path = content_path / "myst.yml" |
72 | 115 | index_md = content_path / "index.md" |
73 | 116 |
|
74 | | - # Delete index.md from source |
| 117 | + # Delete index.md from source only when it is an AfterPython-generated placeholder. |
75 | 118 | if index_md.exists(): |
| 119 | + if not _is_afterpython_placeholder_index(index_md, content_type): |
| 120 | + raise click.ClickException( |
| 121 | + f"Found existing 'afterpython/{content_type}/index.md'.\n" |
| 122 | + f"\n" |
| 123 | + f"'afterpython/{content_type}/index.md' is reserved for internal use by AfterPython.\n" |
| 124 | + f"The '/{content_type}' route is owned by the SvelteKit listing page,\n" |
| 125 | + f"so user-authored non-doc index.md files are not supported.\n" |
| 126 | + f"\n" |
| 127 | + f"Please rename this file to something else " |
| 128 | + f"(e.g., '{content_type}_intro.md') and update the reference in " |
| 129 | + f"afterpython/{content_type}/myst.yml." |
| 130 | + ) |
| 131 | + |
76 | 132 | index_md.unlink() |
77 | | - click.echo(f"Deleted: {index_md}") |
| 133 | + click.echo(f"Deleted placeholder: {index_md}") |
78 | 134 |
|
79 | 135 | # Delete index.html from build output |
80 | 136 | index_html = content_path / "_build" / "html" / "index.html" |
|
0 commit comments