Skip to content
Myr

docs.py

This file

Path

scripts/docs.py

137 lines · 3.8 KiB · Python

Last changed by

💄 Add dark mode logo (#907)Sebastián Ramírezbefore adoptiond3fe76a

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 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    # Remove content between <!-- only-mkdocs --> and <!-- /only-mkdocs -->47    new_content = re.sub(48        r"<!-- only-mkdocs -->.*?<!-- /only-mkdocs -->",49        "",50        new_content,51        flags=re.DOTALL,52    )53    return new_content545556@app.command()57def generate_readme() -> None:58    """59    Generate README.md content from main index.md60    """61    typer.echo("Generating README")62    readme_path = Path("README.md")63    new_content = generate_readme_content()64    readme_path.write_text(new_content, encoding="utf-8")656667@app.command()68def verify_readme() -> None:69    """70    Verify README.md content from main index.md71    """72    typer.echo("Verifying README")73    readme_path = Path("README.md")74    generated_content = generate_readme_content()75    readme_content = readme_path.read_text("utf-8")76    if generated_content != readme_content:77        typer.secho(78            "README.md outdated from the latest index.md", color=typer.colors.RED79        )80        raise typer.Abort()81    typer.echo("Valid README ✅")828384@app.command()85def live() -> None:86    """87    Serve with livereload a docs site for a specific language.8889    This only shows the actual translated files, not the placeholders created with90    build-all.9192    Takes an optional LANG argument with the name of the language to serve, by default93    en.94    """95    # Enable line numbers during local development to make it easier to highlight96    os.environ["LINENUMS"] = "true"97    mkdocs.commands.serve.serve(dev_addr="127.0.0.1:8008")9899100@app.command()101def build() -> None:102    """103    Build the docs.104    """105    insiders_env_file = os.environ.get("INSIDERS_FILE")106    print(f"Insiders file {insiders_env_file}")107    if is_mkdocs_insiders():108        print("Using insiders")109    print("Building docs")110    subprocess.run(["mkdocs", "build"], check=True)111    typer.secho("Successfully built docs", color=typer.colors.GREEN)112113114@app.command()115def serve() -> None:116    """117    A quick server to preview a built site.118119    For development, prefer the command live (or just mkdocs serve).120121    This is here only to preview the documentation site.122123    Make sure you run the build command first.124    """125    typer.echo("Warning: this is a very simple server.")126    typer.echo("For development, use the command live instead.")127    typer.echo("This is here only to preview the documentation site.")128    typer.echo("Make sure you run the build command first.")129    os.chdir("site")130    server_address = ("", 8008)131    server = HTTPServer(server_address, SimpleHTTPRequestHandler)132    typer.echo("Serving at: http://127.0.0.1:8008")133    server.serve_forever()134135136if __name__ == "__main__":137    app()