mirror of
https://github.com/garrytan/gstack.git
synced 2026-05-10 06:28:23 +08:00
Turn markdown into publication-quality PDFs. $P generate input.md out.pdf
produces a PDF with 1in margins, intelligent page breaks, page numbers,
running header, CONFIDENTIAL footer, and curly quotes/em dashes — all on
Helvetica so copy-paste extraction works ("S ai li ng" bug avoided).
Architecture (per Codex round 2):
markdown → render.ts (marked + sanitize + smartypants) → orchestrator
→ $B newtab --json → $B load-html --tab-id → $B js (poll Paged.js)
→ $B pdf --tab-id → $B closetab
browseClient.ts shells out to the compiled browse CLI rather than
duplicating Playwright. --tab-id isolation per render means parallel
$P generate calls don't race on the active tab. try/finally tab cleanup
survives Paged.js timeouts, browser crashes, and output-path failures.
Features in v1:
--cover left-aligned cover page (eyebrow + title + hairline rule)
--toc clickable static TOC (Paged.js page numbers deferred)
--watermark <text> diagonal DRAFT/CONFIDENTIAL layer
--no-chapter-breaks opt out of H1-starts-new-page
--page-numbers "N of M" footer (default on)
--tagged --outline accessible PDF + bookmark outline (default on)
--allow-network opt in to external image loading (default off for privacy)
--quiet --verbose stderr control
Design decisions locked from the /plan-design-review pass:
- Helvetica everywhere (Chromium emits single-word Tj operators for
system fonts; bundled webfonts emit per-glyph and break extraction).
- Left-aligned body, flush-left paragraphs, no text-indent, 12pt gap.
- Cover shares 1in margins with body pages; no flexbox-center, no
inset padding.
- The reference HTMLs at .context/designs/*.html are the implementation
source of truth for print-css.ts.
Tests (56 unit + 1 E2E combined-features gate):
- smartypants: code/URL-safe, verified against 10 fixtures
- sanitizer: strips <script>/<iframe>/on*/javascript: URLs
- render: HTML assembly, CJK fallback, cover/TOC/chapter wrap
- print-css: all @page rules, margin variants, watermark
- pdftotext: normalize()+copyPasteGate() cross-OS tolerance
- browseClient: binary resolution + typed error propagation
- combined-features gate (P0): 2-chapter fixture with smartypants +
hyphens + ligatures + bold/italic + inline code + lists + blockquote
passes through PDF → pdftotext → expected.txt diff
Deferred to Phase 4 (future PR): Paged.js vendored for accurate TOC page
numbers, highlight.js for syntax highlighting, drop caps, pull quotes,
two-column, CMYK, watermark visual-diff acceptance.
Plan: .context/ceo-plans/2026-04-19-perfect-pdf-generator.md
References: .context/designs/make-pdf-*.html
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
111 lines
3.9 KiB
TypeScript
111 lines
3.9 KiB
TypeScript
/**
|
|
* `$P setup` — guided smoke test.
|
|
*
|
|
* Flow (per the CEO plan CLI UX spec):
|
|
* 1. Verify browse binary exists and responds
|
|
* 2. Verify Chromium launches via $B goto about:blank
|
|
* 3. Verify pdftotext is installed (warn, don't fail)
|
|
* 4. Generate a smoke-test PDF from an inline 2-paragraph fixture
|
|
* 5. Open it
|
|
* 6. Print a 3-command cheatsheet
|
|
*/
|
|
|
|
import * as os from "node:os";
|
|
import * as path from "node:path";
|
|
import * as fs from "node:fs";
|
|
|
|
import * as browseClient from "./browseClient";
|
|
import { resolvePdftotext, PdftotextUnavailableError } from "./pdftotext";
|
|
import { generate } from "./orchestrator";
|
|
|
|
export async function runSetup(): Promise<void> {
|
|
process.stderr.write("make-pdf setup — verifying install\n\n");
|
|
|
|
// 1. Resolve browse binary
|
|
process.stderr.write(" [1/5] Checking browse binary...");
|
|
try {
|
|
const bin = browseClient.resolveBrowseBin();
|
|
process.stderr.write(` OK (${bin})\n`);
|
|
} catch (err: any) {
|
|
process.stderr.write(" FAIL\n");
|
|
process.stderr.write(`\n${err.message}\n`);
|
|
process.exit(4);
|
|
}
|
|
|
|
// 2. Chromium smoke (navigate a dedicated tab to about:blank)
|
|
process.stderr.write(" [2/5] Launching Chromium...");
|
|
let chromiumTab: number | null = null;
|
|
try {
|
|
chromiumTab = browseClient.newtab("about:blank");
|
|
process.stderr.write(` OK (tab ${chromiumTab})\n`);
|
|
} catch (err: any) {
|
|
process.stderr.write(" FAIL\n");
|
|
process.stderr.write(`\nChromium failed to launch: ${err.message}\n`);
|
|
process.stderr.write("\nTo fix: run gstack setup from the gstack repo:\n");
|
|
process.stderr.write(" cd ~/.claude/skills/gstack && ./setup\n");
|
|
process.exit(4);
|
|
} finally {
|
|
if (chromiumTab !== null) {
|
|
try { browseClient.closetab(chromiumTab); } catch { /* ignore */ }
|
|
}
|
|
}
|
|
|
|
// 3. pdftotext (optional — CI gate only)
|
|
process.stderr.write(" [3/5] Checking pdftotext (optional)...");
|
|
try {
|
|
const info = resolvePdftotext();
|
|
process.stderr.write(` OK (${info.flavor}, ${info.version.split(" ").slice(-1)[0] || "version unknown"})\n`);
|
|
} catch (err) {
|
|
process.stderr.write(" SKIP\n");
|
|
if (err instanceof PdftotextUnavailableError) {
|
|
process.stderr.write(
|
|
" pdftotext not installed. This is optional — only the CI\n" +
|
|
" copy-paste gate needs it. To enable:\n" +
|
|
" macOS: brew install poppler\n" +
|
|
" Ubuntu: sudo apt-get install poppler-utils\n",
|
|
);
|
|
}
|
|
}
|
|
|
|
// 4. Render smoke-test PDF
|
|
process.stderr.write(" [4/5] Generating smoke-test PDF...\n");
|
|
const fixture = [
|
|
"# Hello from make-pdf",
|
|
"",
|
|
"This is a two-paragraph smoke test. If you can read this sentence in the PDF that just opened, the pipeline works end-to-end.",
|
|
"",
|
|
"The second paragraph contains curly quotes (\"hello\"), an em dash -- like this, and an ellipsis... all of which should render correctly.",
|
|
"",
|
|
].join("\n");
|
|
const fixturePath = path.join(os.tmpdir(), `make-pdf-smoke-${process.pid}.md`);
|
|
const outPath = path.join(os.tmpdir(), `make-pdf-smoke-${process.pid}.pdf`);
|
|
fs.writeFileSync(fixturePath, fixture, "utf8");
|
|
|
|
try {
|
|
await generate({
|
|
input: fixturePath,
|
|
output: outPath,
|
|
quiet: true,
|
|
pageNumbers: true,
|
|
});
|
|
process.stderr.write(` PASSED. Smoke test saved to ${outPath}\n`);
|
|
} catch (err: any) {
|
|
process.stderr.write(` FAILED: ${err.message}\n`);
|
|
process.exit(2);
|
|
} finally {
|
|
try { fs.unlinkSync(fixturePath); } catch { /* ignore */ }
|
|
}
|
|
|
|
// 5. Cheatsheet
|
|
process.stderr.write(" [5/5] All checks passed.\n\n");
|
|
process.stderr.write([
|
|
"make-pdf is ready. Try:",
|
|
" $P generate letter.md # default memo mode",
|
|
" $P generate --cover --toc essay.md # full publication",
|
|
" $P generate --watermark DRAFT memo.md # diagonal watermark",
|
|
"",
|
|
`Smoke-test PDF: ${outPath}`,
|
|
"",
|
|
].join("\n"));
|
|
}
|