90 lines
3.7 KiB
Python
90 lines
3.7 KiB
Python
section"""
|
||
lines = [
|
||
"| 元素选择器 | 页面Top(px) | 高度(px) | 高度(rpx) | paddingT | paddingB | marginT | marginB | gap | fontSize | lineHeight |",
|
||
"|---|---|---|---|---|---|---|---|---|---|---|",
|
||
]
|
||
for it in items:
|
||
h_rpx = px_to_rpx(it['height'])
|
||
lh = px_val(it['lineHeight']) if it['lineHeight'] not in ('normal', '') else 0
|
||
lines.append(
|
||
f"| {it['selector']}[{it['index']}] "
|
||
f"| {it['pageTop']:.1f} | {it['height']:.1f} | {h_rpx} "
|
||
f"| {it['paddingTop']} | {it['paddingBottom']} "
|
||
f"| {it['marginTop']} | {it['marginBottom']} "
|
||
f"| {it.get('gap','0px')} | {it['fontSize']} | {it['lineHeight']} |"
|
||
)
|
||
return "\n".join(lines)
|
||
|
||
|
||
def main():
|
||
if sys.platform == 'win32':
|
||
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace')
|
||
parser = argparse.ArgumentParser(description='测量 H5 页面元素间距,输出 px 和 rpx')
|
||
parser.add_argument('page', help='页面名,如 task-list')
|
||
parser.add_argument('--selectors', nargs='+', default=[], help='CSS 选择器列表')
|
||
parser.add_argument('--scroll', type=int, default=0, help='scrollTop 位置(默认 0)')
|
||
parser.add_argument('--pairs', nargs=2, metavar=('A', 'B'), help='计算两个选择器间的间距')
|
||
parser.add_argument('--out', help='输出 JSON 路径(默认自动命名)')
|
||
args = parser.parse_args()
|
||
|
||
if not args.selectors and not args.pairs:
|
||
print('请指定 --selectors 或 --pairs')
|
||
parser.print_help()
|
||
sys.exit(1)
|
||
|
||
selectors = args.selectors or list(args.pairs)
|
||
results = asyncio.run(measure_page(args.page, selectors, args.scroll, args.pairs))
|
||
|
||
# save JSON
|
||
out_path = Path(args.out) if args.out else OUT_DIR / f"{args.page}-gaps.json"
|
||
with open(out_path, 'w', encoding='utf-8') as f:
|
||
json.dump(results, f, ensure_ascii=False, indent=2)
|
||
|
||
# print table
|
||
items = results.get('items', [])
|
||
if items:
|
||
print(f'\n页面: {args.page} scrollTop={args.scroll} 元素数: {len(items)}')
|
||
print_table(items)
|
||
|
||
# spacing props summary
|
||
print('\n关键间距汇总(px → rpx):')
|
||
for it in items:
|
||
pt = px_val(it['paddingTop'])
|
||
pb = px_val(it['paddingBottom'])
|
||
mt = px_val(it['marginTop'])
|
||
mb = px_val(it['marginBottom'])
|
||
g = px_val(it.get('gap', '0px'))
|
||
fs = px_val(it['fontSize'])
|
||
lh_raw = it['lineHeight']
|
||
lh = px_val(lh_raw) if lh_raw not in ('normal', '') else 0
|
||
parts = []
|
||
if pt: parts.append(f'paddingTop={pt:.1f}px→{px_to_rpx(pt)}rpx')
|
||
if pb: parts.append(f'paddingBot={pb:.1f}px→{px_to_rpx(pb)}rpx')
|
||
if mt: parts.append(f'marginTop={mt:.1f}px→{px_to_rpx(mt)}rpx')
|
||
if mb: parts.append(f'marginBot={mb:.1f}px→{px_to_rpx(mb)}rpx')
|
||
if g: parts.append(f'gap={g:.1f}px→{px_to_rpx(g)}rpx')
|
||
if fs: parts.append(f'fontSize={fs:.1f}px→{px_to_rpx(fs)}rpx')
|
||
if lh: parts.append(f'lineHeight={lh:.1f}px→{px_to_rpx(lh)}rpx')
|
||
if parts:
|
||
print(f' {it["selector"]}[{it["index"]}]: {" ".join(parts)}')
|
||
|
||
# consecutive gaps
|
||
gaps = results.get('consecutive_gaps', [])
|
||
if gaps:
|
||
print('\n\u76f8邻元素垂直间距:')
|
||
print_gaps(gaps)
|
||
|
||
# pair gap
|
||
pg = results.get('pair_gap')
|
||
if pg:
|
||
print(f'\n指定对间距: {pg["a"]} → {pg["b"]}: {pg["gap_px"]:.1f}px = {pg["gap_rpx"]}rpx')
|
||
|
||
# markdown table
|
||
print('\n--- audit.md 间距表格 ---')
|
||
print(spacing_audit_table(items))
|
||
print(f'\n详细数据已保存: {out_path}')
|
||
|
||
|
||
if __name__ == '__main__':
|
||
main()
|