"""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")
# ── Normalize line endings to LF to avoid CRLF mismatch on Windows ──
content = content.replace("\r\n", "\n")
# ── Correct themeBaseUrl and mediaPath in editor.html ──
content = content.replace("Vvveb.themeBaseUrl = 'demo/landing/';", "Vvveb.themeBaseUrl = '/static/Vvvebjs/demo/landing/';")
content = content.replace("window.mediaPath = '../../media';", "window.mediaPath = '/static/Vvvebjs/media/';")
# ── 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)
# ── Make editor load requested page from query parameter ──
old_init = 'let firstPage = Object.keys(pages)[0];\nVvveb.Builder.init(pages[firstPage]["url"], function () {'
new_init = """let firstPage = Object.keys(pages)[0];
const urlParams = new URLSearchParams(window.location.search);
const requestedPage = urlParams.get('page');
if (requestedPage) {
\tlet pageKey = requestedPage.replace(".html", "");
\tif (pages[pageKey]) {
\t\tfirstPage = pageKey;
\t}
}
Vvveb.Builder.init(pages[firstPage]["url"], function () {"""
content = content.replace(old_init, new_init)
# ── 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