docs.py
This file
Path
scripts/docs.py
130 lines · 3.5 KiB · Python
Last changed by
🔧 Update MkDocs configs and docs build setup (#900)Sebastián Ramírezbefore adoption0f4b07f
History of this fileTake it
🔧 Update MkDocs configs and docs build setup (#900)Sebastián Ramírezbefore adoption0f4b07f
lines 1–130why, one step back1import logging2import os3import re4import subprocess5from functools import lru_cache6from http.server import HTTPServer, SimpleHTTPRequestHandler7from importlib import metadata8from pathlib import Path910import mkdocs.commands.build11import mkdocs.commands.serve12import mkdocs.config13import mkdocs.utils14import typer1516logging.basicConfig(level=logging.INFO)1718mkdocs_name = "mkdocs.yml"19en_docs_path = Path("")2021app = typer.Typer()222324@lru_cache25def is_mkdocs_insiders() -> bool:26 version = metadata.version("mkdocs-material")27 return "insiders" in version282930@app.callback()31def callback() -> None:32 if is_mkdocs_insiders():33 os.environ["INSIDERS_FILE"] = "./mkdocs.insiders.yml"34 # For MacOS with insiders and Cairo35 os.environ["DYLD_FALLBACK_LIBRARY_PATH"] = "/opt/homebrew/lib"363738def generate_readme_content() -> str:39 en_index = en_docs_path / "docs" / "index.md"40 content = en_index.read_text("utf-8")41 match_pre = re.search(r"</style>\n\n", content)42 if not match_pre:43 raise RuntimeError("Couldn't find pre section (<style>) in index.md")44 frontmatter_end = match_pre.end()45 new_content = content[frontmatter_end:]46 return new_content474849@app.command()50def generate_readme() -> None:51 """52 Generate README.md content from main index.md53 """54 typer.echo("Generating README")55 readme_path = Path("README.md")56 new_content = generate_readme_content()57 readme_path.write_text(new_content, encoding="utf-8")585960@app.command()61def verify_readme() -> None:62 """63 Verify README.md content from main index.md64 """65 typer.echo("Verifying README")66 readme_path = Path("README.md")67 generated_content = generate_readme_content()68 readme_content = readme_path.read_text("utf-8")69 if generated_content != readme_content:70 typer.secho(71 "README.md outdated from the latest index.md", color=typer.colors.RED72 )73 raise typer.Abort()74 typer.echo("Valid README ✅")757677@app.command()78def live() -> None:79 """80 Serve with livereload a docs site for a specific language.8182 This only shows the actual translated files, not the placeholders created with83 build-all.8485 Takes an optional LANG argument with the name of the language to serve, by default86 en.87 """88 # Enable line numbers during local development to make it easier to highlight89 os.environ["LINENUMS"] = "true"90 mkdocs.commands.serve.serve(dev_addr="127.0.0.1:8008")919293@app.command()94def build() -> None:95 """96 Build the docs.97 """98 insiders_env_file = os.environ.get("INSIDERS_FILE")99 print(f"Insiders file {insiders_env_file}")100 if is_mkdocs_insiders():101 print("Using insiders")102 print("Building docs")103 subprocess.run(["mkdocs", "build"], check=True)104 typer.secho("Successfully built docs", color=typer.colors.GREEN)105106107@app.command()108def serve() -> None:109 """110 A quick server to preview a built site.111112 For development, prefer the command live (or just mkdocs serve).113114 This is here only to preview the documentation site.115116 Make sure you run the build command first.117 """118 typer.echo("Warning: this is a very simple server.")119 typer.echo("For development, use the command live instead.")120 typer.echo("This is here only to preview the documentation site.")121 typer.echo("Make sure you run the build command first.")122 os.chdir("site")123 server_address = ("", 8008)124 server = HTTPServer(server_address, SimpleHTTPRequestHandler)125 typer.echo("Serving at: http://127.0.0.1:8008")126 server.serve_forever()127128129if __name__ == "__main__":130 app()