Skip to content
Myr

docs.py

This file

Path

scripts/docs.py

135 lines · 3.8 KiB · Python

Last changed by

🔨 Update docs.py script to enable dirty reload conditionally (#918)Sebastián Ramírezbefore adoption53ccae1

History of this file

Take it

1import logging2import os3import re4import subprocess5from functools import lru_cache6from http.server import HTTPServer, SimpleHTTPRequestHandler7from importlib import metadata8from pathlib import Path910import typer1112logging.basicConfig(level=logging.INFO)1314mkdocs_name = "mkdocs.yml"15en_docs_path = Path("")1617app = typer.Typer()181920@lru_cache21def is_mkdocs_insiders() -> bool:22    version = metadata.version("mkdocs-material")23    return "insiders" in version242526@app.callback()27def callback() -> None:28    if is_mkdocs_insiders():29        os.environ["INSIDERS_FILE"] = "./mkdocs.insiders.yml"30    # For MacOS with insiders and Cairo31    os.environ["DYLD_FALLBACK_LIBRARY_PATH"] = "/opt/homebrew/lib"323334def generate_readme_content() -> str:35    en_index = en_docs_path / "docs" / "index.md"36    content = en_index.read_text("utf-8")37    match_pre = re.search(r"</style>\n\n", content)38    if not match_pre:39        raise RuntimeError("Couldn't find pre section (<style>) in index.md")40    frontmatter_end = match_pre.end()41    new_content = content[frontmatter_end:]42    # Remove content between <!-- only-mkdocs --> and <!-- /only-mkdocs -->43    new_content = re.sub(44        r"<!-- only-mkdocs -->.*?<!-- /only-mkdocs -->",45        "",46        new_content,47        flags=re.DOTALL,48    )49    return new_content505152@app.command()53def generate_readme() -> None:54    """55    Generate README.md content from main index.md56    """57    typer.echo("Generating README")58    readme_path = Path("README.md")59    new_content = generate_readme_content()60    readme_path.write_text(new_content, encoding="utf-8")616263@app.command()64def verify_readme() -> None:65    """66    Verify README.md content from main index.md67    """68    typer.echo("Verifying README")69    readme_path = Path("README.md")70    generated_content = generate_readme_content()71    readme_content = readme_path.read_text("utf-8")72    if generated_content != readme_content:73        typer.secho(74            "README.md outdated from the latest index.md", color=typer.colors.RED75        )76        raise typer.Abort()77    typer.echo("Valid README ✅")787980@app.command()81def live(dirty: bool = False) -> None:82    """83    Serve with livereload a docs site for a specific language.8485    This only shows the actual translated files, not the placeholders created with86    build-all.8788    Takes an optional LANG argument with the name of the language to serve, by default89    en.90    """91    # Enable line numbers during local development to make it easier to highlight92    args = ["mkdocs", "serve", "--dev-addr", "127.0.0.1:8008"]93    if dirty:94        args.append("--dirty")95    subprocess.run(args, env={**os.environ, "LINENUMS": "true"}, check=True)969798@app.command()99def build() -> None:100    """101    Build the docs.102    """103    insiders_env_file = os.environ.get("INSIDERS_FILE")104    print(f"Insiders file {insiders_env_file}")105    if is_mkdocs_insiders():106        print("Using insiders")107    print("Building docs")108    subprocess.run(["mkdocs", "build"], check=True)109    typer.secho("Successfully built docs", color=typer.colors.GREEN)110111112@app.command()113def serve() -> None:114    """115    A quick server to preview a built site.116117    For development, prefer the command live (or just mkdocs serve).118119    This is here only to preview the documentation site.120121    Make sure you run the build command first.122    """123    typer.echo("Warning: this is a very simple server.")124    typer.echo("For development, use the command live instead.")125    typer.echo("This is here only to preview the documentation site.")126    typer.echo("Make sure you run the build command first.")127    os.chdir("site")128    server_address = ("", 8008)129    server = HTTPServer(server_address, SimpleHTTPRequestHandler)130    typer.echo("Serving at: http://127.0.0.1:8008")131    server.serve_forever()132133134if __name__ == "__main__":135    app()