|
|
|
|
|
import re
|
|
from pathlib import Path
|
|
|
|
README_TEMPLATE_FILEPATH = "readme_template.md"
|
|
GETTING_STARTED_TEMPLATE_FILEPATH = "guides/01_getting-started/01_quickstart.md"
|
|
|
|
readme_template = Path(README_TEMPLATE_FILEPATH).read_text()
|
|
getting_started_template = Path(GETTING_STARTED_TEMPLATE_FILEPATH).read_text()
|
|
getting_started_template = getting_started_template.replace("# Quickstart", "")
|
|
getting_started_template = getting_started_template.replace("Tip:", "> [!TIP]\n >")
|
|
|
|
|
|
code_tags = re.findall(r"\$code_([^\s]+)", getting_started_template)
|
|
demo_tags = re.findall(r"\$demo_([^\s]+)", getting_started_template)
|
|
codes = {}
|
|
demos = {}
|
|
|
|
for src in code_tags:
|
|
context = Path(f"demo/{src}/run.py").read_text()
|
|
|
|
context = re.sub(r"if __name__(.*[\n$]*)*", "demo.launch()", context)
|
|
codes[src] = f"```python\n{context}\n```\n"
|
|
|
|
for src in demo_tags:
|
|
demos[src] = f"![`{src}` demo](demo/{src}/screenshot.gif)"
|
|
|
|
|
|
|
|
getting_started_template = re.sub(r"^(#+)", r"#\1", getting_started_template, flags=re.MULTILINE)
|
|
readme_template = readme_template.replace("$getting_started", getting_started_template)
|
|
|
|
|
|
readme_template = re.sub(r"\$code_([^\s]+)", lambda x: codes[x.group(1)], readme_template)
|
|
readme_template = re.sub(r"\$demo_([^\s]+)", lambda x: demos[x.group(1)], readme_template)
|
|
|
|
|
|
EDITING_NOTE = ("<!-- DO NOT EDIT THIS FILE DIRECTLY. INSTEAD EDIT THE `readme_template.md` OR "
|
|
"`guides/1)getting_started/1)quickstart.md` TEMPLATES AND THEN RUN `render_readme.py` SCRIPT. -->")
|
|
Path("README.md").write_text(f"{EDITING_NOTE}\n\n{readme_template}")
|
|
|