File size: 3,447 Bytes
598559d
 
 
 
 
 
e0b5e77
598559d
cb33bba
598559d
cb33bba
 
 
 
598559d
 
cb33bba
 
 
 
 
598559d
 
cb33bba
598559d
cb33bba
598559d
 
cb33bba
598559d
 
cb33bba
598559d
cb33bba
598559d
 
cb33bba
598559d
 
 
 
cb33bba
 
 
 
598559d
 
 
 
 
 
 
 
cb33bba
 
 
598559d
 
cb33bba
 
 
 
 
 
 
 
 
 
 
598559d
 
cb33bba
 
 
 
 
 
 
598559d
cb33bba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
598559d
 
 
cb33bba
 
598559d
 
 
 
 
cb33bba
 
 
598559d
 
 
cb33bba
 
 
 
 
598559d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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