doc-compiler-j2-pdf / app /renderer.py
JairoDanielMT's picture
Update app/renderer.py
cb33bba verified
from __future__ import annotations
from datetime import datetime
from typing import Any, Dict
from jinja2 import Environment, FileSystemLoader, select_autoescape
from weasyprint import HTML
from .config import BASE_DIR
# =========================
# RUTAS
# =========================
TEMPLATES_DIR = BASE_DIR / "templates"
# =========================
# MAPA PRIMARIO DE TEMPLATES
# (coincidencias exactas)
# =========================
TEMPLATE_MAP: Dict[str, str] = {
"RESTlet": "RESTlet.html.j2",
"ClientScript": "ClientScript.html.j2",
"Map/Reduce": "MapReduce.html.j2",
"MapReduce": "MapReduce.html.j2",
"Scheduled": "ScheduledScript.html.j2",
"ScheduledScript": "ScheduledScript.html.j2",
"Suitelet": "Suitelet.html.j2",
"UserEvent": "UserEventScript.html.j2",
"UserEventScript": "UserEventScript.html.j2",
"UserModule.js": "InternalModule.html.j2",
"InternalModule": "InternalModule.html.j2",
}
# =========================
# ENTORNO DE TEMPLATES
# =========================
env = Environment(
loader=FileSystemLoader(str(TEMPLATES_DIR)),
autoescape=select_autoescape(["html", "xml"]),
trim_blocks=True,
lstrip_blocks=True,
)
# =========================
# RESOLVER DE TEMPLATES
# =========================
def _resolve_template(doc: Dict[str, Any]) -> str:
metadata = doc.get("metadata") or {}
raw_type = metadata.get("script_type") or metadata.get("type") or "InternalModule"
# ---- Normalización del tipo ----
normalized = (
str(raw_type)
.strip()
.lower()
.replace(" ", "")
.replace("-", "")
.replace("_", "")
.replace("/", "")
)
# ---- Mapa normalizado ----
normalized_map = {
"restlet": "RESTlet.html.j2",
"clientscript": "ClientScript.html.j2",
"clients": "ClientScript.html.j2",
"cs": "ClientScript.html.j2",
"mapreduce": "MapReduce.html.j2",
"mr": "MapReduce.html.j2",
"scheduledscript": "ScheduledScript.html.j2",
"scheduled": "ScheduledScript.html.j2",
"ss": "ScheduledScript.html.j2",
"suitelet": "Suitelet.html.j2",
"sl": "Suitelet.html.j2",
"userevent": "UserEventScript.html.j2",
"usereventscript": "UserEventScript.html.j2",
"ue": "UserEventScript.html.j2",
"internalmodule": "InternalModule.html.j2",
"module": "InternalModule.html.j2",
"usermodulejs": "InternalModule.html.j2",
}
# ---- Primera prueba: coincidencia exacta ----
if raw_type in TEMPLATE_MAP:
return TEMPLATE_MAP[raw_type]
# ---- Segunda prueba: coincidencia normalizada ----
if normalized in normalized_map:
return normalized_map[normalized]
# ---- Fallback ----
return "InternalModule.html.j2"
# =========================
# RENDER HTML
# =========================
def render_html(doc: Dict[str, Any]) -> str:
template_name = _resolve_template(doc)
template = env.get_template(template_name)
return template.render(
doc=doc,
generated_at=datetime.utcnow().strftime("%Y-%m-%d %H:%M UTC"),
)
# =========================
# RENDER PDF
# =========================
def render_pdf_bytes(doc: Dict[str, Any]) -> bytes:
html = render_html(doc)
pdf_bytes: bytes = HTML(
string=html,
base_url=str(TEMPLATES_DIR)
).write_pdf()
return pdf_bytes