File size: 21,787 Bytes
ec94fc1 205c317 ec94fc1 | 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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 | """HTML builder β assembles the full ISP Handbook HTML document.
Uses Jinja2 templates for HTML generation. Data preparation logic is
preserved from the original string-concatenation approach. The output
is a self-contained HTML suitable for Playwright Chromium PDF export.
"""
from __future__ import annotations
import base64
import logging
import mimetypes
import os
import re
from pathlib import Path
from typing import Any
from jinja2 import Environment, FileSystemLoader, select_autoescape
from markupsafe import Markup
from app.core.config import get_settings
from app.core.fonts import font_face_css, select_font_family
from app.services.normalizer import normalize_section, normalize_university
from app.services.renderers import (
fetch_image_data_uri,
render_global_blocks,
sort_toc,
)
from app.services.utils import (
format_money_figures,
get_any,
h,
handbook_anchor,
hb_slug,
is_truthy,
sort_sections_stable,
)
logger = logging.getLogger(__name__)
# Jinja2 environment β templates live alongside the app package
_TEMPLATES_DIR = Path(__file__).resolve().parent.parent / "templates"
def _get_jinja_env() -> Environment:
"""Create a Jinja2 environment pointing to our templates directory."""
env = Environment(
loader=FileSystemLoader(str(_TEMPLATES_DIR)),
autoescape=select_autoescape(["html"]),
trim_blocks=True,
lstrip_blocks=True,
)
return env
def _static_base_url() -> str:
"""Return absolute file:// URL to the static directory."""
static_dir = Path(__file__).resolve().parent.parent / "static"
return static_dir.as_uri()
def _unused_pdf_override_css(font_stack: str) -> str:
"""Legacy inline PDF override CSS β kept for reference only.
All styling now lives in static/css/print.css for Chromium rendering.
"""
return ""
# Section class map
SECTION_CLASS_MAP = {
"overview": "sec-overview",
"how_program_works_and_qualification_requirements": "sec-qualification",
"enrolment_steps": "sec-steps",
"withdrawal_late_payment_refund_policy": "sec-policy",
"refund_guidelines": "sec-refund",
"program_contributions": "sec-contributions",
"funding_options_available": "sec-funding",
"summary_of_universities": "sec-summary",
}
PAGE_BREAK_KEYS = {
"overview",
"how_program_works_and_qualification_requirements",
"enrolment_steps",
"withdrawal_late_payment_refund_policy",
"refund_guidelines",
"program_contributions",
"funding_options_available",
"summary_of_universities",
}
def _prepare_university_data(
uni_raw: dict[str, Any],
allow_remote: bool,
include_inactive_programs: bool,
debug: bool,
stats: dict[str, Any],
) -> dict[str, Any]:
"""Prepare a single university's template data.
Extracts overview, campus image, benefits, programs, and extra sections
from the raw sections list. This moves the logic that was in
render_university_section into a data-preparation step so that the
Jinja2 template handles the HTML.
"""
uni_name = uni_raw["name"]
sections = uni_raw.get("sections", [])
is_first = uni_raw.get("_is_first", False)
stats["universities"] = stats.get("universities", 0) + 1
# Build section map; merge duplicate "programs"
sec_map: dict[str, dict] = {}
for s in sections:
if not isinstance(s, dict):
continue
k = str(s.get("section_key", ""))
if not k:
continue
if k == "programs" and k in sec_map:
existing = sec_map["programs"].get("section_json", {})
incoming = s.get("section_json", {})
if not isinstance(existing, dict):
existing = {}
if not isinstance(incoming, dict):
incoming = {}
a = existing.get("programs", [])
b = incoming.get("programs", [])
if not isinstance(a, list):
a = []
if not isinstance(b, list):
b = []
existing["programs"] = a + b
sec_map["programs"]["section_json"] = existing
continue
sec_map[k] = s
# Campus image
img_section = sec_map.get("campus_image") or sec_map.get("image")
campus_image = ""
campus_caption = ""
if img_section:
j = img_section.get("section_json", {})
if isinstance(j, dict):
campus_url = str(j.get("image_url", "")).strip()
campus_caption = str(j.get("caption", "")).strip()
if allow_remote and campus_url:
embedded = fetch_image_data_uri(campus_url)
if embedded:
campus_image = embedded
stats["images_embedded"] = stats.get("images_embedded", 0) + 1
else:
stats["images_placeholder"] = stats.get("images_placeholder", 0) + 1
else:
stats["images_placeholder"] = stats.get("images_placeholder", 0) + 1
# Overview and website
resolved_website = (uni_raw.get("website") or "").strip()
overview_data = None
if "overview" in sec_map:
overview_json = sec_map["overview"].get("section_json", {})
if not isinstance(overview_json, dict):
overview_json = {}
site_from_overview = get_any(
overview_json,
["university_website", "university_website_url", "website", "site", "url", "homepage", "web_url"],
)
if not resolved_website and site_from_overview:
resolved_website = site_from_overview
overview_data = {
"founded": get_any(overview_json, ["founded", "Founded"]),
"total_students": get_any(overview_json, ["total_students", "Total Students"]),
"undergraduates": get_any(overview_json, ["undergraduates", "Undergraduate Students", "undergraduate_students"]),
"postgraduates": get_any(overview_json, ["postgraduate_students", "Postgraduate Students"]),
"acceptance_rate": get_any(overview_json, ["acceptance_rate", "Acceptance Rate"]),
"location": get_any(overview_json, ["location", "Location"]),
"tuition": get_any(overview_json, [
"tuition_out_of_state_yearly",
"Yearly Out of State Tuition Fees",
"Yearly Out-of-State Tuition Fees",
"Yearly Tuition Fees",
"Yearly Out-of-State Tuition Fees:",
]),
}
if resolved_website:
stats["university_links"] = stats.get("university_links", 0) + 1
stats["website_rows"] = stats.get("website_rows", 0) + 1
# Benefits
benefits = None
if "benefits" in sec_map:
j = sec_map["benefits"].get("section_json", {})
if not isinstance(j, dict):
j = {}
raw_benefits = j.get("benefits", [])
if isinstance(raw_benefits, list):
benefits = [str(b).strip() for b in raw_benefits if str(b).strip()]
else:
benefits = []
# Programs
programs = None
if "programs" in sec_map:
j = sec_map["programs"].get("section_json", {})
if not isinstance(j, dict):
j = {}
programs_raw = j.get("programs", [])
if not isinstance(programs_raw, list):
programs_raw = []
if not include_inactive_programs:
programs_raw = [
p for p in programs_raw
if isinstance(p, dict) and is_truthy(
p.get("program_active", p.get("is_active", p.get("active", 1)))
)
]
programs = []
seen_names = set()
for p in programs_raw:
if not isinstance(p, dict):
continue
program_name = str(p.get("program_name", "")).strip()
# Deduplicate by lowercase program name
key = program_name.lower()
if key in seen_names:
continue
seen_names.add(key)
link = str(p.get("program_link", "")).strip()
if not link and isinstance(p.get("program_links"), dict):
link = str(p["program_links"].get("web_link", "")).strip()
# Build career HTML
career = p.get("career_pathways", [])
career_html = ""
if isinstance(career, list):
career_items = [str(x).strip() for x in career if str(x).strip()]
if career_items:
career_html = '<ul class="career-list">'
for ci in career_items:
career_html += f"<li>{h(ci)}</li>"
career_html += "</ul>"
else:
raw = str(career).strip()
if raw:
import re as _re
lines = [l.strip() for l in _re.split(r"[\r\n]+", raw) if l.strip()]
if len(lines) > 1:
career_html = '<ul class="career-list">'
for line in lines:
career_html += f"<li>{h(line)}</li>"
career_html += "</ul>"
else:
career_html = h(raw)
if not career_html:
career_html = " "
programs.append({
"name": program_name,
"link": link,
"designation": str(p.get("designation", "")),
"entrance": str(p.get("entrance_exam", p.get("entrance_examination", ""))),
"career_html": Markup(career_html),
"funding": str(p.get("funding_category", "")),
})
# Extra sections
skip_keys = {"campus_image", "image", "overview", "benefits", "programs"}
extra_sections = []
for s in sections:
if not isinstance(s, dict):
continue
k = str(s.get("section_key", ""))
if not k or k in skip_keys:
continue
title = str(s.get("section_title", ""))
j = s.get("section_json", {})
if not isinstance(j, dict):
j = {}
rendered = render_global_blocks(k, title, j, debug)
extra_sections.append({"rendered_html": Markup(rendered)})
classes = ["uni"]
if not is_first:
classes.append("page-break")
return {
"name": uni_name,
"anchor": uni_raw.get("anchor"),
"sort_order": uni_raw.get("sort_order"),
"website": resolved_website,
"classes": classes,
"overview": overview_data,
"campus_image": campus_image,
"campus_caption": campus_caption,
"benefits": benefits,
"programs": programs,
"extra_sections": extra_sections,
}
def build_handbook_html(
globals_data: list[dict[str, Any]],
by_uni: dict[int, dict[str, Any]],
images: dict[str, Any],
allow_remote: bool,
include_inactive_programs: bool = False,
debug: bool = False,
) -> str:
"""Build the full handbook HTML document using Jinja2 templates.
Preserves the same data preparation logic from the original version.
Rendering is delegated to Jinja2 templates with Playwright-compatible
HTML/CSS output.
"""
env = _get_jinja_env()
template = env.get_template("handbook.html")
font_meta = select_font_family()
font_css = font_face_css(font_meta)
# Base URL for static assets (CSS, images, etc.)
base_url = _static_base_url()
stats: dict[str, Any] = {
"universities": 0,
"images_embedded": 0,
"images_placeholder": 0,
"program_links_total": 0,
"program_missing_links_total": 0,
"missing_program_links": {},
"university_links": 0,
"website_rows": 0,
}
# ββ Cover Image ββ
cover_image = images.get("coverImage", "")
if cover_image and os.path.isfile(cover_image):
cover_image = Path(cover_image).as_uri()
else:
cover_image = ""
# ββ TOC Image ββ
toc_image = images.get("tocImage", "")
if toc_image and os.path.isfile(toc_image):
toc_image = Path(toc_image).as_uri()
else:
toc_image = ""
# ββ Header Image (repeating page header) ββ
header_image = images.get("headerImage", "")
if header_image and os.path.isfile(header_image):
mime = mimetypes.guess_type(header_image)[0] or "image/jpeg"
with open(header_image, "rb") as f:
header_image = f"data:{mime};base64,{base64.b64encode(f.read()).decode()}"
else:
header_image = ""
# ββ Label Image (repeating right-side label) ββ
label_image = images.get("labelImage", "")
if label_image and os.path.isfile(label_image):
mime = mimetypes.guess_type(label_image)[0] or "image/jpeg"
with open(label_image, "rb") as f:
label_image = f"data:{mime};base64,{base64.b64encode(f.read()).decode()}"
else:
# Fallback to remote URL when local file is unavailable
label_image = "https://finsapdev.qhtestingserver.com/MODEL_APIS/handbook/images/label.jpeg"
# ββ Prepare active universities ββ
active_universities: list[dict[str, Any]] = []
for uid, uni in by_uni.items():
if not isinstance(uni, dict):
continue
if not is_truthy(uni.get("is_active", True)):
continue
name = str(uni.get("university_name", f"University #{uid}"))
anchor = handbook_anchor("uni", name, int(uid))
active_universities.append({
"id": int(uid),
"anchor": anchor,
"name": name,
"sections": uni.get("sections", []) if isinstance(uni.get("sections"), list) else [],
"website": str(uni.get("website", "")),
"sort_order": int(uni["sort_order"]) if uni.get("sort_order") is not None and str(uni.get("sort_order", "")).lstrip("-").isdigit() else None,
})
# ββ Normalise globals ββ
globals_data = sort_sections_stable(globals_data)
required_keys = [
"table_of_contents",
"overview",
"how_program_works_and_qualification_requirements",
]
existing_keys = {str(g.get("section_key", "")).lower() for g in globals_data if isinstance(g, dict)}
missing = [k for k in required_keys if k not in existing_keys]
if missing:
msg = f"Handbook required sections missing: {','.join(missing)}"
logger.error(msg)
raise RuntimeError(msg)
general_sections: list[dict[str, Any]] = []
summary_block: dict[str, Any] | None = None
toc_sort_order = None
toc_title = "Table of Contents"
for idx, g in enumerate(globals_data):
if not isinstance(g, dict):
continue
key_raw = str(g.get("section_key", ""))
key = key_raw.lower()
sort_order = int(g["sort_order"]) if g.get("sort_order") is not None and str(g.get("sort_order", "")).lstrip("-").isdigit() else None
if key == "table_of_contents" and toc_sort_order is None:
toc_sort_order = sort_order if sort_order is not None else (idx + 1)
toc_title = str(g.get("section_title", "Table of Contents"))
continue
if key == "summary_of_universities":
summary_block = {
"anchor": handbook_anchor("summary", "summary-of-universities", idx),
"data": g,
"sort_order": sort_order,
}
continue
anchor = handbook_anchor("g", str(g.get("section_title", g.get("section_key", "section"))), idx)
general_sections.append({
"anchor": anchor,
"data": g,
"sort_order": sort_order,
})
# ββ Build TOC items ββ
toc_items: list[dict[str, Any]] = []
for gs in general_sections:
title = str(gs["data"].get("section_title", gs["data"].get("section_key", "Section")))
toc_items.append({
"title": title,
"target": "#" + gs["anchor"],
"level": 0,
"bold": True,
"sort": gs["sort_order"],
})
if summary_block:
title = str(summary_block["data"].get("section_title", "Summary of Universities"))
toc_items.append({
"title": title,
"target": "#" + summary_block["anchor"],
"level": 0,
"bold": True,
"sort": summary_block["sort_order"],
})
for u in active_universities:
toc_items.append({
"title": u["name"],
"target": "#" + u["anchor"],
"level": 1,
"bold": False,
"sort": u.get("sort_order"),
})
# ββ Prepare sorted TOC items for template ββ
sorted_toc = sort_toc(list(toc_items))
toc_items_sorted = []
for e in sorted_toc:
if not isinstance(e, dict):
continue
title = str(e.get("title", "")).strip()
if not title:
continue
level = max(0, min(3, int(e.get("level", 0))))
bold = bool(e.get("bold", False))
upper = bool(e.get("upper", False))
if level == 0:
bold = True
upper = True
display_title = title.upper() if upper else title
page = str(e.get("page", "")).strip()
toc_items_sorted.append({
"title": title,
"display_title": display_title,
"target": str(e.get("target", e.get("anchor", ""))).strip(),
"level": level,
"bold": bold,
"upper": upper,
"page": page,
})
# ββ Prepare general sections with rendered HTML and typed blocks ββ
template_sections = []
for gs in general_sections:
data = gs["data"]
key_lower = str(data.get("section_key", "")).lower()
sec_class = SECTION_CLASS_MAP.get(key_lower)
if sec_class is None:
sec_class = "sec-" + re.sub(r"[^a-z0-9]+", "-", key_lower)
section_json = data.get("section_json", {})
if not isinstance(section_json, dict):
section_json = {}
# Typed blocks for the new rendering path
blocks = normalize_section(
str(data.get("section_key", "")),
str(data.get("section_title", "")),
section_json,
debug=debug,
)
# Legacy HTML fallback
section_html = render_global_blocks(
str(data.get("section_key", "")),
str(data.get("section_title", "")),
section_json,
debug,
)
if not section_html.strip() and not blocks:
logger.warning(
"Empty section render key=%s sort_order=%s",
data.get("section_key"),
data.get("sort_order"),
)
template_sections.append({
"anchor": gs["anchor"],
"data": data,
"page_break": key_lower in PAGE_BREAK_KEYS,
"sec_class": sec_class,
"blocks": blocks,
"rendered_html": Markup(section_html),
})
# ββ Prepare summary block ββ
summary_template = None
if summary_block:
data = summary_block["data"]
section_json = data.get("section_json", {})
if not isinstance(section_json, dict):
section_json = {}
# Typed blocks for summary
summary_blocks = normalize_section(
str(data.get("section_key", "")),
str(data.get("section_title", "")),
section_json,
universities=active_universities,
debug=debug,
)
summary_html = render_global_blocks(
str(data.get("section_key", "")),
str(data.get("section_title", "")),
section_json,
debug,
universities=active_universities,
)
summary_template = {
"anchor": summary_block["anchor"],
"data": data,
"blocks": summary_blocks,
"rendered_html": Markup(summary_html),
}
# ββ Prepare university data for templates (both old + new paths) ββ
university_template_data = []
university_block_data = []
for idx, uni_raw in enumerate(active_universities):
uni_raw["_is_first"] = (idx == 0)
# Legacy path
uni_data = _prepare_university_data(
uni_raw, allow_remote, include_inactive_programs, debug, stats,
)
university_template_data.append(uni_data)
# New block path
uni_block = normalize_university(
uni_raw, allow_remote, include_inactive_programs, debug, stats,
)
university_block_data.append(uni_block)
# ββ Bottom pages ββ
bottom_pages_urls = []
raw_bottom = images.get("bottomPages", [])
if isinstance(raw_bottom, list):
for img_path in raw_bottom:
if os.path.isfile(str(img_path)):
bottom_pages_urls.append(Path(str(img_path)).as_uri())
# ββ Render template ββ
html = template.render(
font_css=Markup(font_css),
base_url=base_url,
extra_css="",
header_image=header_image,
label_image=label_image,
cover_image=cover_image,
toc_image=toc_image,
toc_items=toc_items,
toc_items_sorted=toc_items_sorted,
toc_title=toc_title,
toc_sort_order=toc_sort_order,
general_sections=template_sections,
summary_block=summary_template,
universities=university_template_data,
university_blocks=university_block_data,
bottom_pages=bottom_pages_urls,
debug=debug,
stats=stats,
)
return html
|