borndeveloper Cursor commited on
Commit
f1ffeba
·
1 Parent(s): b98b323

fix: regenerate TRD.pdf with proper print layout pipeline

Browse files

Add build_trd_pdf.py using Chromium headless with diagram scaling,
prose fence normalization, and print CSS instead of LibreOffice HTML export.

Co-authored-by: Cursor <cursoragent@cursor.com>

Files changed (3) hide show
  1. PDC data/TRD.md +1 -1
  2. PDC data/TRD.pdf +2 -2
  3. PDC data/build_trd_pdf.py +387 -0
PDC data/TRD.md CHANGED
@@ -20,7 +20,7 @@
20
  | 2.1 | 5 Jun 2026 | 5-case cascade, EPI/PPI probing, distance-weighted aggregation |
21
  | 2.2 | 12 Jun 2026 | **Client feasibility workflow**: GSM count-pair matrix, exact historical article recommendations, selectable count pairs, full dataset table |
22
 
23
- > **Note:** `TRD2.pdf` is the archived v2.0 export. `TRD.pdf` is generated from this document (v2.2).
24
 
25
  ## 1. Executive Overview
26
 
 
20
  | 2.1 | 5 Jun 2026 | 5-case cascade, EPI/PPI probing, distance-weighted aggregation |
21
  | 2.2 | 12 Jun 2026 | **Client feasibility workflow**: GSM count-pair matrix, exact historical article recommendations, selectable count pairs, full dataset table |
22
 
23
+ > **Note:** `TRD2.pdf` is the archived v2.0 export. Regenerate `TRD.pdf` with: `python3 build_trd_pdf.py`
24
 
25
  ## 1. Executive Overview
26
 
PDC data/TRD.pdf CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:dc18b2a5086390d079f5339e829432ffd173f433692fa365458cc6174ae97a06
3
- size 898230
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4f124090ea0ffe4ba8bbdf6a5b106c2c23947574db99dd4322bfcb90658fadb0
3
+ size 680057
PDC data/build_trd_pdf.py ADDED
@@ -0,0 +1,387 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """Build TRD.pdf from TRD.md with print-quality layout (Chromium headless)."""
3
+
4
+ from __future__ import annotations
5
+
6
+ import re
7
+ import shutil
8
+ import subprocess
9
+ import sys
10
+ import tempfile
11
+ from pathlib import Path
12
+
13
+ import markdown
14
+ from markdown.extensions import fenced_code, tables
15
+
16
+ ROOT = Path(__file__).resolve().parent
17
+ MD_PATH = ROOT / "TRD.md"
18
+ PDF_PATH = ROOT / "TRD.pdf"
19
+
20
+ BOX_CHARS = set("┌┐└┘│─┬┴├┤┼▼↑↓←→╭╮╯╰═║╔╗╚╝╠╣╦╩╬")
21
+
22
+ PRINT_CSS = """
23
+ @page {
24
+ size: A4;
25
+ margin: 18mm 16mm 20mm 16mm;
26
+ }
27
+
28
+ * { box-sizing: border-box; }
29
+
30
+ html { font-size: 10.5pt; }
31
+
32
+ body {
33
+ font-family: "Noto Sans", "Liberation Sans", Arial, sans-serif;
34
+ color: #1e293b;
35
+ line-height: 1.55;
36
+ max-width: 100%;
37
+ margin: 0;
38
+ padding: 0;
39
+ }
40
+
41
+ h1 {
42
+ font-size: 1.65rem;
43
+ color: #312e81;
44
+ border-bottom: 2px solid #6366f1;
45
+ padding-bottom: 0.35rem;
46
+ margin: 1.6rem 0 0.9rem;
47
+ page-break-after: avoid;
48
+ }
49
+
50
+ h2 {
51
+ font-size: 1.25rem;
52
+ color: #1e3a5f;
53
+ margin: 1.35rem 0 0.65rem;
54
+ page-break-after: avoid;
55
+ }
56
+
57
+ h3 {
58
+ font-size: 1.05rem;
59
+ color: #334155;
60
+ margin: 1.1rem 0 0.5rem;
61
+ page-break-after: avoid;
62
+ }
63
+
64
+ h4, h5, h6 {
65
+ font-size: 0.95rem;
66
+ color: #475569;
67
+ margin: 0.9rem 0 0.4rem;
68
+ page-break-after: avoid;
69
+ }
70
+
71
+ p {
72
+ margin: 0.45rem 0 0.65rem;
73
+ text-align: justify;
74
+ hyphens: auto;
75
+ }
76
+
77
+ strong { color: #0f172a; }
78
+
79
+ blockquote {
80
+ margin: 0.6rem 0;
81
+ padding: 0.55rem 0.85rem;
82
+ border-left: 3px solid #6366f1;
83
+ background: #f8fafc;
84
+ color: #334155;
85
+ }
86
+
87
+ table {
88
+ width: 100%;
89
+ border-collapse: collapse;
90
+ margin: 0.75rem 0 1rem;
91
+ font-size: 8.5pt;
92
+ table-layout: fixed;
93
+ page-break-inside: avoid;
94
+ }
95
+
96
+ th, td {
97
+ border: 1px solid #cbd5e1;
98
+ padding: 5px 7px;
99
+ vertical-align: top;
100
+ word-wrap: break-word;
101
+ overflow-wrap: anywhere;
102
+ hyphens: auto;
103
+ }
104
+
105
+ /* Two-column capability tables */
106
+ table:has(thead th:nth-child(2)) th:first-child,
107
+ table:has(thead th:nth-child(2)) td:first-child {
108
+ width: 26%;
109
+ font-weight: 600;
110
+ }
111
+
112
+ table:has(thead th:nth-child(2)) th:last-child,
113
+ table:has(thead th:nth-child(2)) td:last-child {
114
+ width: 74%;
115
+ }
116
+
117
+ th {
118
+ background: #f1f5f9;
119
+ font-weight: 600;
120
+ color: #0f172a;
121
+ }
122
+
123
+ tr:nth-child(even) td { background: #fafafa; }
124
+
125
+ pre {
126
+ margin: 0.65rem 0 0.85rem;
127
+ padding: 0;
128
+ background: transparent;
129
+ border: none;
130
+ page-break-inside: avoid;
131
+ }
132
+
133
+ pre code,
134
+ code {
135
+ font-family: "CaskaydiaMono NFM", "DejaVu Sans Mono", "Liberation Mono", monospace;
136
+ }
137
+
138
+ pre code {
139
+ display: block;
140
+ white-space: pre;
141
+ overflow-x: visible;
142
+ background: #f8fafc;
143
+ border: 1px solid #e2e8f0;
144
+ border-radius: 4px;
145
+ padding: 8px 10px;
146
+ line-height: 1.25;
147
+ }
148
+
149
+ pre.diagram code {
150
+ background: #f1f5f9;
151
+ border-color: #cbd5e1;
152
+ padding: 10px 8px;
153
+ letter-spacing: 0;
154
+ }
155
+
156
+ pre.prose code {
157
+ font-family: "Noto Sans", "Liberation Sans", Arial, sans-serif;
158
+ white-space: pre-wrap;
159
+ background: transparent;
160
+ border: none;
161
+ padding: 0;
162
+ font-size: inherit;
163
+ line-height: inherit;
164
+ color: inherit;
165
+ }
166
+
167
+ pre.code code {
168
+ font-size: 8.5pt;
169
+ white-space: pre-wrap;
170
+ word-break: break-word;
171
+ }
172
+
173
+ p > code,
174
+ li > code,
175
+ td > code {
176
+ font-size: 0.88em;
177
+ background: #f1f5f9;
178
+ padding: 1px 4px;
179
+ border-radius: 3px;
180
+ }
181
+
182
+ ul, ol {
183
+ margin: 0.4rem 0 0.7rem 1.2rem;
184
+ padding: 0;
185
+ }
186
+
187
+ li { margin: 0.2rem 0; }
188
+
189
+ hr {
190
+ border: none;
191
+ border-top: 1px solid #e2e8f0;
192
+ margin: 1.2rem 0;
193
+ }
194
+
195
+ .cover-meta {
196
+ margin: 0.5rem 0 1.2rem;
197
+ font-size: 10pt;
198
+ color: #475569;
199
+ }
200
+
201
+ .cover-meta p { margin: 0.2rem 0; text-align: left; }
202
+ """
203
+
204
+
205
+ def _is_diagram(text: str) -> bool:
206
+ return any(ch in BOX_CHARS for ch in text)
207
+
208
+
209
+ def _is_code(lang: str | None, text: str) -> bool:
210
+ if lang in {"python", "json", "bash", "shell", "sql"}:
211
+ return True
212
+ stripped = text.strip()
213
+ if stripped.startswith("{") or stripped.startswith("def "):
214
+ return True
215
+ if re.search(r"\b(import |return |for |if |elif |else:)\b", text):
216
+ return True
217
+ return False
218
+
219
+
220
+ def preprocess_markdown(raw: str) -> str:
221
+ """Normalize fences: prose blocks → paragraphs; tag diagrams and code."""
222
+ fence_re = re.compile(r"```(\w*)\n(.*?)```", re.DOTALL)
223
+
224
+ def repl(match: re.Match[str]) -> str:
225
+ lang = match.group(1).strip().lower() or None
226
+ body = match.group(2).rstrip("\n")
227
+ if not body.strip():
228
+ return ""
229
+
230
+ if lang == "text" or _is_diagram(body):
231
+ size = _diagram_font_size(body)
232
+ escaped = _escape_html(body)
233
+ return (
234
+ f'<pre class="diagram" style="font-size:{size}pt">'
235
+ f"<code>{escaped}</code></pre>"
236
+ )
237
+
238
+ if _is_code(lang, body):
239
+ escaped = _escape_html(body)
240
+ return f'<pre class="code"><code>{escaped}</code></pre>'
241
+
242
+ # Prose incorrectly wrapped in fences — render as paragraph(s)
243
+ paragraphs = [p.strip() for p in body.split("\n\n") if p.strip()]
244
+ out = []
245
+ for p in paragraphs:
246
+ line = " ".join(ln.strip() for ln in p.splitlines())
247
+ out.append(f"<p>{_inline_format(line)}</p>")
248
+ return "\n".join(out)
249
+
250
+ return fence_re.sub(repl, raw)
251
+
252
+
253
+ def _diagram_font_size(text: str) -> float:
254
+ lines = [ln.rstrip() for ln in text.splitlines() if ln.strip()]
255
+ if not lines:
256
+ return 7.5
257
+ max_len = max(len(ln) for ln in lines)
258
+ # A4 printable width ≈ 95 chars at 7.5pt mono with 16mm margins
259
+ if max_len <= 72:
260
+ return 8.5
261
+ if max_len <= 85:
262
+ return 7.5
263
+ if max_len <= 95:
264
+ return 6.8
265
+ if max_len <= 110:
266
+ return 6.0
267
+ return max(5.0, 95 * 6.0 / max_len)
268
+
269
+
270
+ def _escape_html(text: str) -> str:
271
+ return (
272
+ text.replace("&", "&amp;")
273
+ .replace("<", "&lt;")
274
+ .replace(">", "&gt;")
275
+ )
276
+
277
+
278
+ def _inline_format(text: str) -> str:
279
+ text = _escape_html(text)
280
+ text = re.sub(r"\*\*(.+?)\*\*", r"<strong>\1</strong>", text)
281
+ text = re.sub(r"`([^`]+)`", r"<code>\1</code>", text)
282
+ return text
283
+
284
+
285
+ def _fix_heading_levels(raw: str) -> str:
286
+ """Demote duplicate H1 lines so the doc has one title hierarchy."""
287
+ lines = raw.splitlines()
288
+ out: list[str] = []
289
+ h1_seen = False
290
+ for line in lines:
291
+ if line.startswith("# ") and not line.startswith("## "):
292
+ if h1_seen:
293
+ line = "#" + line # H1 → H2
294
+ else:
295
+ h1_seen = True
296
+ out.append(line)
297
+ return "\n".join(out)
298
+
299
+
300
+ def _split_legacy_table_rows(html: str) -> str:
301
+ """Split single-column 'Details' tables into readable multi-line cells."""
302
+ def fix_row(match: re.Match[str]) -> str:
303
+ cell = match.group(1)
304
+ if cell.strip().lower() == "details":
305
+ return match.group(0)
306
+ # Insert line breaks before common field-label patterns in dense rows
307
+ cell = re.sub(
308
+ r"\s+(?=(?:Input Field|Output Field|Capability|Description|Confidence|"
309
+ r"Endpoint|Metric|KPI|Case ID|Requirement ID|Risk ID|Weave Key|"
310
+ r"Warp Count|Weft Count|Finish EPI|Greige|Reed|Blend|Loom|GSM)\b)",
311
+ "<br>",
312
+ cell,
313
+ )
314
+ return f"<td>{cell}</td>"
315
+
316
+ return re.sub(r"<td>([^<]{80,})</td>", fix_row, html)
317
+
318
+
319
+ def build_html(md_text: str) -> str:
320
+ md_text = _fix_heading_levels(md_text)
321
+ md_text = preprocess_markdown(md_text)
322
+ body = markdown.markdown(
323
+ md_text,
324
+ extensions=["tables", "fenced_code", "nl2br", "sane_lists"],
325
+ )
326
+ body = _split_legacy_table_rows(body)
327
+ return f"""<!DOCTYPE html>
328
+ <html lang="en">
329
+ <head>
330
+ <meta charset="utf-8">
331
+ <meta name="viewport" content="width=device-width, initial-scale=1">
332
+ <title>PDC Technical Requirements Document v2.2</title>
333
+ <style>{PRINT_CSS}</style>
334
+ </head>
335
+ <body>
336
+ {body}
337
+ </body>
338
+ </html>
339
+ """
340
+
341
+
342
+ def find_chromium() -> str:
343
+ for name in ("chromium", "chromium-browser", "google-chrome", "google-chrome-stable"):
344
+ path = shutil.which(name)
345
+ if path:
346
+ return path
347
+ raise RuntimeError("Chromium/Chrome not found — required for PDF export")
348
+
349
+
350
+ def print_pdf(html_path: Path, pdf_path: Path) -> None:
351
+ chrome = find_chromium()
352
+ cmd = [
353
+ chrome,
354
+ "--headless=new",
355
+ "--disable-gpu",
356
+ "--no-sandbox",
357
+ "--run-all-compositor-stages-before-draw",
358
+ "--virtual-time-budget=15000",
359
+ f"--print-to-pdf={pdf_path}",
360
+ "--no-pdf-header-footer",
361
+ f"file://{html_path.resolve()}",
362
+ ]
363
+ result = subprocess.run(cmd, capture_output=True, text=True)
364
+ if result.returncode != 0:
365
+ raise RuntimeError(result.stderr or result.stdout or "Chromium PDF export failed")
366
+
367
+
368
+ def main() -> int:
369
+ if not MD_PATH.exists():
370
+ print(f"Missing {MD_PATH}", file=sys.stderr)
371
+ return 1
372
+
373
+ raw = MD_PATH.read_text(encoding="utf-8")
374
+ html = build_html(raw)
375
+
376
+ with tempfile.TemporaryDirectory() as tmp:
377
+ html_path = Path(tmp) / "TRD.html"
378
+ html_path.write_text(html, encoding="utf-8")
379
+ print_pdf(html_path, PDF_PATH)
380
+
381
+ size_kb = PDF_PATH.stat().st_size // 1024
382
+ print(f"Wrote {PDF_PATH} ({size_kb} KB)")
383
+ return 0
384
+
385
+
386
+ if __name__ == "__main__":
387
+ raise SystemExit(main())