"""Build editor.html template from vvvebjs source with Jinja2 escaping.""" from pathlib import Path BASE = Path(__file__).parent SRC = BASE / "static" / "Vvvebjs" / "editor.html" OUT = BASE / "templates" / "editor.html" content = SRC.read_text(encoding="utf-8") # ── 1. Fix static asset paths ──────────────────────────────────── VBASE = "/static/Vvvebjs/" replacements = [ ('href="css/', f'href="{VBASE}css/'), ('href="libs/', f'href="{VBASE}libs/'), ('href="img/', f'href="{VBASE}img/'), ('href="favicon.ico"', f'href="{VBASE}favicon.ico"'), ('src="libs/', f'src="{VBASE}libs/'), ('src="js/', f'src="{VBASE}js/'), ('src="img/', f'src="{VBASE}img/'), ('src="media/', f'src="{VBASE}media/'), ('src="fonts/', f'src="{VBASE}fonts/'), ('src="demo/', f'src="{VBASE}demo/'), ] for old, new in replacements: content = content.replace(old, new) # ── 2. Fix PHP save URL → Flask API ───────────────────────────── content = content.replace('data-vvveb-url="save.php"', 'data-vvveb-url="/api/save"') # ── 3. Fix JS url variables ────────────────────────────────────── JS_VARS = [ ("let renameUrl = 'save.php?action=rename';", "let renameUrl = '/api/save?action=rename';"), ("let deleteUrl = 'save.php?action=delete';", "let deleteUrl = '/api/save?action=delete';"), ("let saveReusableUrl = 'save.php?action=saveReusable';", "let saveReusableUrl = '/api/save?action=saveReusable';"), ("let oEmbedProxyUrl = 'save.php?action=oembedProxy';", "let oEmbedProxyUrl = '/api/save?action=oembedProxy';"), ] for old, new in JS_VARS: content = content.replace(old, new) # ── 4. Wrap everything in {% raw %} ... {% endraw %} to avoid Jinja2 parsing conflicts ── # We do this first so Jinja2 ignores VvvebJS's frontend micro-templates ({%= %}, {% %}). # Then we selectively exit the {% raw %} block using {% endraw %} ... {% raw %} for our dynamic values. content = "{% raw %}" + content + "{% endraw %}" # ── 5. Replace defaultPages block with Escaped Jinja2 Injection ── # Since we are inside a {% raw %} block, we use {% endraw %}{{ pages_json | safe }}{% raw %} start = content.find("let defaultPages = {") end = content.find("};\n\n\nlet pages = defaultPages;") if start != -1 and end != -1: end += len("};\n\n\nlet pages = defaultPages;") content = ( content[:start] + "let defaultPages = {% endraw %}{{ pages_json | safe }}{% raw %};\nlet pages = defaultPages;" + content[end:] ) # ── 6. Add back button + slug variable before ─────────── # We break out of {% raw %} using {% endraw %} to interpolate {{ slug | safe }} BACK_BTN = """ ← 返回管理器 """ content = content.replace("", BACK_BTN + "\n") OUT.write_text(content, encoding="utf-8") print(f"Successfully generated escaped Jinja2 template! Size: {len(content)} bytes.")