Repository Public MIT
1/**2 * HTML of the PDF research note, light and sober: a white cover carrying the sparkle mark, a contents page,3 * then the analysis set as a research document (numbered sections, financial tables, captioned figures,4 * callouts) with a running header and page numbers. Rendered by Chromium (see pdfService.ts); page furniture5 * uses CSS @page margin boxes. Inter is embedded from the server's own font files, so the layout never6 * depends on a font CDN.7 */8import fs from "fs";9import path from "path";10import { fileURLToPath } from "url";11import { sparkleSvg } from "@shared/vqLogo";1213const __dirname = path.dirname(fileURLToPath(import.meta.url));1415let fontCss: string | null = null;16function fonts(): string {17 if (fontCss !== null) return fontCss;18 fontCss = "";19 const dirs = [path.join(__dirname, "fonts"), path.join(__dirname, "python", "fonts"), path.join(process.cwd(), "server", "services", "python", "fonts"), path.join(process.cwd(), "dist", "fonts")];20 const dir = dirs.find((d) => fs.existsSync(path.join(d, "inter-400.ttf")));21 if (!dir) return fontCss;22 const face = (family: string, weight: number, file: string) => {23 const full = path.join(dir, file);24 if (fs.existsSync(full)) fontCss += `@font-face{font-family:"${family}";font-weight:${weight};font-style:normal;src:url(data:font/ttf;base64,${fs.readFileSync(full).toString("base64")}) format("truetype");}\n`;25 };26 for (const w of [400, 500, 600, 700]) face("Inter", w, `inter-${w}.ttf`);27 face("JetBrains Mono", 500, "jetbrains-mono-500.ttf");28 return fontCss;29}3031export interface ReportInput {32 /** analysis body already converted from Markdown to HTML */33 bodyHtml: string;34 title: string;35 /** the user's question, shown on the cover when it differs from the title */36 question: string;37 /** e.g. "15 novembre 2026" */38 dateLabel: string;39}4041const esc = (s: string) => s.replace(/[&<>"']/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" })[c]!);42/** For the CSS string of the running header. */43const cssString = (s: string) => s.replace(/\\/g, "\\\\").replace(/"/g, '\\"').replace(/[\r\n]+/g, " ");4445export function buildReportHtml(input: ReportInput): string {46 const { bodyHtml, title, question, dateLabel } = input;47 const running = title.length > 70 ? title.slice(0, 69).trimEnd() + "…" : title;48 const showQuestion = question.trim() && question.trim().toLowerCase() !== title.trim().toLowerCase();49 const titleSize = title.length > 90 ? 27 : title.length > 55 ? 32 : 38;5051 return `<!DOCTYPE html>52<html lang="fr">53<head>54<meta charset="UTF-8">55<title>${esc(title)}</title>56<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/katex.min.css">57<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/katex.min.js"></script>58<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/contrib/auto-render.min.js"59 onload="renderMathInElement(document.body, {delimiters:[{left:'\\\\[',right:'\\\\]',display:true},{left:'\\\\(',right:'\\\\)',display:false}],throwOnError:false});"></script>60<style>61${fonts()}62:root {63 --ink: #14171f; --body: #30343f; --muted: #6a7080; --faint: #9a9fac;64 --line: #e3e5ea; --hair: #eef0f3; --wash: #f7f8fa; --paper: #ffffff;65 --accent: #d9500f; --accent-soft: #fbeee6; --up: #16794c; --down: #c0392b;66}67* { box-sizing: border-box; }68html { -webkit-print-color-adjust: exact; print-color-adjust: exact; }69body { margin: 0; font: 400 10.1pt/1.64 Inter, -apple-system, "Helvetica Neue", Arial, sans-serif; color: var(--body); background: var(--paper); font-feature-settings: "cv11", "ss01", "tnum" 0; }7071/* ── Pages ─────────────────────────────────────────────────────────────── */72@page {73 size: A4;74 margin: 26mm 20mm 22mm;75 @top-left { content: "vquant"; font: 700 8.4pt Inter, sans-serif; color: #14171f; letter-spacing: -.01em; vertical-align: bottom; padding-bottom: 7mm; }76 @top-right { content: "${cssString(running)}"; font: 400 7.8pt Inter, sans-serif; color: #9a9fac; vertical-align: bottom; padding-bottom: 7mm; }77 @bottom-left { content: "${cssString(dateLabel)} · Note d'analyse"; font: 400 7.6pt Inter, sans-serif; color: #9a9fac; vertical-align: top; padding-top: 7mm; }78 @bottom-right { content: counter(page) " / " counter(pages); font: 500 7.8pt Inter, sans-serif; color: #6a7080; vertical-align: top; padding-top: 7mm; }79}80@page cover { margin: 0; @top-left { content: none; } @top-right { content: none; } @bottom-left { content: none; } @bottom-right { content: none; } }8182/* ── Cover: white, typographic, one mark ───────────────────────────────── */83.cover { page: cover; position: relative; width: 210mm; height: 297mm; overflow: hidden; background: var(--paper); color: var(--ink); break-after: page; }84.cover .watermark { position: absolute; right: -46mm; top: -40mm; width: 170mm; height: 170mm; opacity: .035; }85.cover .inner { position: absolute; inset: 0; padding: 24mm 24mm 22mm; display: flex; flex-direction: column; }86.brand { display: flex; align-items: center; gap: 2.6mm; }87.brand svg { width: 8.6mm; height: 8.6mm; }88.brand .word { font: 700 15pt Inter, sans-serif; letter-spacing: -.035em; color: var(--ink); }89.brand .tag { margin-left: auto; font: 500 7.4pt Inter, sans-serif; letter-spacing: .18em; text-transform: uppercase; color: var(--muted); }90.cover .topline { margin-top: 7mm; height: .3mm; background: var(--ink); }91.cover .lead { margin-top: 62mm; max-width: 150mm; }92.cover .kicker { font: 600 7.8pt Inter, sans-serif; letter-spacing: .22em; text-transform: uppercase; color: var(--accent); margin-bottom: 7mm; }93.cover h1 { margin: 0; font: 700 ${titleSize}pt/1.12 Inter, sans-serif; letter-spacing: -.032em; color: var(--ink); text-wrap: balance; }94.cover .question { margin: 9mm 0 0; max-width: 138mm; font: 400 11.2pt/1.55 Inter, sans-serif; color: var(--muted); padding-left: 4.5mm; border-left: .5mm solid var(--line); }95.cover .bottom { margin-top: auto; }96.cover .meta { display: grid; grid-template-columns: repeat(4, 1fr); border-top: .3mm solid var(--ink); border-bottom: .25mm solid var(--line); }97.cover .meta div { padding: 4.6mm 4mm 4.6mm 0; }98.cover .meta div + div { padding-left: 4.6mm; border-left: .25mm solid var(--line); }99.cover .meta dt { font: 500 6.8pt Inter, sans-serif; letter-spacing: .16em; text-transform: uppercase; color: var(--faint); margin-bottom: 1.6mm; }100.cover .meta dd { margin: 0; font: 600 11pt Inter, sans-serif; color: var(--ink); letter-spacing: -.01em; }101.cover .foot { margin: 7mm 0 0; font: 400 7.4pt/1.55 Inter, sans-serif; color: var(--faint); max-width: 150mm; }102103/* ── Contents ──────────────────────────────────────────────────────────── */104.toc { break-after: page; }105.toc h2 { counter-increment: none; margin-top: 0; border-top: 0; padding-top: 0; font-size: 17pt; }106.toc h2::before { content: none; }107.toc ol { list-style: none; margin: 9mm 0 0; padding: 0; }108.toc li { display: flex; align-items: baseline; gap: 4mm; padding: 3mm 0; border-bottom: .25mm solid var(--hair); }109.toc li .n { font: 600 8.6pt Inter, sans-serif; color: var(--accent); min-width: 9mm; font-variant-numeric: tabular-nums; }110.toc li a { color: var(--ink); text-decoration: none; border: 0; font: 500 10.4pt Inter, sans-serif; }111.toc li.sub { padding: 1.7mm 0 1.7mm 13mm; border-bottom: 0; }112.toc li.sub a { font-weight: 400; color: var(--muted); font-size: 9.4pt; }113.toc .stats { margin-top: 11mm; display: flex; gap: 7mm; flex-wrap: wrap; padding-top: 4mm; border-top: .25mm solid var(--line); }114.toc .stats span { font: 400 8pt Inter, sans-serif; color: var(--muted); }115.toc .stats b { color: var(--ink); font-weight: 600; }116117/* ── Body ──────────────────────────────────────────────────────────────── */118.doc { counter-reset: sec; }119.doc > :first-child { margin-top: 0; }120h1, h2, h3, h4 { font-family: Inter, sans-serif; color: var(--ink); break-after: avoid; break-inside: avoid; text-wrap: balance; }121.doc h1 { font-size: 20pt; font-weight: 700; letter-spacing: -.028em; line-height: 1.16; margin: 0 0 6mm; }122h2 { font-size: 14.2pt; font-weight: 700; letter-spacing: -.022em; line-height: 1.22; margin: 12mm 0 4mm; padding-top: 5mm; border-top: .3mm solid var(--ink); }123.doc h2 { counter-increment: sec; }124.doc h2::before { content: counter(sec, decimal-leading-zero); display: block; font: 600 7.8pt Inter, sans-serif; letter-spacing: .18em; color: var(--accent); margin-bottom: 1.6mm; }125h3 { font-size: 11.2pt; font-weight: 600; letter-spacing: -.012em; margin: 7mm 0 2.2mm; }126h4 { font-weight: 600; margin: 5mm 0 1.6mm; color: var(--muted); text-transform: uppercase; letter-spacing: .1em; font-size: 7.8pt; }127p { margin: 0 0 3.5mm; orphans: 3; widows: 3; text-wrap: pretty; }128strong { color: var(--ink); font-weight: 600; }129em { color: inherit; }130a { color: var(--accent); text-decoration: none; border-bottom: .2mm solid rgba(217,80,15,.3); }131ul, ol { margin: 0 0 3.8mm; padding-left: 5.2mm; }132li { margin-bottom: 1.3mm; padding-left: .8mm; }133li::marker { color: var(--faint); font-weight: 500; }134hr { border: 0; height: .25mm; background: var(--line); margin: 7mm 0; }135/* A rule right before a section is redundant with the section's own top rule. */136hr:has(+ h2), hr:last-child { display: none; }137.lede { font-size: 11.4pt; line-height: 1.6; color: var(--ink); padding: .5mm 0 .5mm 5.4mm; border-left: .7mm solid var(--accent); margin-bottom: 7mm; }138.lede strong { font-weight: 650; }139140blockquote { margin: 4.6mm 0; padding: 3.8mm 5mm; background: var(--wash); border-left: .6mm solid var(--line); border-radius: 0 1.6mm 1.6mm 0; color: var(--body); break-inside: avoid; }141blockquote p:last-child { margin-bottom: 0; }142143/* Tables: ruled, no dark fills */144.table-wrap { margin: 4.8mm 0 6mm; break-inside: auto; }145table { width: 100%; border-collapse: collapse; font-size: 8.6pt; line-height: 1.42; font-variant-numeric: tabular-nums; border-top: .35mm solid var(--ink); border-bottom: .25mm solid var(--ink); }146thead { display: table-header-group; }147th { background: var(--paper); color: var(--muted); font: 600 7.2pt Inter, sans-serif; letter-spacing: .08em; text-transform: uppercase; text-align: left; padding: 2.6mm 3mm 2.3mm; border-bottom: .25mm solid var(--ink); white-space: nowrap; }148td { padding: 2.2mm 3mm; border-top: .2mm solid var(--hair); vertical-align: top; color: var(--body); }149tbody tr:first-child td { border-top: 0; }150tbody tr:nth-child(even) td { background: var(--wash); }151tr { break-inside: avoid; }152td:first-child { color: var(--ink); font-weight: 500; }153th.num, td.num { text-align: right; white-space: nowrap; }154td.up { color: var(--up); font-weight: 600; }155td.down { color: var(--down); font-weight: 600; }156table.wide { font-size: 7.5pt; }157table.wide th { font-size: 6.6pt; padding: 2.2mm 2mm; }158table.wide td { padding: 1.9mm 2mm; }159160/* Figures: the image, a hairline, a caption */161figure { margin: 6mm 0 7mm; break-inside: avoid; }162figure .frame { border: .25mm solid var(--line); border-radius: 1.6mm; padding: 2.6mm; background: var(--paper); }163figure img { display: block; width: 100%; height: auto; max-height: 108mm; object-fit: contain; }164figcaption { margin-top: 2.4mm; font: 400 8.2pt/1.45 Inter, sans-serif; color: var(--muted); display: flex; gap: 2.6mm; align-items: baseline; }165figcaption b { font: 600 7.2pt Inter, sans-serif; letter-spacing: .14em; text-transform: uppercase; color: var(--accent); white-space: nowrap; }166img { max-width: 100%; }167168/* Code */169code { font: 500 8.4pt "JetBrains Mono", ui-monospace, monospace; background: var(--wash); border: .2mm solid var(--line); border-radius: 1mm; padding: .2mm 1.2mm; color: var(--ink); }170pre { margin: 4mm 0 5mm; padding: 4mm 4.6mm; background: var(--wash); color: var(--ink); border: .25mm solid var(--line); border-radius: 1.6mm; font: 500 7.9pt/1.55 "JetBrains Mono", ui-monospace, monospace; white-space: pre-wrap; word-break: break-word; break-inside: avoid; }171pre code { background: none; border: 0; padding: 0; color: inherit; font: inherit; }172.math-display { display: block; margin: 4mm 0; text-align: center; break-inside: avoid; }173174.vqm { display: inline-block; margin-right: .8mm; vertical-align: baseline; }175.vqm svg { vertical-align: -0.2em; }176177/* Closing note */178.closing { margin-top: 13mm; padding-top: 4mm; border-top: .25mm solid var(--line); font-size: 7.5pt; line-height: 1.55; color: var(--faint); break-inside: avoid; display: flex; gap: 3.4mm; align-items: flex-start; }179.closing svg { width: 4.6mm; height: 4.6mm; flex: none; margin-top: .4mm; }180.closing b { color: var(--muted); font-weight: 600; }181</style>182</head>183<body>184185<section class="cover">186 ${sparkleSvg({ mono: "#14171f", attrs: 'class="watermark"' })}187 <div class="inner">188 <div class="brand">${sparkleSvg({ id: "vqs-cover" })}<span class="word">vquant</span><span class="tag">Note d'analyse</span></div>189 <div class="topline"></div>190 <div class="lead">191 <div class="kicker">Recherche</div>192 <h1>${esc(title)}</h1>193 ${showQuestion ? `<p class="question">${esc(question.length > 320 ? question.slice(0, 319).trimEnd() + "…" : question)}</p>` : ""}194 </div>195 <div class="bottom">196 <dl class="meta">197 <div><dt>Date</dt><dd>${esc(dateLabel)}</dd></div>198 <div><dt>Sections</dt><dd id="m-sections">—</dd></div>199 <div><dt>Tableaux · Figures</dt><dd id="m-exhibits">—</dd></div>200 <div><dt>Lecture</dt><dd id="m-read">—</dd></div>201 </dl>202 <p class="foot">Document produit par VQuant, analyste quantitatif assisté par IA, à partir de données de marché et de sources publiques citées dans le texte. Les chiffres portent leur date ; vérifiez-les avant toute décision.</p>203 </div>204 </div>205</section>206207<section class="toc" id="toc" hidden>208 <h2>Sommaire</h2>209 <ol id="toc-list"></ol>210 <div class="stats" id="toc-stats"></div>211</section>212213<main class="doc" id="doc">214${bodyHtml}215<div class="closing">${sparkleSvg({ id: "vqs-end" })}<p style="margin:0"><b>À propos de ce document.</b> Cette note est une analyse à visée d'information. Elle ne constitue pas un conseil en placement personnalisé, ni une sollicitation d'achat ou de vente. Les données de marché proviennent des sources citées et peuvent comporter des retards ou des erreurs ; les projections et scénarios reposent sur des hypothèses qui peuvent ne pas se réaliser.</p></div>216</main>217218<script>219(function () {220 var doc = document.getElementById('doc');221222 // Headings: the document numbers its sections itself, so a leading "3." or a decorative emoji is dropped.223 doc.querySelectorAll('h1, h2, h3, h4').forEach(function (h) {224 var node = h.firstChild;225 while (node && node.nodeType !== 3) node = node.firstChild;226 if (!node) return;227 var cleaned = node.nodeValue228 .replace(/^[\\s\\u200d\\ufe0f\\u2600-\\u27bf\\u2b00-\\u2bff]*(?:[\\ud83c-\\ud83e][\\udc00-\\udfff][\\ufe0f\\u200d]*\\s*)*/, '')229 .replace(/^\\d\\ufe0f?\\u20e3\\s*/, '')230 .replace(/^(?:\\d{1,2}(?:\\.\\d{1,2})*[.)]|[IVX]{1,4}[.)])\\s+/, '');231 if (cleaned.trim() || h.textContent.trim() !== node.nodeValue.trim()) node.nodeValue = cleaned;232 });233234 // Lede: the opening paragraph carries the conclusion — set it apart.235 var first = doc.firstElementChild;236 while (first && (first.tagName === 'HR' || /^H[1-4]$/.test(first.tagName))) first = first.nextElementSibling;237 if (first && first.tagName === 'P' && first.textContent.trim().length > 80 && !first.querySelector('img')) first.classList.add('lede');238239 // Tables: frame, right-align numeric columns, colour signed percentages.240 var numeric = /^[\\s(]*[+\\-−–]?[$€£]?\\s?\\d[\\d\\s.,]*\\s?(%|x|×|k|M|Md|G|B|T|bp|pb|\\$|€|US\\$|CAD|USD|Md\\$|M\\$|G\\$)?[)\\s]*$/i;241 doc.querySelectorAll('table').forEach(function (table) {242 var wrap = document.createElement('div');243 wrap.className = 'table-wrap';244 table.parentNode.insertBefore(wrap, table);245 wrap.appendChild(table);246 var rows = Array.prototype.slice.call(table.querySelectorAll('tbody tr'));247 var cols = table.querySelectorAll('thead th').length || (rows[0] ? rows[0].children.length : 0);248 if (cols >= 7) table.classList.add('wide');249 for (var c = 1; c < cols; c++) {250 var cells = rows.map(function (r) { return r.children[c]; }).filter(Boolean);251 var filled = cells.filter(function (td) { var t = td.textContent.trim(); return t && t !== '—' && t !== '-' && t !== 'n/d' && t !== 'n.d.'; });252 var isNum = filled.length > 0 && filled.filter(function (td) { return numeric.test(td.textContent.trim()); }).length / filled.length >= 0.7;253 if (!isNum) continue;254 var th = table.querySelectorAll('thead th')[c];255 if (th) th.classList.add('num');256 cells.forEach(function (td) {257 td.classList.add('num');258 var t = td.textContent.trim();259 if (/%/.test(t)) {260 if (/^[(\\s]*\\+/.test(t)) td.classList.add('up');261 else if (/^[(\\s]*[\\-−–]\\s?\\d/.test(t)) td.classList.add('down');262 }263 });264 }265 });266267 // Figures: frame each image and number it.268 var figN = 0;269 doc.querySelectorAll('img').forEach(function (img) {270 var host = img.parentNode;271 var figure = document.createElement('figure');272 var frame = document.createElement('div');273 frame.className = 'frame';274 figN++;275 var cap = document.createElement('figcaption');276 var alt = (img.getAttribute('alt') || '').replace(/^(figure|fig\\.?|graphique)\\s*\\d*\\s*[:.\\-–]?\\s*/i, '').trim();277 cap.innerHTML = '<b>Figure ' + figN + '</b>';278 if (alt) { var s = document.createElement('span'); s.textContent = alt; cap.appendChild(s); }279 if (host.tagName === 'P' && host.textContent.trim() === '' && host.children.length === 1) {280 host.parentNode.insertBefore(figure, host); host.parentNode.removeChild(host);281 } else {282 host.insertBefore(figure, img);283 }284 frame.appendChild(img); figure.appendChild(frame); figure.appendChild(cap);285 });286287 // Contents: built from the section headings; shown when there is enough structure to navigate.288 var h2s = Array.prototype.slice.call(doc.querySelectorAll('h2'));289 var list = document.getElementById('toc-list');290 var secN = 0;291 doc.querySelectorAll('h2, h3').forEach(function (h, i) {292 h.id = 'sec-' + i;293 if (h2s.length < 3) return;294 var li = document.createElement('li');295 if (h.tagName === 'H2') {296 secN++;297 li.innerHTML = '<span class="n">' + (secN < 10 ? '0' : '') + secN + '</span>';298 } else {299 li.className = 'sub';300 }301 var a = document.createElement('a');302 a.href = '#' + h.id; a.textContent = h.textContent;303 li.appendChild(a); list.appendChild(li);304 });305306 var words = doc.textContent.trim().split(/\\s+/).length;307 var minutes = Math.max(1, Math.round(words / 220));308 var tables = doc.querySelectorAll('table').length;309 document.getElementById('m-sections').textContent = h2s.length || '1';310 document.getElementById('m-exhibits').textContent = tables + ' · ' + figN;311 document.getElementById('m-read').textContent = minutes + ' min';312 if (h2s.length >= 3) {313 document.getElementById('toc').hidden = false;314 document.getElementById('toc-stats').innerHTML =315 '<span><b>' + words.toLocaleString('fr-CA') + '</b> mots</span><span><b>' + tables + '</b> tableau' + (tables > 1 ? 'x' : '') +316 '</span><span><b>' + figN + '</b> figure' + (figN > 1 ? 's' : '') + '</span><span>Lecture <b>' + minutes + ' min</b></span>';317 }318 document.documentElement.setAttribute('data-ready', '1');319})();320</script>321</body>322</html>`;323}324