mirror of
https://github.com/PR0M3TH3AN/VoxVera.git
synced 2026-10-27 09:40:40 +00:00
74 lines
2.2 KiB
Python
74 lines
2.2 KiB
Python
"""Pure-Python replacements for the shell-based build pipeline.
|
|
|
|
Eliminates the need for jq, qrencode, ImageMagick, javascript-obfuscator,
|
|
html-minifier-terser, node, pdftotext, and poppler-utils.
|
|
"""
|
|
|
|
import json
|
|
import re
|
|
from pathlib import Path
|
|
|
|
import htmlmin # provided by htmlmin2 package
|
|
import qrcode
|
|
from jsmin import jsmin
|
|
from PIL import Image
|
|
|
|
|
|
def generate_qr(config_path: Path, output_dir: Path) -> None:
|
|
"""Generate QR code PNGs from config URLs.
|
|
|
|
Replaces generate_qr.sh (qrencode + ImageMagick convert).
|
|
URL is auto-generated by Tor during serve(), so it may be empty during initial build.
|
|
"""
|
|
with open(config_path) as f:
|
|
config = json.load(f)
|
|
|
|
url = config.get("url", "")
|
|
tear = config.get("tear_off_link", "")
|
|
|
|
# Skip QR generation if no URLs configured yet (will be generated after serve)
|
|
if not url and not tear:
|
|
from voxvera.cli import t
|
|
print(t("cli.no_urls_qr_skip"))
|
|
return
|
|
|
|
# Generate QR codes for available URLs
|
|
qr_pairs = []
|
|
if url:
|
|
qr_pairs.append((url, "qrcode-content.png"))
|
|
if tear:
|
|
qr_pairs.append((tear, "qrcode-tear-offs.png"))
|
|
|
|
for data, filename in qr_pairs:
|
|
qr = qrcode.QRCode(box_size=10, border=0)
|
|
qr.add_data(data)
|
|
qr.make(fit=True)
|
|
img = qr.make_image(fill_color="black", back_color="white")
|
|
img = img.resize((128, 128), Image.LANCZOS)
|
|
img.save(output_dir / filename)
|
|
|
|
|
|
def obfuscate_html(input_file: Path, output_file: Path) -> None:
|
|
"""Minify JS inside <script> tags and minify the overall HTML.
|
|
|
|
Replaces obfuscate_index.sh (javascript-obfuscator + html-minifier-terser + node).
|
|
"""
|
|
if not input_file.exists():
|
|
from voxvera.cli import t
|
|
raise FileNotFoundError(t("cli.input_file_not_found", path=str(input_file)))
|
|
|
|
html = input_file.read_text(encoding="utf-8")
|
|
|
|
def _minify_script(match):
|
|
js = match.group(1)
|
|
return f"<script>{jsmin(js)}</script>"
|
|
|
|
html = re.sub(r"<script>(.*?)</script>", _minify_script, html, flags=re.DOTALL)
|
|
html = htmlmin.minify(
|
|
html,
|
|
remove_comments=True,
|
|
remove_empty_space=False,
|
|
remove_optional_attribute_quotes=False,
|
|
)
|
|
output_file.write_text(html, encoding="utf-8")
|