#!/usr/bin/env python3
"""Write embed.html: the hub's attempt tree alone, for the iframe on Proposition IX (satake-two-levels/props/converse.html).

Run after build_tree.py:  python3 scripts/R/make_embed.py   (from the run folder, or anywhere)

The stage markup is copied from index.html, so the embedded tree is the hub's tree, drawn by the same
assets/tree.js from the same assets/data.js. <base target="_top"> opens a clicked step in the whole window.
"""
import re
from pathlib import Path

RUN = Path(__file__).resolve().parents[2]
hub = (RUN / 'index.html').read_text()
start = hub.index('<div id="stage"')
end = hub.index('</section>', hub.index('<div class="legend-wrap">'))
stage = hub[start:end]
page = f"""<!doctype html>
<html lang="en"><head>
<meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>Attempt tree · Proposition IX</title>
<base target="_top">
<link rel="stylesheet" href="assets/katex/katex.min.css"><link rel="stylesheet" href="assets/style.css">
<style>body{{margin:0;padding:8px 10px;background:var(--bg,#fff)}} .stage{{width:auto;max-width:none;margin:0;transform:none}} .legend-wrap{{margin-top:.6rem}}</style>
<script defer src="assets/katex/katex.min.js"></script><script defer src="assets/katex/auto-render.min.js"></script>
<script defer src="assets/data.js"></script><script defer src="assets/tree.js"></script>
</head><body class="embed">
{stage}
</body></html>
"""
(RUN / 'embed.html').write_text(page)
print(f'embed.html: {len(page)} bytes')
