@conversation/spec 0.0.0-stage → 0.1.2
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/LICENSE +22 -0
- package/README.md +132 -2
- package/errors.d.ts +9 -0
- package/errors.js +36 -0
- package/esm/errors.js +31 -0
- package/esm/include.js +15 -0
- package/esm/index.js +7 -0
- package/esm/parse.js +469 -0
- package/esm/plugin.js +50 -0
- package/esm/plugins/effects.js +17 -0
- package/esm/plugins/index.js +10 -0
- package/esm/plugins/slides.js +48 -0
- package/esm/plugins/speakers.js +52 -0
- package/esm/plugins/video.js +39 -0
- package/esm/schema.js +100 -0
- package/esm/segments.js +60 -0
- package/esm/suggest.js +51 -0
- package/esm/types.js +1 -0
- package/esm/values.js +228 -0
- package/include.d.ts +23 -0
- package/include.js +18 -0
- package/index.d.ts +8 -0
- package/index.js +48 -0
- package/package.json +42 -4
- package/parse.d.ts +15 -0
- package/parse.js +472 -0
- package/plugin.d.ts +52 -0
- package/plugin.js +54 -0
- package/plugins/effects.d.ts +7 -0
- package/plugins/effects.js +20 -0
- package/plugins/index.d.ts +7 -0
- package/plugins/index.js +18 -0
- package/plugins/slides.d.ts +6 -0
- package/plugins/slides.js +51 -0
- package/plugins/speakers.d.ts +9 -0
- package/plugins/speakers.js +56 -0
- package/plugins/video.d.ts +6 -0
- package/plugins/video.js +42 -0
- package/schema.d.ts +56 -0
- package/schema.js +113 -0
- package/segments.d.ts +9 -0
- package/segments.js +63 -0
- package/suggest.d.ts +7 -0
- package/suggest.js +56 -0
- package/types.d.ts +93 -0
- package/types.js +2 -0
- package/values.d.ts +36 -0
- package/values.js +236 -0
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.slides = slides;
|
|
4
|
+
/**
|
|
5
|
+
* `[slide ID]` / `[slide ID step:N]` — a deck shows slide ID (and its Nth build)
|
|
6
|
+
* from this point. Silent and timeless: a renderer records when it falls.
|
|
7
|
+
*/
|
|
8
|
+
function slides() {
|
|
9
|
+
return {
|
|
10
|
+
name: 'slides',
|
|
11
|
+
directives: {
|
|
12
|
+
slide(args, ctx) {
|
|
13
|
+
let id = null;
|
|
14
|
+
let step = 0;
|
|
15
|
+
let bad = false;
|
|
16
|
+
for (const t of args.split(/[\s,]+/).filter(Boolean)) {
|
|
17
|
+
const i = t.indexOf(':');
|
|
18
|
+
if (i >= 0) {
|
|
19
|
+
const k = t.slice(0, i);
|
|
20
|
+
const v = t.slice(i + 1);
|
|
21
|
+
if (k !== 'step') {
|
|
22
|
+
ctx.error(`\`${k}:\` isn't a slide option — use \`[slide ID]\` or \`[slide ID step:N]\`` +
|
|
23
|
+
ctx.suggest(k, ['step']));
|
|
24
|
+
bad = true;
|
|
25
|
+
}
|
|
26
|
+
else if (!/^\d+$/.test(v)) {
|
|
27
|
+
ctx.error(`\`step:${v}\` — step is a whole number of builds, e.g. \`step:1\``);
|
|
28
|
+
bad = true;
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
step = parseInt(v, 10);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
else if (id === null && /^[a-z0-9][a-z0-9_-]*$/.test(t)) {
|
|
35
|
+
id = t;
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
ctx.error(id === null
|
|
39
|
+
? `\`${t}\` — a slide id is lowercase, like the deck's ids (\`title\`, \`market-opportunity\`)`
|
|
40
|
+
: `\`${t}\` — one slide per marker; put the next on its own line`);
|
|
41
|
+
bad = true;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (id === null && !bad) {
|
|
45
|
+
ctx.error('`[slide]` needs the slide\'s id, e.g. `[slide problem]` or `[slide problem step:1]`');
|
|
46
|
+
}
|
|
47
|
+
return { id, step };
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ConvPlugin } from '../plugin';
|
|
2
|
+
/** What a player can show for whoever is speaking. */
|
|
3
|
+
export declare const SPEAKER_WIDGETS: string[];
|
|
4
|
+
/**
|
|
5
|
+
* `[speakers faces lower-third waveform captions]`, `[speakers all]`, `[speakers off]`
|
|
6
|
+
* — which on-screen widgets a player shows from here. Characters gain `name:` and
|
|
7
|
+
* `title:` for the lower-third.
|
|
8
|
+
*/
|
|
9
|
+
export declare function speakers(): ConvPlugin;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SPEAKER_WIDGETS = void 0;
|
|
4
|
+
exports.speakers = speakers;
|
|
5
|
+
const schema_1 = require("../schema");
|
|
6
|
+
/** What a player can show for whoever is speaking. */
|
|
7
|
+
exports.SPEAKER_WIDGETS = ['faces', 'lower-third', 'waveform', 'captions'];
|
|
8
|
+
const ALWAYS = 'lower-third:always';
|
|
9
|
+
/** `faces` leaves the mouth alone (still is the default; `faces:still` says the same); `faces:talking` moves it with the voice. */
|
|
10
|
+
const TALKING = 'faces:talking';
|
|
11
|
+
/**
|
|
12
|
+
* `[speakers faces lower-third waveform captions]`, `[speakers all]`, `[speakers off]`
|
|
13
|
+
* — which on-screen widgets a player shows from here. Characters gain `name:` and
|
|
14
|
+
* `title:` for the lower-third.
|
|
15
|
+
*/
|
|
16
|
+
function speakers() {
|
|
17
|
+
const choices = [...exports.SPEAKER_WIDGETS, ALWAYS, TALKING, 'all', 'off'];
|
|
18
|
+
return {
|
|
19
|
+
name: 'speakers',
|
|
20
|
+
keys: { character: { name: (0, schema_1.str)(), title: (0, schema_1.str)() } },
|
|
21
|
+
directives: {
|
|
22
|
+
speakers(args, ctx) {
|
|
23
|
+
const want = args
|
|
24
|
+
.toLowerCase()
|
|
25
|
+
.split(/[\s,+]+/)
|
|
26
|
+
.filter(Boolean)
|
|
27
|
+
.map((w) => (w === 'lower-third:first' ? 'lower-third' : w === 'faces:still' ? 'faces' : w));
|
|
28
|
+
if (!want.length) {
|
|
29
|
+
ctx.error('`[speakers]` needs what to show, e.g. `[speakers faces lower-third]`, `[speakers all]` ' +
|
|
30
|
+
`or \`[speakers off]\` — widgets: ${exports.SPEAKER_WIDGETS.join(', ')}`);
|
|
31
|
+
}
|
|
32
|
+
for (const w of want.filter((x) => !choices.includes(x))) {
|
|
33
|
+
ctx.error(`\`${w}\` isn't a speaker widget` +
|
|
34
|
+
ctx.suggest(w, choices) +
|
|
35
|
+
` — widgets: ${exports.SPEAKER_WIDGETS.join(', ')} (\`${ALWAYS}\` to show it on every line, ` +
|
|
36
|
+
`\`${TALKING}\` for a face that moves its mouth with the voice), or \`all\` / \`off\``);
|
|
37
|
+
}
|
|
38
|
+
if ((want.includes('off') || want.includes('all')) && want.length > 1) {
|
|
39
|
+
ctx.error('`off` and `all` stand alone: `[speakers off]`, `[speakers all]`');
|
|
40
|
+
}
|
|
41
|
+
let show;
|
|
42
|
+
if (want[0] === 'off' && want.length === 1)
|
|
43
|
+
show = [];
|
|
44
|
+
else if (want[0] === 'all' && want.length === 1)
|
|
45
|
+
show = [...exports.SPEAKER_WIDGETS];
|
|
46
|
+
else
|
|
47
|
+
show = [...exports.SPEAKER_WIDGETS, ALWAYS, TALKING].filter((w) => want.includes(w));
|
|
48
|
+
if (show.includes(ALWAYS))
|
|
49
|
+
show = show.filter((w) => w !== 'lower-third');
|
|
50
|
+
if (show.includes(TALKING))
|
|
51
|
+
show = show.filter((w) => w !== 'faces');
|
|
52
|
+
return { show };
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
}
|
package/plugins/video.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.video = video;
|
|
4
|
+
/**
|
|
5
|
+
* `[video CLIP]` / `[video CLIP start:2, for:4]` — a clip takes the screen and the
|
|
6
|
+
* scene waits for it. `video` is a built-in track; the sound must be a `file:`.
|
|
7
|
+
*/
|
|
8
|
+
function video() {
|
|
9
|
+
const allowed = ['hold', 'start', 'for', 'volume'];
|
|
10
|
+
return {
|
|
11
|
+
name: 'video',
|
|
12
|
+
tracks: ['video'],
|
|
13
|
+
trackRules: {
|
|
14
|
+
video(item, ctx) {
|
|
15
|
+
const ov = item.overrides;
|
|
16
|
+
const bad = Object.keys(ov).filter((k) => !allowed.includes(k));
|
|
17
|
+
if (item.action !== 'play' || !item.sound) {
|
|
18
|
+
ctx.error('`[video …]` plays one clip, e.g. `[video COVER]` — it can\'t be stopped or set; a clip ' +
|
|
19
|
+
'plays its length (trim with `start:`/`for:`)');
|
|
20
|
+
}
|
|
21
|
+
else if (ov.at !== undefined) {
|
|
22
|
+
ctx.error('a video takes the screen, so it can\'t overlap anything — drop `at:`');
|
|
23
|
+
}
|
|
24
|
+
else if (bad.length) {
|
|
25
|
+
ctx.error(`\`${bad.join(', ')}\` doesn't apply to a video clip — it takes \`start:\`, \`for:\` and \`vol:\``);
|
|
26
|
+
}
|
|
27
|
+
ov.hold = true;
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
finalize(scene, ctx) {
|
|
31
|
+
for (const item of scene.items) {
|
|
32
|
+
if (item.type !== 'track' || item.track !== 'video' || !item.sound)
|
|
33
|
+
continue;
|
|
34
|
+
const sound = scene.sounds[item.sound];
|
|
35
|
+
if (sound && !sound.file) {
|
|
36
|
+
ctx.problem(item, `\`${item.sound}\` is generated (\`prompt:\`), but \`[video …]\` needs a video file: ` +
|
|
37
|
+
`\`${item.sound}: { file: path/to/clip.mp4 }\``);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
}
|
package/schema.d.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keys and the values they take, per context. A plugin adds keys to any
|
|
3
|
+
* context; the parser validates and coerces every key against the result.
|
|
4
|
+
*/
|
|
5
|
+
export type KeyContext = 'character' | 'line' | 'span' | 'sound' | 'track' | 'play' | 'off' | 'set';
|
|
6
|
+
export declare const KEY_CONTEXTS: KeyContext[];
|
|
7
|
+
export type KeySpec = {
|
|
8
|
+
kind: 'string';
|
|
9
|
+
} | {
|
|
10
|
+
kind: 'int';
|
|
11
|
+
} | {
|
|
12
|
+
kind: 'number';
|
|
13
|
+
min: number;
|
|
14
|
+
max: number;
|
|
15
|
+
} | {
|
|
16
|
+
kind: 'bool';
|
|
17
|
+
}
|
|
18
|
+
/** seconds, or m:ss */
|
|
19
|
+
| {
|
|
20
|
+
kind: 'time';
|
|
21
|
+
}
|
|
22
|
+
/** one value from a list, or from a named enum plugins can extend */
|
|
23
|
+
| {
|
|
24
|
+
kind: 'enum';
|
|
25
|
+
values: string[] | {
|
|
26
|
+
named: string;
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
/** the name of a known track */
|
|
30
|
+
| {
|
|
31
|
+
kind: 'track';
|
|
32
|
+
}
|
|
33
|
+
/** a list of track names (`a,b` or `a+b`); `speech` means every track speech goes to */
|
|
34
|
+
| {
|
|
35
|
+
kind: 'tracks';
|
|
36
|
+
};
|
|
37
|
+
export type Schema = Record<string, KeySpec>;
|
|
38
|
+
export declare const str: () => KeySpec;
|
|
39
|
+
export declare const int: () => KeySpec;
|
|
40
|
+
export declare const num: (min: number, max: number) => KeySpec;
|
|
41
|
+
export declare const bool: () => KeySpec;
|
|
42
|
+
export declare const time: () => KeySpec;
|
|
43
|
+
export declare const oneOf: (...values: string[]) => KeySpec;
|
|
44
|
+
export declare const named: (name: string) => KeySpec;
|
|
45
|
+
export declare const track: () => KeySpec;
|
|
46
|
+
export declare const tracks: () => KeySpec;
|
|
47
|
+
/** The core grammar's keys. Everything else comes from plugins. */
|
|
48
|
+
export declare const CORE_SCHEMAS: Record<KeyContext, Schema>;
|
|
49
|
+
/** Tracks every scene has without declaring them. */
|
|
50
|
+
export declare const CORE_TRACKS: string[];
|
|
51
|
+
/** Lowercase words that can never name a track. */
|
|
52
|
+
export declare const CORE_RESERVED: string[];
|
|
53
|
+
/** Other spellings an author may use, mapped to the canonical key. */
|
|
54
|
+
export declare const ALIASES: Record<string, string>;
|
|
55
|
+
/** How a canonical key is shown back to an author. */
|
|
56
|
+
export declare function shown(key: string): string;
|
package/schema.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Keys and the values they take, per context. A plugin adds keys to any
|
|
4
|
+
* context; the parser validates and coerces every key against the result.
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.ALIASES = exports.CORE_RESERVED = exports.CORE_TRACKS = exports.CORE_SCHEMAS = exports.tracks = exports.track = exports.named = exports.oneOf = exports.time = exports.bool = exports.num = exports.int = exports.str = exports.KEY_CONTEXTS = void 0;
|
|
8
|
+
exports.shown = shown;
|
|
9
|
+
exports.KEY_CONTEXTS = [
|
|
10
|
+
'character',
|
|
11
|
+
'line',
|
|
12
|
+
'span',
|
|
13
|
+
'sound',
|
|
14
|
+
'track',
|
|
15
|
+
'play',
|
|
16
|
+
'off',
|
|
17
|
+
'set',
|
|
18
|
+
];
|
|
19
|
+
const str = () => ({ kind: 'string' });
|
|
20
|
+
exports.str = str;
|
|
21
|
+
const int = () => ({ kind: 'int' });
|
|
22
|
+
exports.int = int;
|
|
23
|
+
const num = (min, max) => ({ kind: 'number', min, max });
|
|
24
|
+
exports.num = num;
|
|
25
|
+
const bool = () => ({ kind: 'bool' });
|
|
26
|
+
exports.bool = bool;
|
|
27
|
+
const time = () => ({ kind: 'time' });
|
|
28
|
+
exports.time = time;
|
|
29
|
+
const oneOf = (...values) => ({ kind: 'enum', values });
|
|
30
|
+
exports.oneOf = oneOf;
|
|
31
|
+
const named = (name) => ({ kind: 'enum', values: { named: name } });
|
|
32
|
+
exports.named = named;
|
|
33
|
+
const track = () => ({ kind: 'track' });
|
|
34
|
+
exports.track = track;
|
|
35
|
+
const tracks = () => ({ kind: 'tracks' });
|
|
36
|
+
exports.tracks = tracks;
|
|
37
|
+
/** The core grammar's keys. Everything else comes from plugins. */
|
|
38
|
+
exports.CORE_SCHEMAS = {
|
|
39
|
+
character: {
|
|
40
|
+
persona: (0, exports.str)(),
|
|
41
|
+
voice: (0, exports.str)(),
|
|
42
|
+
speaker: (0, exports.str)(),
|
|
43
|
+
emotion: (0, exports.str)(),
|
|
44
|
+
track: (0, exports.track)(),
|
|
45
|
+
seed: (0, exports.int)(),
|
|
46
|
+
gain: (0, exports.num)(-60, 24),
|
|
47
|
+
},
|
|
48
|
+
line: { emotion: (0, exports.str)(), seed: (0, exports.int)(), track: (0, exports.track)(), gain: (0, exports.num)(-60, 24) },
|
|
49
|
+
span: { emotion: (0, exports.str)(), seed: (0, exports.int)(), gain: (0, exports.num)(-60, 24) },
|
|
50
|
+
sound: {
|
|
51
|
+
prompt: (0, exports.str)(),
|
|
52
|
+
file: (0, exports.str)(),
|
|
53
|
+
duration: (0, exports.num)(0.05, 3600),
|
|
54
|
+
seed: (0, exports.int)(),
|
|
55
|
+
loop: (0, exports.bool)(),
|
|
56
|
+
level: (0, exports.num)(-80, 0),
|
|
57
|
+
start: (0, exports.time)(),
|
|
58
|
+
layer: (0, exports.track)(),
|
|
59
|
+
track: (0, exports.track)(),
|
|
60
|
+
},
|
|
61
|
+
track: {
|
|
62
|
+
kind: (0, exports.oneOf)('bed', 'oneshot'),
|
|
63
|
+
level: (0, exports.num)(-80, 0),
|
|
64
|
+
volume: (0, exports.num)(0, 4),
|
|
65
|
+
gain: (0, exports.num)(-60, 24),
|
|
66
|
+
pan: (0, exports.num)(-1, 1),
|
|
67
|
+
duck: (0, exports.num)(-60, 0),
|
|
68
|
+
duck_under: (0, exports.tracks)(),
|
|
69
|
+
fade_in: (0, exports.num)(0, 60),
|
|
70
|
+
fade_out: (0, exports.num)(0, 60),
|
|
71
|
+
xfade: (0, exports.num)(0, 30),
|
|
72
|
+
tail: (0, exports.num)(0, 60),
|
|
73
|
+
loop: (0, exports.bool)(),
|
|
74
|
+
exclusive: (0, exports.bool)(),
|
|
75
|
+
},
|
|
76
|
+
play: {
|
|
77
|
+
at: (0, exports.num)(-3600, 36000),
|
|
78
|
+
volume: (0, exports.num)(0, 4),
|
|
79
|
+
pan: (0, exports.num)(-1, 1),
|
|
80
|
+
fade_in: (0, exports.num)(0, 60),
|
|
81
|
+
fade_out: (0, exports.num)(0, 60),
|
|
82
|
+
duration: (0, exports.num)(0.05, 3600),
|
|
83
|
+
seed: (0, exports.int)(),
|
|
84
|
+
start: (0, exports.time)(),
|
|
85
|
+
for: (0, exports.time)(),
|
|
86
|
+
hold: (0, exports.bool)(),
|
|
87
|
+
loop: (0, exports.bool)(),
|
|
88
|
+
noloop: (0, exports.bool)(),
|
|
89
|
+
level: (0, exports.num)(-80, 0),
|
|
90
|
+
xfade: (0, exports.num)(0, 30),
|
|
91
|
+
},
|
|
92
|
+
off: { fade_out: (0, exports.num)(0, 60) },
|
|
93
|
+
set: { volume: (0, exports.num)(0, 4), ramp: (0, exports.num)(0, 600), at: (0, exports.num)(-3600, 36000) },
|
|
94
|
+
};
|
|
95
|
+
/** Tracks every scene has without declaring them. */
|
|
96
|
+
exports.CORE_TRACKS = ['dialogue', 'foreground', 'ambience', 'music', 'transition'];
|
|
97
|
+
/** Lowercase words that can never name a track. */
|
|
98
|
+
exports.CORE_RESERVED = ['pause', 'off', 'track'];
|
|
99
|
+
/** Other spellings an author may use, mapped to the canonical key. */
|
|
100
|
+
exports.ALIASES = {
|
|
101
|
+
vol: 'volume',
|
|
102
|
+
fadein: 'fade_in',
|
|
103
|
+
fadeout: 'fade_out',
|
|
104
|
+
dur: 'duration',
|
|
105
|
+
from: 'start',
|
|
106
|
+
length: 'for',
|
|
107
|
+
};
|
|
108
|
+
/** How a canonical key is shown back to an author. */
|
|
109
|
+
function shown(key) {
|
|
110
|
+
if (key === 'volume')
|
|
111
|
+
return 'vol';
|
|
112
|
+
return key.replace(/_/g, '-');
|
|
113
|
+
}
|
package/segments.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export interface RawSegment {
|
|
2
|
+
type: 'text' | 'break' | 'sfx';
|
|
3
|
+
text?: string;
|
|
4
|
+
seconds?: number;
|
|
5
|
+
sound?: string;
|
|
6
|
+
/** span override text (`whisper, reverb:hall`) or sfx play text */
|
|
7
|
+
raw?: string;
|
|
8
|
+
}
|
|
9
|
+
export declare function splitSegments(text: string): RawSegment[];
|
package/segments.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.splitSegments = splitSegments;
|
|
4
|
+
/**
|
|
5
|
+
* Markers inside one line of dialogue:
|
|
6
|
+
*
|
|
7
|
+
* DAN: calm about it <break 0.5> and then it hit me.
|
|
8
|
+
* DAN: I hear you, {excited: but this changes everything!}
|
|
9
|
+
* DAN: {whisper, reverb:cathedral: come here} <sfx DOOR> {firm: we need to talk}.
|
|
10
|
+
*/
|
|
11
|
+
// A span's prefix is a list of `key` / `key:value` tokens ended by ONE colon
|
|
12
|
+
// before the text. It is matched token by token: `reverb:cathedral` is itself
|
|
13
|
+
// a key:value, so "everything up to the first colon" would cut it in half.
|
|
14
|
+
const TOKEN = String.raw `[a-zA-Z_][\w.-]*(?::[a-zA-Z0-9_.+-]+)?`;
|
|
15
|
+
const PREFIX = String.raw `(?:${TOKEN}\s*,\s*)*${TOKEN}`;
|
|
16
|
+
const INLINE = String.raw `<break\s+([\d.]+)>|<([\d.]+)>|<sfx\s+([A-Z][A-Z0-9_-]*)(?:\s+([^>]*))?>|\{(${PREFIX}):([^{}]*)\}`;
|
|
17
|
+
function splitSegments(text) {
|
|
18
|
+
const re = new RegExp(INLINE, 'g');
|
|
19
|
+
const out = [];
|
|
20
|
+
let pos = 0;
|
|
21
|
+
let m;
|
|
22
|
+
let any = false;
|
|
23
|
+
while ((m = re.exec(text))) {
|
|
24
|
+
any = true;
|
|
25
|
+
const before = text.slice(pos, m.index);
|
|
26
|
+
if (before.trim())
|
|
27
|
+
out.push({ type: 'text', text: before.trim() });
|
|
28
|
+
const [, breakA, breakB, sfx, sfxArgs, spanKeys, spanText] = m;
|
|
29
|
+
if (breakA !== undefined || breakB !== undefined) {
|
|
30
|
+
out.push({ type: 'break', seconds: Number(breakA ?? breakB) });
|
|
31
|
+
}
|
|
32
|
+
else if (sfx !== undefined) {
|
|
33
|
+
out.push({ type: 'sfx', sound: sfx, raw: sfxArgs ?? '' });
|
|
34
|
+
}
|
|
35
|
+
else if (spanText.trim()) {
|
|
36
|
+
out.push({ type: 'text', text: spanText.trim(), raw: spanKeys });
|
|
37
|
+
}
|
|
38
|
+
pos = m.index + m[0].length;
|
|
39
|
+
}
|
|
40
|
+
if (!any)
|
|
41
|
+
return [{ type: 'text', text }];
|
|
42
|
+
const rest = text.slice(pos);
|
|
43
|
+
if (rest.trim())
|
|
44
|
+
out.push({ type: 'text', text: rest.trim() });
|
|
45
|
+
const folded = foldPunctuation(out);
|
|
46
|
+
return folded.length ? folded : [{ type: 'text', text }];
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* A fragment of bare punctuation beside a span — the "." in `{firm: talk}.` —
|
|
50
|
+
* joins the text before it rather than being spoken on its own.
|
|
51
|
+
*/
|
|
52
|
+
function foldPunctuation(segments) {
|
|
53
|
+
const out = [];
|
|
54
|
+
for (const seg of segments) {
|
|
55
|
+
const prev = out[out.length - 1];
|
|
56
|
+
if (seg.type === 'text' && !/[\p{L}\p{N}]/u.test(seg.text) && prev?.type === 'text') {
|
|
57
|
+
out[out.length - 1] = { ...prev, text: prev.text + seg.text };
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
out.push(seg);
|
|
61
|
+
}
|
|
62
|
+
return out;
|
|
63
|
+
}
|
package/suggest.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* "Did you mean" — closest option by Ratcliff/Obershelp similarity, the same
|
|
3
|
+
* measure as Python's difflib, so suggestions match across implementations.
|
|
4
|
+
*/
|
|
5
|
+
export declare function similarity(a: string, b: string): number;
|
|
6
|
+
export declare function closest(word: string, options: Iterable<string>, cutoff?: number): string | null;
|
|
7
|
+
export declare function didYouMean(word: string, options: Iterable<string>, show?: (s: string) => string): string;
|
package/suggest.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* "Did you mean" — closest option by Ratcliff/Obershelp similarity, the same
|
|
4
|
+
* measure as Python's difflib, so suggestions match across implementations.
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.similarity = similarity;
|
|
8
|
+
exports.closest = closest;
|
|
9
|
+
exports.didYouMean = didYouMean;
|
|
10
|
+
function matching(a, b) {
|
|
11
|
+
if (!a.length || !b.length)
|
|
12
|
+
return 0;
|
|
13
|
+
let best = 0;
|
|
14
|
+
let ai = 0;
|
|
15
|
+
let bi = 0;
|
|
16
|
+
let prev = new Array(b.length + 1).fill(0);
|
|
17
|
+
for (let i = 1; i <= a.length; i++) {
|
|
18
|
+
const cur = new Array(b.length + 1).fill(0);
|
|
19
|
+
for (let j = 1; j <= b.length; j++) {
|
|
20
|
+
if (a[i - 1] === b[j - 1]) {
|
|
21
|
+
cur[j] = prev[j - 1] + 1;
|
|
22
|
+
if (cur[j] > best) {
|
|
23
|
+
best = cur[j];
|
|
24
|
+
ai = i - best;
|
|
25
|
+
bi = j - best;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
prev = cur;
|
|
30
|
+
}
|
|
31
|
+
if (!best)
|
|
32
|
+
return 0;
|
|
33
|
+
return (best +
|
|
34
|
+
matching(a.slice(0, ai), b.slice(0, bi)) +
|
|
35
|
+
matching(a.slice(ai + best), b.slice(bi + best)));
|
|
36
|
+
}
|
|
37
|
+
function similarity(a, b) {
|
|
38
|
+
const total = a.length + b.length;
|
|
39
|
+
return total ? (2 * matching(a, b)) / total : 1;
|
|
40
|
+
}
|
|
41
|
+
function closest(word, options, cutoff = 0.6) {
|
|
42
|
+
let best = null;
|
|
43
|
+
let score = cutoff;
|
|
44
|
+
for (const o of options) {
|
|
45
|
+
const s = similarity(word, o);
|
|
46
|
+
if (s >= score && (best === null || s > score)) {
|
|
47
|
+
best = o;
|
|
48
|
+
score = s;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return best;
|
|
52
|
+
}
|
|
53
|
+
function didYouMean(word, options, show = (s) => s) {
|
|
54
|
+
const m = closest(word, options);
|
|
55
|
+
return m ? `. Did you mean \`${show(m)}\`?` : '';
|
|
56
|
+
}
|
package/types.d.ts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/** Where a play sits on the clock, when it isn't simply "here in the flow". */
|
|
2
|
+
export interface At {
|
|
3
|
+
/** `offset`: seconds after this point (`at:+2`); `absolute`: from the scene start (`at:18.5`). */
|
|
4
|
+
mode: 'offset' | 'absolute';
|
|
5
|
+
seconds: number;
|
|
6
|
+
}
|
|
7
|
+
export type Value = string | number | boolean | string[] | At;
|
|
8
|
+
/** A declaration's settings or an item's overrides, keys normalised to snake_case. */
|
|
9
|
+
export type Config = Record<string, Value>;
|
|
10
|
+
export type Segment = {
|
|
11
|
+
type: 'text';
|
|
12
|
+
text: string;
|
|
13
|
+
overrides: Config;
|
|
14
|
+
} | {
|
|
15
|
+
type: 'break';
|
|
16
|
+
seconds: number;
|
|
17
|
+
} | {
|
|
18
|
+
type: 'sfx';
|
|
19
|
+
sound: string;
|
|
20
|
+
overrides: Config;
|
|
21
|
+
};
|
|
22
|
+
interface ItemBase {
|
|
23
|
+
/** 1-based line in the source. */
|
|
24
|
+
line: number;
|
|
25
|
+
}
|
|
26
|
+
/** `NAME[overrides]: text` */
|
|
27
|
+
export interface LineItem extends ItemBase {
|
|
28
|
+
type: 'line';
|
|
29
|
+
speaker: string;
|
|
30
|
+
text: string;
|
|
31
|
+
overrides: Config;
|
|
32
|
+
segments: Segment[];
|
|
33
|
+
}
|
|
34
|
+
/** `[pause N]` */
|
|
35
|
+
export interface PauseItem extends ItemBase {
|
|
36
|
+
type: 'pause';
|
|
37
|
+
seconds: number;
|
|
38
|
+
}
|
|
39
|
+
/** `[SOUND …]`, played on its default track. */
|
|
40
|
+
export interface CueItem extends ItemBase {
|
|
41
|
+
type: 'cue';
|
|
42
|
+
sound: string;
|
|
43
|
+
track: string;
|
|
44
|
+
overrides: Config;
|
|
45
|
+
}
|
|
46
|
+
/** `[track SOUND …]`, `[track off]`, `[track key:value …]`, `[music: file …]` */
|
|
47
|
+
export interface TrackItem extends ItemBase {
|
|
48
|
+
type: 'track';
|
|
49
|
+
track: string;
|
|
50
|
+
action: 'play' | 'off' | 'set';
|
|
51
|
+
sound: string | null;
|
|
52
|
+
/** only for the `[music: file …]` form, which names a file instead of a sound */
|
|
53
|
+
file?: string;
|
|
54
|
+
overrides: Config;
|
|
55
|
+
}
|
|
56
|
+
/** A silent, timeless marker produced by a directive plugin, e.g. `[slide intro]`. */
|
|
57
|
+
export interface MarkItem extends ItemBase {
|
|
58
|
+
type: 'mark';
|
|
59
|
+
kind: string;
|
|
60
|
+
data: Record<string, unknown>;
|
|
61
|
+
}
|
|
62
|
+
export type Item = LineItem | PauseItem | CueItem | TrackItem | MarkItem;
|
|
63
|
+
export interface Problem {
|
|
64
|
+
line: number | null;
|
|
65
|
+
/** the trimmed source line */
|
|
66
|
+
text: string | null;
|
|
67
|
+
message: string;
|
|
68
|
+
}
|
|
69
|
+
export interface Declaration {
|
|
70
|
+
line: number;
|
|
71
|
+
/** set when the declaration came from an `@include` (the line is the include's) */
|
|
72
|
+
include?: string;
|
|
73
|
+
}
|
|
74
|
+
export interface SceneIndex {
|
|
75
|
+
character: Record<string, Declaration>;
|
|
76
|
+
sound: Record<string, Declaration>;
|
|
77
|
+
track: Record<string, Declaration>;
|
|
78
|
+
/** first line each speaker speaks */
|
|
79
|
+
speaker: Record<string, Declaration>;
|
|
80
|
+
/** first line each directly-named file appears */
|
|
81
|
+
file: Record<string, Declaration>;
|
|
82
|
+
}
|
|
83
|
+
export interface Scene {
|
|
84
|
+
characters: Record<string, Config>;
|
|
85
|
+
sounds: Record<string, Config>;
|
|
86
|
+
/** tracks declared in `@tracks` (or included); built-in tracks need no entry */
|
|
87
|
+
tracks: Record<string, Config>;
|
|
88
|
+
items: Item[];
|
|
89
|
+
/** empty unless parsed with `strict: false` */
|
|
90
|
+
problems: Problem[];
|
|
91
|
+
index: SceneIndex;
|
|
92
|
+
}
|
|
93
|
+
export {};
|
package/types.js
ADDED
package/values.d.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reading `key: value` lists and checking them against a schema.
|
|
3
|
+
*
|
|
4
|
+
* Three surface styles share one validator:
|
|
5
|
+
* config `{ persona: dan, prompt: "rain, distant thunder" }` commas, quotes allowed
|
|
6
|
+
* overrides `[excited, seed:303]` commas; a bareword is an emotion
|
|
7
|
+
* play `[GLASS vol:0.9, pan:0.2 hold]` commas or spaces; a bareword is a flag
|
|
8
|
+
*/
|
|
9
|
+
import { Registry } from './plugin';
|
|
10
|
+
import { Schema } from './schema';
|
|
11
|
+
import { Config } from './types';
|
|
12
|
+
export interface Pair {
|
|
13
|
+
/** canonical key */
|
|
14
|
+
key: string;
|
|
15
|
+
/** what the author typed */
|
|
16
|
+
typed: string;
|
|
17
|
+
/** raw text, or `true` for a bareword flag */
|
|
18
|
+
value: string | true;
|
|
19
|
+
}
|
|
20
|
+
/** Split on commas outside quotes, dropping the quote characters themselves. */
|
|
21
|
+
export declare function splitTop(raw: string): string[];
|
|
22
|
+
export declare function readConfig(raw: string, errs: string[]): Pair[];
|
|
23
|
+
export declare function readOverrides(raw: string, errs: string[]): Pair[];
|
|
24
|
+
export declare function readPlay(raw: string): Pair[];
|
|
25
|
+
export declare function seconds(v: string): number;
|
|
26
|
+
export interface CheckOptions {
|
|
27
|
+
reg: Registry;
|
|
28
|
+
/** every track name in scope: built-in, declared and included */
|
|
29
|
+
tracks: Set<string>;
|
|
30
|
+
/** "track `crowd`", "a [music …] play" — used in unknown-key messages */
|
|
31
|
+
what: string;
|
|
32
|
+
/** the context the schema is for, for context-specific hints */
|
|
33
|
+
context?: string;
|
|
34
|
+
}
|
|
35
|
+
/** Validate and coerce pairs against a schema. Problems go to `errs`; bad keys are left out. */
|
|
36
|
+
export declare function check(pairs: Pair[], schema: Schema, opts: CheckOptions, errs: string[]): Config;
|