61 lines
2.1 KiB
Python
61 lines
2.1 KiB
Python
import os
|
|
import hashlib
|
|
from .security import get_safe_path, PROJECTS_DIR
|
|
from .state import load_staging, load_ledger, save_ledger
|
|
import sys
|
|
|
|
def compile_page(project_name, page_id):
|
|
staging = load_staging(project_name)
|
|
ledger = load_ledger(project_name)
|
|
|
|
task = next((t for t in ledger.get("task_queue", []) if t["page_id"] == page_id), None)
|
|
if not task:
|
|
return {"status": "error", "reason": f"Task '{page_id}' not found in task_queue."}
|
|
|
|
# 1. Source File Reader
|
|
src_file = get_safe_path(PROJECTS_DIR, project_name, "src", task["filename"])
|
|
if not os.path.exists(src_file):
|
|
err_msg = f"Source file '{task['filename']}' not found in src directory."
|
|
print(f"[Compiler] Error: {err_msg}", file=sys.stderr)
|
|
return {"status": "error", "reason": err_msg}
|
|
|
|
with open(src_file, "r", encoding="utf-8") as bf:
|
|
content_html = bf.read()
|
|
|
|
# 2. Assemble Document
|
|
styles = staging.get("style_tokens", {})
|
|
full_document = f"""<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<title>{task.get('title', 'Project Component')}</title>
|
|
<style>
|
|
body {{
|
|
background-color: {styles.get('background_color', '#FFFFFF')};
|
|
color: {styles.get('text_color', '#000000')};
|
|
font-family: {styles.get('font_family', 'sans-serif')};
|
|
}}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
{content_html}
|
|
</body>
|
|
</html>"""
|
|
|
|
# 3. Write & Hash
|
|
dest_dir = get_safe_path(PROJECTS_DIR, project_name, "dist")
|
|
os.makedirs(dest_dir, exist_ok=True) # Create nested build directories inside the module workspace safely
|
|
|
|
dest_file = get_safe_path(PROJECTS_DIR, project_name, "dist", task["filename"])
|
|
os.makedirs(os.path.dirname(dest_file), exist_ok=True)
|
|
with open(dest_file, "w", encoding="utf-8") as f:
|
|
f.write(full_document)
|
|
|
|
file_hash = hashlib.sha256(full_document.encode('utf-8')).hexdigest()
|
|
|
|
# 4. Update Ledger
|
|
hashes = ledger.get("hashes", {})
|
|
hashes[page_id] = file_hash
|
|
ledger["hashes"] = hashes
|
|
save_ledger(project_name, ledger)
|
|
|
|
return {"status": "success", "message": f"Compiled and hashed '{page_id}'", "hash": file_hash} |