|
|
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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TEMPLATES_DIR = BASE_DIR / "templates" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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", |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
env = Environment( |
|
|
loader=FileSystemLoader(str(TEMPLATES_DIR)), |
|
|
autoescape=select_autoescape(["html", "xml"]), |
|
|
trim_blocks=True, |
|
|
lstrip_blocks=True, |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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" |
|
|
|
|
|
|
|
|
normalized = ( |
|
|
str(raw_type) |
|
|
.strip() |
|
|
.lower() |
|
|
.replace(" ", "") |
|
|
.replace("-", "") |
|
|
.replace("_", "") |
|
|
.replace("/", "") |
|
|
) |
|
|
|
|
|
|
|
|
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", |
|
|
} |
|
|
|
|
|
|
|
|
if raw_type in TEMPLATE_MAP: |
|
|
return TEMPLATE_MAP[raw_type] |
|
|
|
|
|
|
|
|
if normalized in normalized_map: |
|
|
return normalized_map[normalized] |
|
|
|
|
|
|
|
|
return "InternalModule.html.j2" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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"), |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
|
|
|