decklight 0.10.0 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/NOTICE +2 -0
- package/PLATFORM.md +17 -0
- package/README.md +53 -25
- package/SPEC.md +1239 -67
- package/cli/agents.mjs +139 -15
- package/cli/bundle.mjs +68 -2
- package/cli/cast.mjs +45 -14
- package/cli/check-fixes.mjs +12 -4
- package/cli/check-watch.mjs +151 -0
- package/cli/check.mjs +460 -71
- package/cli/code-editor.mjs +287 -0
- package/cli/commands.mjs +13 -2
- package/cli/commit-message.mjs +34 -7
- package/cli/commit-split.mjs +123 -23
- package/cli/decklight.mjs +11 -1
- package/cli/design-system-edit.mjs +37 -1
- package/cli/edit-address.mjs +184 -0
- package/cli/edit-git.mjs +21 -4
- package/cli/edit-handover.mjs +136 -25
- package/cli/edit-look.mjs +45 -1
- package/cli/edit-open.mjs +143 -0
- package/cli/edit-slides.mjs +1043 -67
- package/cli/edit-translate.mjs +243 -0
- package/cli/edit.mjs +3182 -267
- package/cli/editor-support.mjs +286 -0
- package/cli/enhance.mjs +148 -20
- package/cli/find-replace.mjs +142 -0
- package/cli/fix-review.mjs +368 -0
- package/cli/init.mjs +93 -19
- package/cli/keep-awake.mjs +68 -0
- package/cli/migrate-writing.mjs +214 -0
- package/cli/narrate.mjs +51 -10
- package/cli/open.mjs +29 -9
- package/cli/outside-edit.mjs +257 -0
- package/cli/paste.mjs +92 -0
- package/cli/pdf.mjs +61 -2
- package/cli/pptx-export.mjs +3 -1
- package/cli/publish.mjs +24 -3
- package/cli/record.mjs +19 -8
- package/cli/rewrite.mjs +265 -0
- package/cli/runtime-link.mjs +218 -17
- package/cli/serve.mjs +7 -4
- package/cli/setup.mjs +2 -2
- package/cli/skill-content.mjs +212 -8
- package/cli/skills/slide-writing.md +333 -0
- package/cli/skills/voiceover.md +423 -0
- package/cli/skills.mjs +36 -13
- package/cli/starter-full.mjs +353 -0
- package/cli/supervise.mjs +14 -0
- package/cli/theme.mjs +3 -2
- package/cli/translate-core.mjs +466 -0
- package/cli/translate-files.mjs +149 -0
- package/cli/translate.mjs +458 -0
- package/cli/util.mjs +25 -0
- package/cli/vocabulary-docs.mjs +138 -0
- package/dist/decklight.css +2087 -91
- package/dist/decklight.js +367 -203
- package/dist/decklight.js.map +4 -4
- package/docs/GLOSSARY.md +9 -2
- package/docs/WRITING_RESEARCH.md +327 -0
- package/package.json +5 -3
- package/themes/coastal.css +3 -0
- package/themes/metropolis.css +2 -0
- package/themes/reveal-blood.css +3 -0
- package/themes/reveal-league.css +2 -0
- package/themes/reveal-moon.css +2 -0
- package/themes/reveal-serif.css +1 -0
- package/themes/reveal-sky.css +1 -0
- package/themes/reveal-solarized.css +4 -0
- package/tools/brand-palette.mjs +6 -1
- package/tools/build-groups.mjs +28 -8
- package/tools/cdp.mjs +176 -0
- package/tools/chart-spec.mjs +242 -0
- package/tools/click-match.mjs +70 -0
- package/tools/code-options.mjs +174 -0
- package/tools/conversation.mjs +49 -0
- package/tools/deck-family.mjs +101 -0
- package/tools/deck-html.mjs +212 -15
- package/tools/design-system-format.mjs +15 -1
- package/tools/diagram-shapes.mjs +221 -0
- package/tools/elevenlabs-tts.mjs +62 -53
- package/tools/face-clip.mjs +96 -0
- package/tools/face-layout.mjs +94 -0
- package/tools/gemini-tts.mjs +1 -1
- package/tools/invisible-in-print.mjs +27 -0
- package/tools/lipsync-auto.mjs +197 -0
- package/tools/lipsync-config.mjs +16 -0
- package/tools/lipsync-engines.mjs +22 -4
- package/tools/lipsync-server.mjs +8 -5
- package/tools/lipsync.mjs +122 -30
- package/tools/local-voice.mjs +1 -1
- package/tools/mouth-file.mjs +26 -0
- package/tools/mouth.mjs +320 -0
- package/tools/narration-manifest.mjs +24 -4
- package/tools/narration-synth.mjs +96 -76
- package/tools/recording-impact.mjs +16 -1
- package/tools/sentences.mjs +7 -1
- package/tools/starters.mjs +402 -0
- package/tools/steps.mjs +118 -0
- package/tools/take-check.mjs +146 -0
- package/tools/template-theme.mjs +3 -1
- package/tools/test-impact.mjs +36 -12
- package/tools/theme-check.mjs +196 -1
- package/tools/tone-cue.mjs +108 -0
- package/tools/tts-cache.mjs +9 -3
- package/tools/tts-engines.mjs +100 -11
- package/tools/video-cache.mjs +93 -0
- package/tools/video-live.mjs +349 -0
- package/tools/video-options.mjs +11 -0
- package/tools/video.mjs +772 -127
- package/tools/viseme-fit.mjs +27 -0
- package/tools/viseme-match.mjs +77 -0
- package/tools/vocabulary.mjs +397 -0
- package/tools/voice-renumber.mjs +605 -0
- package/tools/voiceover-server.mjs +42 -11
- package/tools/voiceover.mjs +48 -20
- package/dist/build-info.json +0 -1
|
@@ -0,0 +1,605 @@
|
|
|
1
|
+
// Copyright 2026 Gilles Philippart
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A voice folder follows its slides (#916).
|
|
6
|
+
*
|
|
7
|
+
* A track folder is matched to the deck by POSITION: manifest entry n is
|
|
8
|
+
* slide n, and its files are `slide-NN.wav`, `slide-NN-KK.wav`,
|
|
9
|
+
* `slide-NN.txt`, `slide-NN-KK.visemes.json`. So a slide inserted at 30 used
|
|
10
|
+
* to mark every recording after it as out of step, though no word of theirs
|
|
11
|
+
* changed: only the numbering moved. This is the renumbering, three ways in:
|
|
12
|
+
*
|
|
13
|
+
* - a slide op: new, duplicate, delete, up, down, move and the template
|
|
14
|
+
* insert each say exactly where every slide goes, so the folders are moved
|
|
15
|
+
* in the same step as the deck;
|
|
16
|
+
* - an agent's edit, or any that rewrote the deck at once (#920): where each
|
|
17
|
+
* slide went is read off the sections before and after (`slideRenumber`
|
|
18
|
+
* in cli/commit-split.mjs), and carried out as `pairs`;
|
|
19
|
+
* - a realign (`realignMoves`): the deck was reordered somewhere else (an
|
|
20
|
+
* editor, a checkout), so where each recording belongs is read from what it
|
|
21
|
+
* was recorded FROM, the beats or the hash its manifest entry stores, and
|
|
22
|
+
* for one whose words changed since, loosely, from its saved script;
|
|
23
|
+
* - undo and redo, which replay the inverse.
|
|
24
|
+
*
|
|
25
|
+
* What is renumbered, and what undo keeps, is a STEP — `{ dir, op, slide, to }`
|
|
26
|
+
* or `{ dir, pairs }` — never a list of files or a copy of the manifest. A step
|
|
27
|
+
* is planned against the folder as it is when it runs, so a take recorded
|
|
28
|
+
* between an op and its undo is renumbered with everything else rather than
|
|
29
|
+
* written over by a stale snapshot, and its manifest entry goes with it.
|
|
30
|
+
*
|
|
31
|
+
* Nothing is ever deleted: a deleted slide's files go to `.trash/` inside
|
|
32
|
+
* the folder (a dot folder, so no track list walks into it), its manifest
|
|
33
|
+
* entry rides in the inverse step, and undo brings both back. A rename that
|
|
34
|
+
* would land on a file outside the plan is refused before anything moves.
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
import { existsSync, mkdirSync, readdirSync, readFileSync, renameSync, rmdirSync, writeFileSync } from 'node:fs';
|
|
38
|
+
import { basename, dirname, join, resolve, sep } from 'node:path';
|
|
39
|
+
import { slideBeats, slideTexts, priorSlideTexts, entryMatches, scriptDrift } from './narration-manifest.mjs';
|
|
40
|
+
import { deckFamily, ownsTrack, readManifest } from './deck-family.mjs';
|
|
41
|
+
|
|
42
|
+
/** A per-slide file of a track: `slide-NN` then an optional `-KK` beat, then its extension. */
|
|
43
|
+
const SLIDE_FILE = /^slide-(\d+)((?:-\d+)?\..+)$/;
|
|
44
|
+
/** The audio a track plays — what makes a folder a voice folder, with or without a manifest. */
|
|
45
|
+
const SLIDE_AUDIO = /^slide-\d+(-\d+)?\.(wav|m4a|mp3|ogg|opus|flac|aac)$/i;
|
|
46
|
+
const pad = (n) => String(n).padStart(2, '0');
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Where slide k (1-based) is after a slide op, as a function: a number, or
|
|
50
|
+
* null for the slide the op deleted. Total over every k, not only the deck's
|
|
51
|
+
* slides, so a manifest longer than the deck shifts with its tail.
|
|
52
|
+
* `insert` is the template insert, which is `new` with a layout.
|
|
53
|
+
*/
|
|
54
|
+
export function slideOpRenumber(op, slide, to) {
|
|
55
|
+
const swap = (a, b) => (k) => (k === a ? b : k === b ? a : k);
|
|
56
|
+
switch (op) {
|
|
57
|
+
case 'new': case 'duplicate': case 'insert': return (k) => (k > slide ? k + 1 : k);
|
|
58
|
+
case 'delete': return (k) => (k === slide ? null : k > slide ? k - 1 : k);
|
|
59
|
+
case 'up': return swap(slide - 1, slide);
|
|
60
|
+
case 'down': return swap(slide, slide + 1);
|
|
61
|
+
case 'move': return (k) => {
|
|
62
|
+
if (k === slide) return to;
|
|
63
|
+
if (slide < to && k > slide && k <= to) return k - 1;
|
|
64
|
+
if (slide > to && k >= to && k < slide) return k + 1;
|
|
65
|
+
return k;
|
|
66
|
+
};
|
|
67
|
+
default: throw new Error(`no renumbering for "${op}"`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** `[[from, to | null]]` as a renumbering: every other slide stays. */
|
|
72
|
+
export const movesRenumber = (pairs) => {
|
|
73
|
+
const map = new Map(pairs);
|
|
74
|
+
return (k) => (map.has(k) ? map.get(k) : k);
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
/** A step's renumbering. */
|
|
78
|
+
const stepRenumber = (step) => (step.pairs ? movesRenumber(step.pairs) : slideOpRenumber(step.op, step.slide, step.to));
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* The step that undoes `step` — the slots it deleted are put back by the
|
|
82
|
+
* plan's `restore`, so here a delete's inverse is only the gap it reopens.
|
|
83
|
+
*/
|
|
84
|
+
function inverseStep(step) {
|
|
85
|
+
if (step.pairs) return { pairs: step.pairs.filter(([, t]) => t != null).map(([f, t]) => [t, f]) };
|
|
86
|
+
const { op, slide, to } = step;
|
|
87
|
+
switch (op) {
|
|
88
|
+
case 'new': case 'duplicate': case 'insert': return { op: 'delete', slide: slide + 1 };
|
|
89
|
+
case 'delete': return { op: 'new', slide: slide - 1 };
|
|
90
|
+
case 'move': return { op: 'move', slide: to, to: slide };
|
|
91
|
+
default: return { op, slide }; // up and down are a swap, their own inverse
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Is `dir` a deck's folder? A `.html` there that is a decklight deck, other
|
|
97
|
+
* than `except`. Track folders are found by walking beside the deck, and one
|
|
98
|
+
* that belongs to another deck is not this deck's to renumber.
|
|
99
|
+
*/
|
|
100
|
+
function holdsDeck(dir, except = []) {
|
|
101
|
+
let names = [];
|
|
102
|
+
try { names = readdirSync(dir).filter((n) => /\.html?$/i.test(n) && !except.includes(n)); } catch { return false; }
|
|
103
|
+
return names.some((n) => {
|
|
104
|
+
try { return /class\s*=\s*["'][^"']*\bdecklight\b/.test(readFileSync(join(dir, n), 'utf8')); } catch { return false; }
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Is `deckPath` the only deck in its folder? The same deck in other
|
|
110
|
+
* languages (`talk.fr.html` beside `talk.html`) is not another deck: a
|
|
111
|
+
* translation must not cost the deck the recordings that follow its slides.
|
|
112
|
+
*/
|
|
113
|
+
export const aloneInFolder = (deckPath, family = deckFamily(deckPath)) => !holdsDeck(dirname(deckPath),
|
|
114
|
+
[basename(deckPath), ...family.members.map((f) => f.name)]);
|
|
115
|
+
|
|
116
|
+
/** Is this folder a voice track: per-slide audio, or a manifest with slides? */
|
|
117
|
+
function isTrack(abs) {
|
|
118
|
+
let names = [];
|
|
119
|
+
try { names = readdirSync(abs); } catch { return false; }
|
|
120
|
+
if (names.some((n) => SLIDE_AUDIO.test(n))) return true;
|
|
121
|
+
if (!names.includes('manifest.json')) return false;
|
|
122
|
+
try { return Array.isArray(JSON.parse(readFileSync(join(abs, 'manifest.json'), 'utf8')).slides); } catch { return false; }
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* The deck's voice folders, relative to its directory: the ones its config
|
|
127
|
+
* names, and — when it is the only deck there — every folder up to two levels
|
|
128
|
+
* down (`voices/<voice>`, the convention) that is a track: slide audio, or a
|
|
129
|
+
* manifest with slides. A folder holding a deck of its own is another deck's,
|
|
130
|
+
* and is neither taken nor walked into; a track in the language of another
|
|
131
|
+
* file of the deck's family (`voices-fr/…` beside `talk.html`) is that
|
|
132
|
+
* file's, and is not taken either. Nothing outside the deck's directory.
|
|
133
|
+
*/
|
|
134
|
+
export function voiceFolders(deckPath, configured = []) {
|
|
135
|
+
const deckDir = dirname(deckPath);
|
|
136
|
+
const out = new Set();
|
|
137
|
+
const inside = (rel) => {
|
|
138
|
+
const abs = resolve(deckDir, rel);
|
|
139
|
+
return abs.startsWith(deckDir + sep) ? abs.slice(deckDir.length + 1).split(sep).join('/') : null;
|
|
140
|
+
};
|
|
141
|
+
for (const c of configured) {
|
|
142
|
+
const rel = typeof c === 'string' ? inside(c.replace(/\/+$/, '')) : null;
|
|
143
|
+
if (rel && existsSync(resolve(deckDir, rel))) out.add(rel);
|
|
144
|
+
}
|
|
145
|
+
const family = deckFamily(deckPath);
|
|
146
|
+
if (aloneInFolder(deckPath, family)) {
|
|
147
|
+
const look = (rel) => {
|
|
148
|
+
const abs = resolve(deckDir, rel);
|
|
149
|
+
if (rel !== '.' && holdsDeck(abs)) return [];
|
|
150
|
+
if (rel !== '.' && isTrack(abs) && ownsTrack(family, rel, readManifest(abs))) out.add(rel);
|
|
151
|
+
let entries;
|
|
152
|
+
try { entries = readdirSync(abs, { withFileTypes: true }); } catch { return []; }
|
|
153
|
+
return entries.filter((e) => e.isDirectory() && !e.name.startsWith('.') && e.name !== 'node_modules');
|
|
154
|
+
};
|
|
155
|
+
for (const e of look('.')) for (const k of look(e.name)) look(`${e.name}/${k.name}`);
|
|
156
|
+
}
|
|
157
|
+
return [...out].sort();
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** A manifest's `file` entry renamed: the folder part, if any, kept; a slide that stays keeps its spelling. */
|
|
161
|
+
function renameRef(file, renumber) {
|
|
162
|
+
if (typeof file !== 'string') return file;
|
|
163
|
+
const at = file.lastIndexOf('/') + 1;
|
|
164
|
+
const m = SLIDE_FILE.exec(file.slice(at));
|
|
165
|
+
if (!m) return file;
|
|
166
|
+
const was = Number(m[1]);
|
|
167
|
+
const n = renumber(was);
|
|
168
|
+
return n == null || n === was ? file : `${file.slice(0, at)}slide-${pad(n)}${m[2]}`;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const readText = (p) => { try { return readFileSync(p, 'utf8'); } catch { return null; } };
|
|
172
|
+
const listFiles = (abs) => { try { return readdirSync(abs, { withFileTypes: true }).filter((e) => e.isFile()).map((e) => e.name); } catch { return []; } };
|
|
173
|
+
|
|
174
|
+
/** A dated trash folder name: `.trash/<time>-<what>`. */
|
|
175
|
+
const trashFor = (what) => `.trash/${new Date().toISOString().replace(/[:.]/g, '-')}-${what}`;
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* What carrying out `step` on its folder takes, read off the folder as it is
|
|
179
|
+
* now, without touching it:
|
|
180
|
+
* { dir, moves: [[from, to]], before, after, conflicts: [sentence], inverse }
|
|
181
|
+
*
|
|
182
|
+
* `moves` are paths relative to the folder: every per-slide file renumbered,
|
|
183
|
+
* a deleted slide's to a fresh `.trash/` folder, and the files a step's
|
|
184
|
+
* `restore` brings back from its `from` folder. `after` is the manifest's new
|
|
185
|
+
* text (null when it does not change): the CURRENT manifest's entries
|
|
186
|
+
* renumbered, and a restored slide's entry put back. `inverse` is the step
|
|
187
|
+
* that undoes this one, carrying what it deleted. A conflict is a rename that
|
|
188
|
+
* would land on a file outside the plan, or two onto one name: a plan with
|
|
189
|
+
* any is not to be carried out.
|
|
190
|
+
*/
|
|
191
|
+
export function planFolder(deckDir, step) {
|
|
192
|
+
const { dir } = step;
|
|
193
|
+
const abs = resolve(deckDir, dir);
|
|
194
|
+
const renumber = stepRenumber(step);
|
|
195
|
+
const names = listFiles(abs);
|
|
196
|
+
const before = readText(join(abs, 'manifest.json'));
|
|
197
|
+
let manifest = null;
|
|
198
|
+
try { manifest = before == null ? null : JSON.parse(before); } catch { /* not ours to rewrite */ }
|
|
199
|
+
const entries = Array.isArray(manifest?.slides) ? manifest.slides : null;
|
|
200
|
+
|
|
201
|
+
// the slides this step deletes, and what each leaves behind
|
|
202
|
+
const gone = new Set();
|
|
203
|
+
for (const name of names) {
|
|
204
|
+
const m = SLIDE_FILE.exec(name);
|
|
205
|
+
if (m && renumber(Number(m[1])) === null) gone.add(Number(m[1]));
|
|
206
|
+
}
|
|
207
|
+
entries?.forEach((e, i) => { if (renumber(i + 1) === null) gone.add(i + 1); });
|
|
208
|
+
const trash = gone.size ? trashFor(`slide-${[...gone].sort((a, b) => a - b).map(pad).join('-')}`) : null;
|
|
209
|
+
|
|
210
|
+
const moves = [];
|
|
211
|
+
for (const name of names) {
|
|
212
|
+
const m = SLIDE_FILE.exec(name);
|
|
213
|
+
if (!m) continue;
|
|
214
|
+
const was = Number(m[1]);
|
|
215
|
+
const n = renumber(was);
|
|
216
|
+
if (n === was) continue;
|
|
217
|
+
moves.push([name, n === null ? `${trash}/${name}` : `slide-${pad(n)}${m[2]}`]);
|
|
218
|
+
}
|
|
219
|
+
// a deleted slide coming back: its files from the trash it went to, at its slot
|
|
220
|
+
const restore = Array.isArray(step.restore) ? step.restore : [];
|
|
221
|
+
const back = new Set(restore.map((r) => r.slot));
|
|
222
|
+
if (restore.length && typeof step.from === 'string' && safe(step.from)) {
|
|
223
|
+
for (const name of listFiles(join(abs, step.from))) {
|
|
224
|
+
const m = SLIDE_FILE.exec(name);
|
|
225
|
+
if (m && back.has(Number(m[1]))) moves.push([`${step.from}/${name}`, name]);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const conflicts = [];
|
|
230
|
+
let after = null;
|
|
231
|
+
if (entries) {
|
|
232
|
+
const slides = [];
|
|
233
|
+
let len = 0;
|
|
234
|
+
entries.forEach((entry, i) => {
|
|
235
|
+
const n = renumber(i + 1);
|
|
236
|
+
if (n == null) return;
|
|
237
|
+
len = Math.max(len, n);
|
|
238
|
+
if (!entry) return;
|
|
239
|
+
if (slides[n - 1]) { conflicts.push(`two recordings would both become slide ${n}`); return; }
|
|
240
|
+
slides[n - 1] = {
|
|
241
|
+
...entry,
|
|
242
|
+
...(entry.file !== undefined ? { file: renameRef(entry.file, renumber) } : {}),
|
|
243
|
+
...(Array.isArray(entry.segments) ? { segments: entry.segments.map((s) => (s && s.file !== undefined ? { ...s, file: renameRef(s.file, renumber) } : s)) } : {}),
|
|
244
|
+
};
|
|
245
|
+
});
|
|
246
|
+
for (const r of restore) {
|
|
247
|
+
if (!r.entry) continue;
|
|
248
|
+
if (slides[r.slot - 1]) { conflicts.push(`slide ${r.slot} already has a recording to come back onto`); continue; }
|
|
249
|
+
slides[r.slot - 1] = r.entry;
|
|
250
|
+
len = Math.max(len, r.slot);
|
|
251
|
+
}
|
|
252
|
+
const text = JSON.stringify({ ...manifest, slides: Array.from({ length: len }, (_, i) => slides[i] ?? null) }, null, 2) + '\n';
|
|
253
|
+
if (text !== before) after = text;
|
|
254
|
+
}
|
|
255
|
+
conflicts.push(...blockers(abs, moves).map((b) => `${dir}/${b} is in the way`));
|
|
256
|
+
const targets = moves.map(([, t]) => t);
|
|
257
|
+
for (const t of new Set(targets.filter((t, i) => targets.indexOf(t) !== i))) conflicts.push(`two files would both become ${dir}/${t}`);
|
|
258
|
+
|
|
259
|
+
const inverse = {
|
|
260
|
+
dir,
|
|
261
|
+
...inverseStep(step),
|
|
262
|
+
...(gone.size ? { from: trash, restore: [...gone].map((slot) => ({ slot, entry: entries?.[slot - 1] ?? null })) } : {}),
|
|
263
|
+
};
|
|
264
|
+
return { dir, moves, before, after, conflicts, inverse };
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/** The targets of `moves` that exist and are not themselves moving: what a rename would overwrite. */
|
|
268
|
+
function blockers(abs, moves) {
|
|
269
|
+
const sources = new Set(moves.map(([f]) => f));
|
|
270
|
+
return moves.map(([, t]) => t).filter((t) => !sources.has(t) && existsSync(join(abs, t)));
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/** A path inside the folder: relative, no `..`, no absolute spelling. */
|
|
274
|
+
const safe = (p) => typeof p === 'string' && p && !/^[/\\]/.test(p) && !/^[a-zA-Z]:/.test(p) && !p.split(/[/\\]/).includes('..');
|
|
275
|
+
|
|
276
|
+
/** A plan worth carrying out: something moves, or the manifest changes. */
|
|
277
|
+
export const planMoves = (p) => p.moves.length > 0 || p.after !== null;
|
|
278
|
+
|
|
279
|
+
/** Plan every step; `{ plans }` worth carrying out, or `{ error }` naming the first conflict. */
|
|
280
|
+
export function planSteps(deckDir, steps) {
|
|
281
|
+
const plans = steps.map((s) => planFolder(deckDir, s)).filter(planMoves);
|
|
282
|
+
const stuck = plans.find((p) => p.conflicts.length);
|
|
283
|
+
if (stuck) return { error: `the recordings in ${stuck.dir}/ cannot follow the slides: ${stuck.conflicts[0]}`, plans };
|
|
284
|
+
return { plans };
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Carry out one plan: renames in two phases — every file to a temporary
|
|
289
|
+
* name, then every one to its own — so slide-30 → slide-31 never lands on a
|
|
290
|
+
* slide-31 that has not moved yet. If a rename fails partway, every rename
|
|
291
|
+
* already made is reversed and the error thrown: the folder is as it was.
|
|
292
|
+
*/
|
|
293
|
+
function carryOut(abs, moves) {
|
|
294
|
+
const tag = `.renumber-${process.pid}-${Date.now()}`;
|
|
295
|
+
const done = [];
|
|
296
|
+
const go = (from, to) => { renameSync(join(abs, from), join(abs, to)); done.push([from, to]); };
|
|
297
|
+
try {
|
|
298
|
+
moves.forEach(([f], i) => go(f, `${tag}-${i}`));
|
|
299
|
+
moves.forEach(([, t], i) => {
|
|
300
|
+
mkdirSync(dirname(join(abs, t)), { recursive: true });
|
|
301
|
+
go(`${tag}-${i}`, t);
|
|
302
|
+
});
|
|
303
|
+
} catch (e) {
|
|
304
|
+
for (const [from, to] of done.reverse()) { try { renameSync(join(abs, to), join(abs, from)); } catch { /* left under its name; said below */ } }
|
|
305
|
+
throw e;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Carry out `plans` (from `planFolder`/`planSteps`). A move whose file is gone
|
|
311
|
+
* is skipped; two onto one name are refused, always. A target that exists and is not moving: refused (an Error
|
|
312
|
+
* naming it, nothing moved) unless `aside`, which undo and redo pass — the
|
|
313
|
+
* file is then moved into `.trash/aside-<time>/` rather than written over,
|
|
314
|
+
* and named in the answer. A folder that fails is put back as it was, and so
|
|
315
|
+
* is every folder before it, before the error is thrown.
|
|
316
|
+
* Returns `{ moved, setAside }`.
|
|
317
|
+
*/
|
|
318
|
+
export function applyPlans(deckDir, plans, { aside = false } = {}) {
|
|
319
|
+
let moved = 0;
|
|
320
|
+
const setAside = [];
|
|
321
|
+
const done = [];
|
|
322
|
+
try {
|
|
323
|
+
for (const p of plans) {
|
|
324
|
+
const abs = resolve(deckDir, p.dir);
|
|
325
|
+
if (!abs.startsWith(deckDir + sep)) continue;
|
|
326
|
+
const moves = p.moves.filter(([f, t]) => safe(f) && safe(t) && existsSync(join(abs, f)));
|
|
327
|
+
const targets = moves.map(([, t]) => t);
|
|
328
|
+
const twice = targets.find((t, i) => targets.indexOf(t) !== i);
|
|
329
|
+
if (twice) throw new Error(`two files would both become ${p.dir}/${twice} — nothing was moved`);
|
|
330
|
+
const blocked = blockers(abs, moves);
|
|
331
|
+
if (blocked.length && !aside) throw new Error(`${p.dir}/${blocked[0]} is in the way — nothing was moved`);
|
|
332
|
+
if (blocked.length) {
|
|
333
|
+
const stamp = `.trash/aside-${new Date().toISOString().replace(/[:.]/g, '-')}`;
|
|
334
|
+
mkdirSync(join(abs, stamp), { recursive: true });
|
|
335
|
+
for (const b of blocked) {
|
|
336
|
+
const to = `${stamp}/${b.split('/').pop()}`;
|
|
337
|
+
renameSync(join(abs, b), join(abs, to));
|
|
338
|
+
setAside.push(`${p.dir}/${to}`);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
carryOut(abs, moves);
|
|
342
|
+
done.push({ abs, moves, before: p.before, wrote: typeof p.after === 'string' });
|
|
343
|
+
moved += moves.length;
|
|
344
|
+
if (typeof p.after === 'string') writeFileSync(join(abs, 'manifest.json'), p.after);
|
|
345
|
+
// a trash folder a restore just emptied goes; one with anything in it stays
|
|
346
|
+
for (const f of new Set(moves.map(([f]) => f).filter((f) => f.startsWith('.trash/')))) {
|
|
347
|
+
for (let d = dirname(join(abs, f)); d.startsWith(abs + sep); d = dirname(d)) {
|
|
348
|
+
try { rmdirSync(d); } catch { break; }
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
} catch (e) {
|
|
353
|
+
for (const d of done.reverse()) {
|
|
354
|
+
try { carryOut(d.abs, d.moves.map(([f, t]) => [t, f])); } catch { /* the error below still says it failed */ }
|
|
355
|
+
if (d.wrote && typeof d.before === 'string') { try { writeFileSync(join(d.abs, 'manifest.json'), d.before); } catch { /* as above */ } }
|
|
356
|
+
}
|
|
357
|
+
throw e;
|
|
358
|
+
}
|
|
359
|
+
return { moved, setAside };
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/**
|
|
363
|
+
* Where each of a manifest's recordings belongs, from what it was recorded
|
|
364
|
+
* from — the deck was reordered by something other than the slide ops.
|
|
365
|
+
*
|
|
366
|
+
* A recording that matches its own slide stays. One that does not is moved
|
|
367
|
+
* to the slide it does match (its stored `beats`, else its `hash`), the
|
|
368
|
+
* nearest one when two do, and never onto a slide whose own recording
|
|
369
|
+
* matches. One that matches nowhere — its words really changed, or it is a
|
|
370
|
+
* deleted slide's — and that its placed neighbours do not agree on, is read
|
|
371
|
+
* loosely (#920): its saved script (`scripts`, a
|
|
372
|
+
* Map slide → the track's `slide-NN.txt`, see trackScripts), else its beats,
|
|
373
|
+
* against each free slide's notes, and placed where half its words or more
|
|
374
|
+
* still are, when one slide alone reads best. Failing that, a run of them
|
|
375
|
+
* between two placed takes that shifted differently is split where the slide
|
|
376
|
+
* was put in or taken out: a take that reads more like the slide on one side
|
|
377
|
+
* of the split than the other says which side it is on. Otherwise it
|
|
378
|
+
* follows its neighbours when both shifted the same way (or its one
|
|
379
|
+
* neighbour, at either end of the track), and stays flagged there; when they
|
|
380
|
+
* disagree it stays, and is named in `unplaced`. An entry with no record of
|
|
381
|
+
* its words is never matched, only carried along by its neighbours, and it
|
|
382
|
+
* never gives way. A move onto a slide
|
|
383
|
+
* something else stays on: when that something is a recording that matches
|
|
384
|
+
* no slide, it is set aside (`to` null, its files to `.trash/`), which is
|
|
385
|
+
* what a slide deleted by hand leaves; otherwise the move is dropped, and so
|
|
386
|
+
* is every move that leaves blocked.
|
|
387
|
+
*
|
|
388
|
+
* Returns `{ moves: [[from, to | null]], said, unplaced }`, `said` the
|
|
389
|
+
* sentence offering it ("26 recordings look shifted by one from slide 30"),
|
|
390
|
+
* '' when nothing moves.
|
|
391
|
+
*/
|
|
392
|
+
export function realignMoves(manifest, html, { scripts = null } = {}) {
|
|
393
|
+
const slides = Array.isArray(manifest?.slides) ? manifest.slides : [];
|
|
394
|
+
const beats = slideBeats(html);
|
|
395
|
+
const texts = slideTexts(html);
|
|
396
|
+
const prior = priorSlideTexts(html);
|
|
397
|
+
const total = texts.length;
|
|
398
|
+
const known = (e) => !!e && ((Array.isArray(e.beats) && e.beats.length > 0) || !!e.hash);
|
|
399
|
+
const matches = (e, j) => {
|
|
400
|
+
if (j < 1 || j > total || !texts[j - 1]) return false;
|
|
401
|
+
if (Array.isArray(e.beats)) return e.beats.length > 0 && scriptDrift(e, beats[j - 1]) === null;
|
|
402
|
+
return entryMatches(manifest, e, texts[j - 1])
|
|
403
|
+
|| (manifest.recorder === 'deck' && !!prior[j - 1] && entryMatches(manifest, e, prior[j - 1]));
|
|
404
|
+
};
|
|
405
|
+
const target = new Map(); // from → to, for every entry placed: itself when it matches in place
|
|
406
|
+
const claimed = new Set();
|
|
407
|
+
slides.forEach((e, i) => { if (known(e) && matches(e, i + 1)) { target.set(i + 1, i + 1); claimed.add(i + 1); } });
|
|
408
|
+
slides.forEach((e, i) => {
|
|
409
|
+
const n = i + 1;
|
|
410
|
+
if (!known(e) || target.has(n)) return;
|
|
411
|
+
const cands = [];
|
|
412
|
+
for (let j = 1; j <= total; j++) if (!claimed.has(j) && matches(e, j)) cands.push(j);
|
|
413
|
+
if (!cands.length) return;
|
|
414
|
+
const best = Math.min(...cands.map((j) => Math.abs(j - n)));
|
|
415
|
+
const at = cands.filter((j) => Math.abs(j - n) === best);
|
|
416
|
+
if (at.length === 1) target.set(n, at[0]);
|
|
417
|
+
});
|
|
418
|
+
// its words changed: placed by what it SAID — its saved script, else its
|
|
419
|
+
// beats — read loosely against each slide's notes now (#920)
|
|
420
|
+
const bags = texts.map(words);
|
|
421
|
+
const heard = new Map();
|
|
422
|
+
const loose = (n, j) => {
|
|
423
|
+
if (j < 1 || j > total) return 0;
|
|
424
|
+
if (!heard.has(n)) {
|
|
425
|
+
const e = slides[n - 1];
|
|
426
|
+
const s = scripts?.get?.(n) ?? (Array.isArray(e?.beats) ? e.beats.join(' ') : null);
|
|
427
|
+
heard.set(n, s ? words(s) : null);
|
|
428
|
+
}
|
|
429
|
+
return overlap(heard.get(n), bags[j - 1]);
|
|
430
|
+
};
|
|
431
|
+
for (const t of target.values()) claimed.add(t);
|
|
432
|
+
// …only where the placed takes around it do not already say where it goes:
|
|
433
|
+
// a take whose words changed between two that stayed is on its slide
|
|
434
|
+
const placed = [...target.keys()].sort((a, b) => a - b);
|
|
435
|
+
const shiftAt = (n) => {
|
|
436
|
+
const prev = placed.filter((k) => k < n).pop();
|
|
437
|
+
const next = placed.find((k) => k > n);
|
|
438
|
+
const dp = prev === undefined ? null : target.get(prev) - prev;
|
|
439
|
+
const dn = next === undefined ? null : target.get(next) - next;
|
|
440
|
+
return dp === null ? dn : dn === null ? dp : dp === dn ? dp : null;
|
|
441
|
+
};
|
|
442
|
+
slides.forEach((e, i) => {
|
|
443
|
+
const n = i + 1;
|
|
444
|
+
if (!e || target.has(n) || shiftAt(n) !== null) return;
|
|
445
|
+
let best = LOOSE, at = [];
|
|
446
|
+
for (let j = 1; j <= total; j++) {
|
|
447
|
+
if (claimed.has(j)) continue;
|
|
448
|
+
const v = loose(n, j);
|
|
449
|
+
if (v > best) { best = v; at = [j]; } else if (v === best && v > LOOSE) at.push(j);
|
|
450
|
+
}
|
|
451
|
+
// one slide reads best, or none does: a tie is the same words twice, not a place
|
|
452
|
+
if (at.length === 1) { target.set(n, at[0]); claimed.add(at[0]); }
|
|
453
|
+
});
|
|
454
|
+
// a run of them between two placed takes that moved differently: the slide
|
|
455
|
+
// put in (or taken out) is somewhere in the run. Each take says which side
|
|
456
|
+
// of it it is on — it reads more like slide k+dp (before) or k+dn (after) —
|
|
457
|
+
// and a take that cannot tell, or a run that contradicts itself, stays put
|
|
458
|
+
let located = [...target.keys()].sort((a, b) => a - b);
|
|
459
|
+
for (let x = 0; x + 1 < located.length; x++) {
|
|
460
|
+
const prev = located[x], next = located[x + 1];
|
|
461
|
+
const dp = target.get(prev) - prev, dn = target.get(next) - next;
|
|
462
|
+
if (dp === dn || Math.abs(dn - dp) !== 1) continue;
|
|
463
|
+
const run = [];
|
|
464
|
+
for (let k = prev + 1; k < next; k++) if (slides[k - 1] && !target.has(k)) run.push(k);
|
|
465
|
+
const side = (k) => {
|
|
466
|
+
const p = loose(k, k + dp), q = loose(k, k + dn);
|
|
467
|
+
return Math.max(p, q) < LOOSE / 2 || p === q ? null : p > q ? dp : dn;
|
|
468
|
+
};
|
|
469
|
+
const said = run.map((k) => [k, side(k)]).filter(([, d]) => d !== null);
|
|
470
|
+
const lastBefore = Math.max(-Infinity, ...said.filter(([, d]) => d === dp).map(([k]) => k));
|
|
471
|
+
const firstAfter = Math.min(Infinity, ...said.filter(([, d]) => d === dn).map(([k]) => k));
|
|
472
|
+
if (!said.length || lastBefore > firstAfter) continue;
|
|
473
|
+
for (const k of run) {
|
|
474
|
+
if (k <= lastBefore) target.set(k, k + dp);
|
|
475
|
+
else if (k >= firstAfter) target.set(k, k + dn);
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
// matched nowhere: follow the neighbours when they agree, or the one there is
|
|
479
|
+
located = [...target.keys()].sort((a, b) => a - b);
|
|
480
|
+
const unplaced = new Set();
|
|
481
|
+
const lost = [];
|
|
482
|
+
slides.forEach((e, i) => {
|
|
483
|
+
const n = i + 1;
|
|
484
|
+
if (!e || target.has(n)) return;
|
|
485
|
+
const prev = located.filter((k) => k < n).pop();
|
|
486
|
+
const next = located.find((k) => k > n);
|
|
487
|
+
const dp = prev === undefined ? null : target.get(prev) - prev;
|
|
488
|
+
const dn = next === undefined ? null : target.get(next) - next;
|
|
489
|
+
const d = dp === null ? dn : dn === null ? dp : dp === dn ? dp : null;
|
|
490
|
+
if (d) { target.set(n, n + d); return; }
|
|
491
|
+
if (known(e)) unplaced.add(n);
|
|
492
|
+
if (d === null && known(e)) lost.push(n);
|
|
493
|
+
});
|
|
494
|
+
// settle: a move onto a slot something stays on, or that another move also
|
|
495
|
+
// wants, is dropped — which can leave its own entry staying, so again. A
|
|
496
|
+
// stayer that matches no slide gives way instead, to the trash.
|
|
497
|
+
const moving = new Map([...target].filter(([f, t]) => f !== t && t >= 1));
|
|
498
|
+
for (let changed = true; changed;) {
|
|
499
|
+
changed = false;
|
|
500
|
+
const stays = new Set(slides.map((e, i) => (e && !moving.has(i + 1) ? i + 1 : null)).filter(Boolean));
|
|
501
|
+
const wanted = new Map();
|
|
502
|
+
for (const t of moving.values()) if (t !== null) wanted.set(t, (wanted.get(t) ?? 0) + 1);
|
|
503
|
+
for (const [f, t] of moving) {
|
|
504
|
+
if (t === null) continue;
|
|
505
|
+
if (wanted.get(t) > 1) { moving.delete(f); changed = true; continue; }
|
|
506
|
+
if (stays.has(t)) {
|
|
507
|
+
if (unplaced.has(t)) moving.set(t, null);
|
|
508
|
+
else moving.delete(f);
|
|
509
|
+
changed = true;
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
// a set-aside that nothing ended up needing is not set aside
|
|
514
|
+
const wantedAtEnd = new Set([...moving.values()].filter((t) => t !== null));
|
|
515
|
+
for (const [f, t] of moving) if (t === null && !wantedAtEnd.has(f)) moving.delete(f);
|
|
516
|
+
const moves = [...moving].sort((a, b) => a[0] - b[0]);
|
|
517
|
+
// what could not be placed is said, never left silently on a wrong slide
|
|
518
|
+
const aside = new Set(moves.filter(([, t]) => t === null).map(([f]) => f));
|
|
519
|
+
const left = lost.filter((n) => !aside.has(n));
|
|
520
|
+
return { moves, said: realignSaid(moves, left), unplaced: left };
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
/** How loosely two scripts may agree and still be the same take: half their words. */
|
|
524
|
+
const LOOSE = 0.5;
|
|
525
|
+
/** A script's words, lowercased: what a loose comparison weighs. */
|
|
526
|
+
const words = (s) => new Set(String(s ?? '').toLowerCase().match(/[\p{L}\p{N}']+/gu) ?? []);
|
|
527
|
+
/** The share of words two scripts have in common (Jaccard), 0 when either has none. */
|
|
528
|
+
function overlap(a, b) {
|
|
529
|
+
if (!a?.size || !b?.size) return 0;
|
|
530
|
+
let shared = 0;
|
|
531
|
+
for (const w of a) if (b.has(w)) shared++;
|
|
532
|
+
return shared / (a.size + b.size - shared);
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
/** A track's saved scripts, `slide-NN.txt`: Map slide → text, what realign reads a stale take by. */
|
|
536
|
+
export function trackScripts(abs) {
|
|
537
|
+
const out = new Map();
|
|
538
|
+
for (const name of listFiles(abs)) {
|
|
539
|
+
const m = /^slide-(\d+)\.txt$/.exec(name);
|
|
540
|
+
if (!m) continue;
|
|
541
|
+
const text = readText(join(abs, name));
|
|
542
|
+
if (text != null) out.set(Number(m[1]), text);
|
|
543
|
+
}
|
|
544
|
+
return out;
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
const WORDS = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten'];
|
|
548
|
+
|
|
549
|
+
/**
|
|
550
|
+
* "26 recordings look shifted by one from slide 30", or the count when they
|
|
551
|
+
* moved several ways; a recording set aside is named after it.
|
|
552
|
+
*/
|
|
553
|
+
export function realignSaid(moves, unplaced = []) {
|
|
554
|
+
const shifted = moves.filter(([, t]) => t !== null);
|
|
555
|
+
const aside = moves.filter(([, t]) => t === null).map(([f]) => f);
|
|
556
|
+
if (!shifted.length) return '';
|
|
557
|
+
const lost = unplaced.length
|
|
558
|
+
? `; slide${unplaced.length === 1 ? '' : 's'} ${ranges(unplaced)} could not be placed (${unplaced.length === 1 ? 'its notes' : 'their notes'} changed): check ${unplaced.length === 1 ? 'it' : 'them'}`
|
|
559
|
+
: '';
|
|
560
|
+
const n = shifted.length;
|
|
561
|
+
const what = `${n} recording${n === 1 ? '' : 's'} look${n === 1 ? 's' : ''}`;
|
|
562
|
+
const shifts = new Set(shifted.map(([f, t]) => t - f));
|
|
563
|
+
const d = Math.abs([...shifts][0]);
|
|
564
|
+
const head = shifts.size === 1 ? `${what} shifted by ${WORDS[d] ?? d} from slide ${shifted[0][0]}` : `${what} out of place, from slide ${shifted[0][0]}`;
|
|
565
|
+
return head + (aside.length ? ` (slide ${aside.join(', ')}'s set aside: its slide is gone)` : '') + lost;
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
/** `[3, 4, 5, 9]` as "3–5, 9". */
|
|
569
|
+
function ranges(list) {
|
|
570
|
+
const out = [];
|
|
571
|
+
for (const n of [...list].sort((a, b) => a - b)) {
|
|
572
|
+
const last = out.at(-1);
|
|
573
|
+
if (last && n === last[1] + 1) last[1] = n; else out.push([n, n]);
|
|
574
|
+
}
|
|
575
|
+
return out.map(([a, b]) => (a === b ? `${a}` : `${a}–${b}`)).join(', ');
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
/**
|
|
579
|
+
* The line an edit's renumbering of one folder is said in (#920): "voices/me:
|
|
580
|
+
* 25 recordings moved with the new slide 32". `plan` is the folder's plan,
|
|
581
|
+
* `pairs` the deck's renumbering, `was` and `now` its slide counts either side.
|
|
582
|
+
*/
|
|
583
|
+
export function followedSaid(plan, pairs, was, now) {
|
|
584
|
+
const renumber = movesRenumber(pairs);
|
|
585
|
+
const moved = new Set();
|
|
586
|
+
for (const [from] of plan.moves) {
|
|
587
|
+
const m = SLIDE_FILE.exec(from);
|
|
588
|
+
if (m) moved.add(Number(m[1]));
|
|
589
|
+
}
|
|
590
|
+
const kept = new Set();
|
|
591
|
+
for (let k = 1; k <= was; k++) { const n = renumber(k); if (n != null) kept.add(n); }
|
|
592
|
+
const added = [];
|
|
593
|
+
for (let n = 1; n <= now; n++) if (!kept.has(n)) added.push(n);
|
|
594
|
+
const removed = pairs.filter(([, t]) => t == null).map(([f]) => f);
|
|
595
|
+
const binned = [...moved].filter((k) => renumber(k) == null).sort((a, b) => a - b);
|
|
596
|
+
const n = moved.size - binned.length;
|
|
597
|
+
const why = added.length === 1 && !removed.length ? `with the new slide ${added[0]}`
|
|
598
|
+
: removed.length === 1 && !added.length ? `as slide ${removed[0]} went`
|
|
599
|
+
: !added.length && !removed.length ? 'with the slides that moved'
|
|
600
|
+
: 'with the slides added, removed and moved';
|
|
601
|
+
const parts = [];
|
|
602
|
+
if (n) parts.push(`${n} recording${n === 1 ? '' : 's'} moved ${why}`);
|
|
603
|
+
if (binned.length) parts.push(`slide ${ranges(binned)}'s set aside in .trash/`);
|
|
604
|
+
return parts.length ? `${plan.dir}: ${parts.join('; ')}` : '';
|
|
605
|
+
}
|