SPB Git

Repository Public

deck-fixture.ts 7.4 KB 81 lines typescript

1/**2 * A tiny course repository with slide decks, written from scratch for the tests: hand-made PDFs (one page3 * per slide, a dark and a light export), the deck sources that give slides their identifiers, a figure4 * manifest, and speaker notes carrying sentinel phrases that must never reach anything public.5 */6import { mkdirSync, writeFileSync } from "node:fs";7import { join } from "node:path";89export const NOTE_SENTINELS = ["NOTE-PRIVEE-ALPHA ne jamais montrer ceci au public", "NOTE-PRIVEE-BETA rappel réservé à l'enseignant", "NOTE-PRIVEE-GAMMA dans le fichier des notes"];10export const SLIDE_WORD = "tardigrade"; // appears on one slide only: the search must find it there1112const WIN: Record<string, number> = { "•": 0x95, "’": 0x92, "—": 0x97, "«": 0xab, "»": 0xbb, "…": 0x85, "œ": 0x9c };13const pdfString = (s: string) => `(${[...s].map((ch) => { const c = WIN[ch] ?? ch.charCodeAt(0); return c > 126 || c < 32 ? `\\${c.toString(8).padStart(3, "0")}` : "()\\".includes(ch) ? `\\${ch}` : ch; }).join("")})`;1415/** A PDF with one 16:9 page per entry; each page is a list of [x, y, size, text]. */16export function makePdf(pages: [number, number, number, string][][], dark: boolean): Buffer {17  const W = 453.543, H = 255.118, objects: string[] = [];18  const add = (body: string) => { objects.push(body); return objects.length; };19  const font = add("<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica /Encoding /WinAnsiEncoding >>");20  const pagesId = objects.length + 1 + pages.length * 2; // reserved: the page tree comes after contents and pages21  const kids: number[] = [];22  for (const lines of pages) {23    const [bg, fg] = dark ? ["0.043 0.078 0.149", "0.96 0.945 0.9"] : ["1 1 1", "0.043 0.078 0.149"];24    const stream = `q ${bg} rg 0 0 ${W} ${H} re f Q\nq 0.1 0.34 0.76 rg 24 ${H - 60} 60 3 re f Q\n${lines.map(([x, y, size, text]) => `BT /F1 ${size} Tf ${fg} rg ${x} ${y} Td ${pdfString(text)} Tj ET`).join("\n")}\n`;25    const content = add(`<< /Length ${Buffer.byteLength(stream, "latin1")} >>\nstream\n${stream}endstream`);26    kids.push(add(`<< /Type /Page /Parent ${pagesId} 0 R /MediaBox [0 0 ${W} ${H}] /Resources << /Font << /F1 ${font} 0 R >> >> /Contents ${content} 0 R >>`));27  }28  const tree = add(`<< /Type /Pages /Kids [${kids.map((k) => `${k} 0 R`).join(" ")}] /Count ${kids.length} >>`);29  if (tree !== pagesId) throw new Error("numérotation des objets PDF incohérente");30  const catalog = add(`<< /Type /Catalog /Pages ${tree} 0 R >>`);31  let out = "%PDF-1.4\n";32  const offsets = objects.map((body, i) => { const at = Buffer.byteLength(out, "latin1"); out += `${i + 1} 0 obj\n${body}\nendobj\n`; return at; });33  const xref = Buffer.byteLength(out, "latin1");34  out += `xref\n0 ${objects.length + 1}\n0000000000 65535 f \n${offsets.map((o) => `${String(o).padStart(10, "0")} 00000 n \n`).join("")}trailer\n<< /Size ${objects.length + 1} /Root ${catalog} 0 R >>\nstartxref\n${xref}\n%%EOF\n`;35  return Buffer.from(out, "latin1");36}3738interface FixtureSlide { kind: "title" | "section" | "content" | "references" | "closing"; id?: string; title: string; body?: string[] }39export function fixtureDeck(session: string): FixtureSlide[] {40  return [41    { kind: "title", title: `Séance ${session} d’essai` },42    { kind: "content", id: `${session}-D02`, title: "Trois machines, un même mot", body: ["Trois systèmes dits « intelligents ».", "Font-ils la même chose ?"] },43    { kind: "section", title: "Définir les mots" },44    { kind: "content", id: `${session}-D03`, title: "La carte conceptuelle", body: ["Une figure occupe cette diapositive."] },45    { kind: "content", id: `${session}-D04`, title: "Un animal minuscule", body: [`Le ${SLIDE_WORD} survit au vide spatial.`, "Exemple choisi pour être unique."] },46    { kind: "section", title: "Conclure" },47    { kind: "content", id: `${session}-D05`, title: "À retenir", body: ["Un LLM prédit une suite plausible."] },48    { kind: "references", title: "Références I", body: ["[1] Auteur, Titre, 2020."] },49    { kind: "references", title: "Références II", body: ["[2] Autre auteur, Autre titre, 2021."] },50    { kind: "closing", title: "Merci, et bon laboratoire." },51  ];52}5354/** Writes `<repo>/course.yaml`-less deck sources and exports for the given sessions (enough for the slide builder). */55export function writeDeckRepo(repo: string, sessions: string[], o: { lightPages?: number } = {}) {56  mkdirSync(join(repo, "dist", "slides"), { recursive: true });57  for (const session of sessions) {58    const deck = fixtureDeck(session), dir = join(repo, "sessions", `${session}-essai`);59    mkdirSync(join(dir, "slides", "sections"), { recursive: true }); mkdirSync(join(dir, "figures"), { recursive: true });60    let section = 0;61    const page = (s: FixtureSlide, i: number): [number, number, number, string][] => {62      const footer: [number, number, number, string][] = [[24, 12, 6, "Prof Essai · UQO"], [200, 12, 6, "ATLAS IA"], [390, 12, 6, `${session} • ${i + 1} / ${deck.length}`]];63      if (s.kind === "title") return [[24, 230, 7, "ATLAS IA"], [24, 150, 20, s.title], [24, 120, 9, "Prof Essai"]];64      if (s.kind === "section") { section++; return [[24, 235, 7, `${session} • SECTION ${section}`], [24, 170, 30, String(section)], [24, 120, 18, s.title], [24, 12, 6, "Prof Essai · UQO"]]; }65      if (s.kind === "closing") return [[24, 235, 7, "ATLAS IA"], [24, 222, 7, `${session} • Séance d’essai`], [24, 150, 18, s.title], [24, 120, 9, "Prof Essai"]];66      return [[24, 235, 7, `${session} • ${s.kind === "references" ? "Synthèse" : "Partie I : Fondements"}`], [24, 205, 15, s.title], ...(s.body ?? []).map((t, k): [number, number, number, string] => [24, 170 - k * 16, 10, t]), ...footer];67    };68    const pages = deck.map(page);69    writeFileSync(join(repo, "dist", "slides", `${session}-web.pdf`), makePdf(pages, true));70    writeFileSync(join(repo, "dist", "slides", `${session}-web-light.pdf`), makePdf(o.lightPages ? pages.slice(0, o.lightPages) : pages, false));71    // The export with the notes exists in the real repository too: it must never be opened.72    writeFileSync(join(repo, "dist", "slides", `${session}-notes.pdf`), makePdf([[[24, 200, 10, NOTE_SENTINELS[0]!]]], false));73    writeFileSync(join(dir, "slides", "main.tex"), `% deck d'essai\n\\documentclass{atlas-beamer}\n\\begin{document}\n\\AtlasTitleSlide\n\\input{sections/01-debut}\n\\input{sections/02-fin}\n% \\input{sections/99-commente}\n\\end{document}\n`);74    const frame = (s: FixtureSlide) => `\\begin{frame}{${s.title}}\n  \\label{${s.id}}\n  ${(s.body ?? []).join(" ")}\n  \\note{${NOTE_SENTINELS[0]} \\AtlasSection{Piège dans une note}{x}}\n\\end{frame}\n`;75    writeFileSync(join(dir, "slides", "sections", "01-debut.tex"), `${frame(deck[1]!)}\n\\AtlasSection{${deck[2]!.title}}{Sous-titre}\n${frame(deck[3]!)}\n% \\begin{frame}{Diapositive commentée}\\label{${session}-D99}\\end{frame}\n${frame(deck[4]!)}\n`);76    writeFileSync(join(dir, "slides", "sections", "02-fin.tex"), `\\AtlasSection{${deck[5]!.title}}{Sous-titre}\n\\begin{frame}[fragile]{${deck[6]!.title}}\n\\label{${deck[6]!.id}}\n\\note{${NOTE_SENTINELS[1]}}\n\\end{frame}\n\\AtlasReferences\n\\AtlasClosingSlide{${deck[9]!.title}}\n`);77    writeFileSync(join(dir, "slides", "speaker-notes.md"), `# Notes de l'enseignant\n\n${NOTE_SENTINELS[2]}\n\n${NOTE_SENTINELS[0]}\n`);78    writeFileSync(join(dir, "figures", "manifest.yaml"), `session: ${session}\nfigures:\n  - id: ${session}-F01\n    slides: [${session}-D03]\n    alt: "Trois rectangles emboîtés, décrits pour qui ne voit pas la figure."\n`);79  }80}81
Files main
Menu

Theme