Skip to content
Myr

docs.py

This file

Path

scripts/docs.py

121 lines · 3.3 KiB · Python

Last changed by

🔧 Upgrade Material for MkDocs and remove insiders (#1416)Sebastián Ramírezbefore adoptionf327548

History of this file

Take it

1import logging2import os3import re4import subprocess5from http.server import HTTPServer, SimpleHTTPRequestHandler6from pathlib import Path78import typer910logging.basicConfig(level=logging.INFO)1112mkdocs_name = "mkdocs.yml"13en_docs_path = Path("")1415app = typer.Typer()161718@app.callback()19def callback() -> None:20    # For MacOS with Cairo21    os.environ["DYLD_FALLBACK_LIBRARY_PATH"] = "/opt/homebrew/lib"222324def generate_readme_content() -> str:25    en_index = en_docs_path / "docs" / "index.md"26    content = en_index.read_text("utf-8")27    match_pre = re.search(r"</style>\n\n", content)28    if not match_pre:29        raise RuntimeError("Couldn't find pre section (<style>) in index.md")30    frontmatter_end = match_pre.end()31    new_content = content[frontmatter_end:]32    # Remove content between <!-- only-mkdocs --> and <!-- /only-mkdocs -->33    new_content = re.sub(34        r"<!-- only-mkdocs -->.*?<!-- /only-mkdocs -->",35        "",36        new_content,37        flags=re.DOTALL,38    )39    return new_content404142@app.command()43def generate_readme() -> None:44    """45    Generate README.md content from main index.md46    """47    typer.echo("Generating README")48    readme_path = Path("README.md")49    new_content = generate_readme_content()50    readme_path.write_text(new_content, encoding="utf-8")515253@app.command()54def verify_readme() -> None:55    """56    Verify README.md content from main index.md57    """58    typer.echo("Verifying README")59    readme_path = Path("README.md")60    generated_content = generate_readme_content()61    readme_content = readme_path.read_text("utf-8")62    if generated_content != readme_content:63        typer.secho(64            "README.md outdated from the latest index.md", color=typer.colors.RED65        )66        raise typer.Abort()67    typer.echo("Valid README ✅")686970@app.command()71def live(dirty: bool = False) -> None:72    """73    Serve with livereload a docs site for a specific language.7475    This only shows the actual translated files, not the placeholders created with76    build-all.7778    Takes an optional LANG argument with the name of the language to serve, by default79    en.80    """81    # Enable line numbers during local development to make it easier to highlight82    args = ["mkdocs", "serve", "--dev-addr", "127.0.0.1:8008"]83    if dirty:84        args.append("--dirty")85    subprocess.run(args, env={**os.environ, "LINENUMS": "true"}, check=True)868788@app.command()89def build() -> None:90    """91    Build the docs.92    """93    print("Building docs")94    subprocess.run(["mkdocs", "build"], check=True)95    typer.secho("Successfully built docs", color=typer.colors.GREEN)969798@app.command()99def serve() -> None:100    """101    A quick server to preview a built site.102103    For development, prefer the command live (or just mkdocs serve).104105    This is here only to preview the documentation site.106107    Make sure you run the build command first.108    """109    typer.echo("Warning: this is a very simple server.")110    typer.echo("For development, use the command live instead.")111    typer.echo("This is here only to preview the documentation site.")112    typer.echo("Make sure you run the build command first.")113    os.chdir("site")114    server_address = ("", 8008)115    server = HTTPServer(server_address, SimpleHTTPRequestHandler)116    typer.echo("Serving at: http://127.0.0.1:8008")117    server.serve_forever()118119120if __name__ == "__main__":121    app()