refactor: unify three-exchange instance UI with shared templates
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,33 +1,63 @@
|
||||
"""Build embed_page_fragment.html from gate index.html."""
|
||||
"""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_lines = (ROOT / "crypto_monitor_gate" / "templates" / "index.html").read_text(
|
||||
encoding="utf-8"
|
||||
).splitlines()
|
||||
SRC = ROOT / "lib" / "instance" / "templates" / "index.html"
|
||||
OUT = ROOT / "lib" / "instance" / "templates" / "embed_page_fragment.html"
|
||||
|
||||
# 1-based line numbers from index.html
|
||||
macro_body = src_lines[243:262] # {% macro %} … {% endmacro %}
|
||||
grid_inner = src_lines[328:736] # inside .grid (exclude outer wrapper)
|
||||
stats_block = src_lines[738:772]
|
||||
GRID_START = ' <div class="grid">'
|
||||
STATS_START = ' <div class="card full stats-card'
|
||||
|
||||
out_lines = [
|
||||
"{# Hub iframe tab fragment — shared via embed_templates #}",
|
||||
*macro_body,
|
||||
'<div class="grid">',
|
||||
*grid_inner,
|
||||
"</div>",
|
||||
*stats_block,
|
||||
]
|
||||
|
||||
out_dir = ROOT / "lib" / "instance" / "templates"
|
||||
out_dir.mkdir(exist_ok=True)
|
||||
text = "\n".join(out_lines) + "\n"
|
||||
text = text.replace(
|
||||
"{% include 'order_monitor_rule_tips_gate.html' %}",
|
||||
"{% include order_rule_tips_tpl %}",
|
||||
)
|
||||
(out_dir / "embed_page_fragment.html").write_text(text, encoding="utf-8")
|
||||
print("wrote", out_dir / "embed_page_fragment.html", "lines", len(out_lines))
|
||||
def _slice_between(lines: list[str], start: str, end: str | None) -> list[str]:
|
||||
try:
|
||||
i = next(idx for idx, line in enumerate(lines) if line == start)
|
||||
except StopIteration:
|
||||
raise SystemExit(f"marker not found: {start!r}")
|
||||
if end is None:
|
||||
return lines[i:]
|
||||
try:
|
||||
j = next(idx for idx, line in enumerate(lines[i + 1 :], i + 1) if line.startswith(end))
|
||||
except StopIteration:
|
||||
raise SystemExit(f"end marker not found: {end!r}")
|
||||
return lines[i:j]
|
||||
|
||||
|
||||
def main() -> 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() == "</div>":
|
||||
# only remove closing div if it closes .grid (heuristic: last line before stats)
|
||||
pass
|
||||
|
||||
stats_block = _slice_between(lines, STATS_START, " </div>")
|
||||
|
||||
out_lines = [
|
||||
"{# Hub iframe tab fragment — shared via embed_templates #}",
|
||||
*macro_body,
|
||||
'<div class="grid">',
|
||||
*grid_block,
|
||||
"</div>",
|
||||
*stats_block,
|
||||
]
|
||||
text = "\n".join(out_lines).rstrip() + "\n"
|
||||
if "order_rule_tips_tpl" not in text:
|
||||
text = text.replace(
|
||||
"{% include 'order_monitor_rule_tips_binance.html' %}",
|
||||
"{% include order_rule_tips_tpl %}",
|
||||
)
|
||||
OUT.write_text(text, encoding="utf-8")
|
||||
print("wrote", OUT, "lines", len(out_lines))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user