#!/usr/bin/env python3 """Nhúng mọi ảnh cục bộ của slide vào thẳng file HTML dưới dạng data URI. Đọc agenthub-introduction.html, thay từng src="*.png" bằng chuỗi base64, ghi ra agenthub-introduction-standalone.html - một file duy nhất, gửi cho ai cũng thấy ảnh. Chạy lại sau mỗi lần sửa slide. @author Nguyễn Ngọc Trí Vĩ """ import base64 import mimetypes import pathlib import re import sys import urllib.parse SRC = pathlib.Path(__file__).parent / "agenthub-introduction.html" OUT = SRC.with_name("agenthub-introduction-standalone.html") def inline(match): raw = match.group(1) if raw.startswith(("data:", "http://", "https://")): return match.group(0) path = SRC.parent / urllib.parse.unquote(raw) if not path.is_file(): sys.exit(f"thiếu ảnh: {path}") mime = mimetypes.guess_type(path.name)[0] or "application/octet-stream" b64 = base64.b64encode(path.read_bytes()).decode() return f'src="data:{mime};base64,{b64}"' html = SRC.read_text(encoding="utf-8") OUT.write_text(re.sub(r'src="([^"]+)"', inline, html), encoding="utf-8") print(f"{OUT.name} - {OUT.stat().st_size / 1024 / 1024:.1f} MB")