"""Build embed_page_fragment.html from lib/instance/templates/index.html."""
from __future__ import annotations
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
SRC = ROOT / "lib" / "instance" / "templates" / "index.html"
OUT = ROOT / "lib" / "instance" / "templates" / "embed_page_fragment.html"
GRID_START = '
'
STATS_START = '
None:
lines = SRC.read_text(encoding="utf-8").splitlines()
macro_start = next(i for i, l in enumerate(lines) if l.startswith("{% macro period_stats"))
macro_end = next(i for i, l in enumerate(lines) if l.strip() == "{% endmacro %}")
macro_body = lines[macro_start : macro_end + 1]
grid_block = _slice_between(lines, GRID_START, STATS_START)
# strip outer .grid wrapper; fragment adds its own
if grid_block and grid_block[0] == GRID_START:
grid_block = grid_block[1:]
if grid_block and grid_block[-1].strip() == "
":
# only remove closing div if it closes .grid (heuristic: last line before stats)
pass
stats_block = _slice_between(lines, STATS_START, "
")
out_lines = [
"{# Hub iframe tab fragment — shared via embed_templates #}",
*macro_body,
'