Skip to content
Myr

docs.py

This file

Path

scripts/docs.py

136 lines · 3.7 KiB · Python

Last changed by

👷 Upgrade build docs configs (#914)Sebastián Ramírezbefore adoption5b82463

History of this file

Take it

🔧 Update MkDocs configs and docs build setup (#900)Sebastián Ramírezbefore adoption0f4b07f

lines 1–41why, one step back
1import logging
2import 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:]

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

lines 42–48why, one step back
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 )

🔧 Update MkDocs configs and docs build setup (#900)Sebastián Ramírezbefore adoption0f4b07f

lines 49–91why, one step back
49 return new_content
505152@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() -> 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 highlight

👷 Upgrade build docs configs (#914)Sebastián Ramírezbefore adoption5b82463

lines 92–96why, one step back
92 subprocess.run(
93 ["mkdocs", "serve", "--dev-addr", "127.0.0.1:8008", "--dirty"],94 env={**os.environ, "LINENUMS": "true"},95 check=True,96 )

🔧 Update MkDocs configs and docs build setup (#900)Sebastián Ramírezbefore adoption0f4b07f

lines 97–136why, one step back
97
9899@app.command()100def build() -> None:101 """102 Build the docs.103 """104 insiders_env_file = os.environ.get("INSIDERS_FILE")105 print(f"Insiders file {insiders_env_file}")106 if is_mkdocs_insiders():107 print("Using insiders")108 print("Building docs")109 subprocess.run(["mkdocs", "build"], check=True)110 typer.secho("Successfully built docs", color=typer.colors.GREEN)111112113@app.command()114def serve() -> None:115 """116 A quick server to preview a built site.117118 For development, prefer the command live (or just mkdocs serve).119120 This is here only to preview the documentation site.121122 Make sure you run the build command first.123 """124 typer.echo("Warning: this is a very simple server.")125 typer.echo("For development, use the command live instead.")126 typer.echo("This is here only to preview the documentation site.")127 typer.echo("Make sure you run the build command first.")128 os.chdir("site")129 server_address = ("", 8008)130 server = HTTPServer(server_address, SimpleHTTPRequestHandler)131 typer.echo("Serving at: http://127.0.0.1:8008")132 server.serve_forever()133134135if __name__ == "__main__":136 app()