#!/usr/bin/env python3 """把设计稿画板拆成每屏一个独立 HTML,便于 headless chrome 逐屏截图。""" import re, os, sys from html.parser import HTMLParser # python3 docs/design/render-screens.py && \ # for f in /tmp/tx-screens/*.html; do "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" \ # --headless=new --window-size=1366,1024 --screenshot="${f%.html}.png" "file://$f"; done SRC = os.path.join(os.path.dirname(os.path.abspath(__file__)), "terminalX-iPadOS.dc.html") OUT = "/tmp/tx-screens" os.makedirs(OUT, exist_ok=True) html = open(SRC, encoding="utf-8").read() # 提取 (helmet 内的全局样式) style = "\n".join(re.findall(r"", html, re.S)) VOID = {"br", "img", "hr", "input", "meta", "link", "source", "area", "base", "col", "embed", "param", "track", "wbr"} class Finder(HTMLParser): def __init__(self): super().__init__(convert_charrefs=False) self.stack = [] # 当前打开的 tag 名 self.capture = None # (label, depth, start_offset) self.results = [] # (label, start, end) def cur_off(self): line, col = self.getpos() return self.line_starts[line - 1] + col def handle_starttag(self, tag, attrs): if tag in VOID: return d = dict(attrs) if self.capture is None and "data-screen-label" in d: self.capture = (d["data-screen-label"], len(self.stack), self.cur_off()) self.stack.append(tag) def handle_startendtag(self, tag, attrs): pass def handle_endtag(self, tag): if tag in VOID: return while self.stack and self.stack[-1] != tag: self.stack.pop() if self.stack: self.stack.pop() if self.capture and len(self.stack) == self.capture[1]: label, _, start = self.capture end = self.cur_off() + len(f"") self.results.append((label, start, end)) self.capture = None f = Finder() f.line_starts = [0] for line in html.splitlines(keepends=True): f.line_starts.append(f.line_starts[-1] + len(line)) f.feed(html) HEAD = """ """ manifest = [] for i, (label, s, e) in enumerate(f.results): frag = html[s:e] m = re.search(r"width:(\d+)px;height:(\d+)px", frag) w, h = (m.group(1), m.group(2)) if m else ("1366", "1024") slug = re.sub(r"[^\w一-鿿]+", "_", label).strip("_") path = f"{OUT}/{i:02d}_{slug}.html" open(path, "w", encoding="utf-8").write(HEAD % style + frag + "") manifest.append((path, w, h, label)) print(f"{i:02d}\t{label}\t{w}x{h}\t{path}") with open(f"{OUT}/manifest.tsv", "w") as fh: for p, w, h, l in manifest: fh.write(f"{p}\t{w}\t{h}\t{l}\n") print(f"total {len(manifest)}")