29 lines
1.6 KiB
Python
Executable File
29 lines
1.6 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Print index.html to loop-claude-explained.pdf beside it, in the playwright-for-claude image."""
|
|
import pathlib, shutil, subprocess, sys, tempfile
|
|
|
|
HERE = pathlib.Path(__file__).resolve().parent
|
|
IMAGE = "playwright-for-claude"
|
|
# index.html is an artifact body, without a doctype, head or body of its own, so wrap it as the artifact host does.
|
|
HEAD = '<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover"><style>:root{color-scheme:light}body{margin:0}img{max-width:100%}[hidden]{display:none!important}</style></head><body>\n'
|
|
|
|
|
|
def main() -> int:
|
|
if subprocess.run(["docker", "image", "inspect", IMAGE], capture_output=True).returncode != 0:
|
|
print(f"No {IMAGE} image. Build it once with: ~/.claude/bin/run-heavy docker build -t {IMAGE} ~/code/playwright-for-agents", file=sys.stderr)
|
|
return 1
|
|
with tempfile.TemporaryDirectory() as folder:
|
|
work = pathlib.Path(folder)
|
|
(work / "page.html").write_text(HEAD + (HERE / "index.html").read_text(encoding="utf-8") + "</body></html>\n", encoding="utf-8")
|
|
shutil.copy(HERE / "render-pdf.js", work)
|
|
printed = subprocess.run(["docker", "run", "--rm", "--init", "--memory", "2g", "--memory-swap", "2g", "-v", f"{work}:/work", "-w", "/work", "--entrypoint", "node", IMAGE, "render-pdf.js"])
|
|
if printed.returncode != 0:
|
|
return printed.returncode
|
|
shutil.copy(work / "out.pdf", HERE / "loop-claude-explained.pdf")
|
|
print(f"Wrote {HERE / 'loop-claude-explained.pdf'}")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|