squiffy-compiler 7.0.0-beta.7 → 7.0.0-beta.8
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/dist/analyse.d.ts +9 -1
- package/dist/analyse.js +114 -9
- package/dist/compiler.d.ts +4 -3
- package/dist/compiler.js +2 -2
- package/dist/emit.d.ts +11 -1
- package/dist/emit.js +18 -0
- package/dist/infix-expressions.d.ts +19 -0
- package/dist/infix-expressions.js +713 -0
- package/dist/language-service.js +12 -5
- package/dist/node.d.ts +2 -1
- package/dist/node.js +1 -0
- package/dist/parse.d.ts +1 -0
- package/dist/parse.js +15 -0
- package/dist/project.d.ts +29 -0
- package/dist/project.js +95 -0
- package/dist/quest/convert.js +29 -2
- package/dist/quest/scripts.js +6 -6
- package/dist/quest/text.js +17 -8
- package/dist/section-labels.d.ts +4 -0
- package/dist/section-labels.js +64 -0
- package/dist/setters.js +5 -5
- package/dist/story-graph.d.ts +2 -0
- package/dist/story-graph.js +61 -16
- package/dist/syntax.d.ts +11 -3
- package/dist/templates.d.ts +6 -0
- package/dist/templates.js +128 -0
- package/dist/text.d.ts +3 -1
- package/dist/text.js +28 -1
- package/dist/twine/convert.d.ts +35 -0
- package/dist/twine/convert.js +209 -19
- package/dist/twine/expression.d.ts +1 -0
- package/dist/twine/expression.js +7 -1
- package/dist/twine/harlowe.js +99 -27
- package/dist/twine/settings.d.ts +9 -0
- package/dist/twine/settings.js +322 -0
- package/dist/twine/sugarcube.js +92 -33
- package/package.json +9 -2
- package/syntaxes/squiffy.tmLanguage.json +1 -1
package/dist/language-service.js
CHANGED
|
@@ -9,7 +9,7 @@ import { analyse, ParseCache } from "./analyse.js";
|
|
|
9
9
|
import { getStoryFiles, globToRegExp, parseProjectFile, PROJECT_FILENAME, storyFileList } from "./project.js";
|
|
10
10
|
import { convertV5Syntax, findV5Syntax } from "./v5.js";
|
|
11
11
|
import { findUnquotedText, splitWithOffsets } from "./setters.js";
|
|
12
|
-
import { buildStoryGraph, storyGraphDiagnostics } from "./story-graph.js";
|
|
12
|
+
import { buildStoryGraph, firstScreenSoundDiagnostics, storyGraphDiagnostics } from "./story-graph.js";
|
|
13
13
|
const sameSymbol = (a, b) => a.kind == b.kind && a.name == b.name && (a.kind == "section" || a.section == b.section);
|
|
14
14
|
const usageRange = (usage) => (usage.link ?? usage.embed ?? usage.goto ?? usage.deactivate).range;
|
|
15
15
|
// Directives, for completion, with what they do
|
|
@@ -27,6 +27,8 @@ const DIRECTIVES = [
|
|
|
27
27
|
["music", "Plays music until it's changed, or stops it with @music stop"],
|
|
28
28
|
["sound", "Plays a sound once, or stops the sounds playing with @sound stop"],
|
|
29
29
|
["class", "Puts classes on the story while this section is shown, for its stylesheet"],
|
|
30
|
+
["transition", "How this section or passage appears, such as @transition fade 1s"],
|
|
31
|
+
["leave", "How the old text goes when this clears the screen, such as @leave fade 1s"],
|
|
30
32
|
["ui", "JavaScript for the story's user interface"],
|
|
31
33
|
];
|
|
32
34
|
const contains = (range, pos) => comparePositions(range.start, pos) <= 0 && comparePositions(pos, range.end) <= 0;
|
|
@@ -289,15 +291,16 @@ export function createLanguageService(options) {
|
|
|
289
291
|
}
|
|
290
292
|
function getGraph(index) {
|
|
291
293
|
if (!index.graph) {
|
|
292
|
-
const
|
|
294
|
+
const source = {
|
|
293
295
|
files: index.files,
|
|
294
296
|
sections: index.sections,
|
|
295
297
|
nodeNames: index.nodeNames,
|
|
296
298
|
usages: index.usages,
|
|
297
299
|
start: index.analysis.start,
|
|
298
|
-
resolve: usage => resolveUsage(index, usage),
|
|
299
|
-
}
|
|
300
|
-
|
|
300
|
+
resolve: (usage) => resolveUsage(index, usage),
|
|
301
|
+
};
|
|
302
|
+
const graph = buildStoryGraph(source);
|
|
303
|
+
index.graph = { graph, diagnostics: [...storyGraphDiagnostics(graph), ...firstScreenSoundDiagnostics(source)] };
|
|
301
304
|
}
|
|
302
305
|
return index.graph;
|
|
303
306
|
}
|
|
@@ -615,6 +618,10 @@ export function createLanguageService(options) {
|
|
|
615
618
|
if ((match = /\{\{~?\s*deactivate\s+["']([^"']*)$/.exec(prefix))) {
|
|
616
619
|
return passageItems(match[1]);
|
|
617
620
|
}
|
|
621
|
+
// A text box that goes somewhere when Enter is pressed (#454)
|
|
622
|
+
if ((match = /<input\b[^>]*\sdata-(section|passage)\s*=\s*["']([^"']*)$/i.exec(prefix))) {
|
|
623
|
+
return match[1].toLowerCase() == "section" ? sectionItems(match[2]) : passageItems(match[2]);
|
|
624
|
+
}
|
|
618
625
|
if ((match = /\[\[[^\]]*\]\]\(([^),]*)$/.exec(prefix))) {
|
|
619
626
|
return sectionItems(match[1]);
|
|
620
627
|
}
|
package/dist/node.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type PageStrings, type Panel } from "./project.js";
|
|
1
|
+
import { type PageStrings, type Panel, type PlayerSetting } from "./project.js";
|
|
2
2
|
export interface StoryOnDisk {
|
|
3
3
|
mainFile: string;
|
|
4
4
|
storyFiles?: string[];
|
|
@@ -10,6 +10,7 @@ export interface StoryOnDisk {
|
|
|
10
10
|
saveButton?: boolean;
|
|
11
11
|
projectId?: string;
|
|
12
12
|
panels?: Panel[];
|
|
13
|
+
settings?: PlayerSetting[];
|
|
13
14
|
}
|
|
14
15
|
export declare class StoryNotFoundError extends Error {
|
|
15
16
|
}
|
package/dist/node.js
CHANGED
|
@@ -84,6 +84,7 @@ async function folderStory(projectFile, options) {
|
|
|
84
84
|
...(project.saveButton !== undefined ? { saveButton: project.saveButton } : {}),
|
|
85
85
|
...(project.id !== undefined ? { projectId: project.id } : {}),
|
|
86
86
|
...(project.panels !== undefined ? { panels: project.panels } : {}),
|
|
87
|
+
...(project.settings !== undefined ? { settings: project.settings } : {}),
|
|
87
88
|
};
|
|
88
89
|
}
|
|
89
90
|
// Finds the story to build from what the author gave: a folder with a squiffy.json in it, or a
|
package/dist/parse.d.ts
CHANGED
package/dist/parse.js
CHANGED
|
@@ -16,6 +16,7 @@ export const lineRegexes = {
|
|
|
16
16
|
music: /^@music (.*)$/,
|
|
17
17
|
sound: /^@sound (.*)$/,
|
|
18
18
|
class: /^@class (.*)$/,
|
|
19
|
+
transition: /^@(transition|leave) (\S+)(?: (\S+))?$/,
|
|
19
20
|
};
|
|
20
21
|
// Parses one .squiffy file into a syntax tree. This never fails: problems such as a passage
|
|
21
22
|
// with no section are reported by analyse(), which knows how the file fits into the story.
|
|
@@ -190,6 +191,20 @@ export function parse(text, filename) {
|
|
|
190
191
|
haveSection = true;
|
|
191
192
|
add({ type: "class", value: match.class[1], valueRange: valueRange(i, match.class[1]), range });
|
|
192
193
|
}
|
|
194
|
+
else if (match.transition) {
|
|
195
|
+
if (!passage)
|
|
196
|
+
haveSection = true;
|
|
197
|
+
const [, directive, value, time] = match.transition;
|
|
198
|
+
const start = line.indexOf(value, line.indexOf("@") + directive.length + 1);
|
|
199
|
+
add({
|
|
200
|
+
type: "transition",
|
|
201
|
+
directive: directive,
|
|
202
|
+
value,
|
|
203
|
+
valueRange: { start: { line: i, character: start }, end: { line: i, character: start + value.length } },
|
|
204
|
+
...(time === undefined ? {} : { time }),
|
|
205
|
+
range,
|
|
206
|
+
});
|
|
207
|
+
}
|
|
193
208
|
else if (match.attributes || match.unset) {
|
|
194
209
|
haveSection = true;
|
|
195
210
|
const value = (match.attributes ?? match.unset)[1];
|
package/dist/project.d.ts
CHANGED
|
@@ -11,7 +11,32 @@ export interface ProjectFile {
|
|
|
11
11
|
saveButton?: boolean;
|
|
12
12
|
strings?: PageStrings;
|
|
13
13
|
panels?: Panel[];
|
|
14
|
+
settings?: PlayerSetting[];
|
|
14
15
|
}
|
|
16
|
+
export type PlayerSetting = PlayerSettingBase & {
|
|
17
|
+
type: "toggle";
|
|
18
|
+
default?: boolean;
|
|
19
|
+
} | PlayerSettingBase & {
|
|
20
|
+
type: "list";
|
|
21
|
+
options: PlayerSettingOption[];
|
|
22
|
+
default?: string;
|
|
23
|
+
} | PlayerSettingBase & {
|
|
24
|
+
type: "range";
|
|
25
|
+
min: number;
|
|
26
|
+
max: number;
|
|
27
|
+
step?: number;
|
|
28
|
+
default?: number;
|
|
29
|
+
};
|
|
30
|
+
interface PlayerSettingBase {
|
|
31
|
+
name: string;
|
|
32
|
+
label: string;
|
|
33
|
+
description?: string;
|
|
34
|
+
}
|
|
35
|
+
export interface PlayerSettingOption {
|
|
36
|
+
value: string;
|
|
37
|
+
label: string;
|
|
38
|
+
}
|
|
39
|
+
export declare const PLAYER_SETTING_TYPES: readonly ["toggle", "list", "range"];
|
|
15
40
|
export declare const PANEL_ICONS: readonly ["bag", "person", "map", "help", "info", "list", "book", "star", "heart"];
|
|
16
41
|
export type PanelIcon = typeof PANEL_ICONS[number];
|
|
17
42
|
export interface Panel {
|
|
@@ -53,6 +78,8 @@ export declare const PAGE_STRINGS: {
|
|
|
53
78
|
wrongStory: string;
|
|
54
79
|
notASave: string;
|
|
55
80
|
cantSave: string;
|
|
81
|
+
settings: string;
|
|
82
|
+
resetSettings: string;
|
|
56
83
|
};
|
|
57
84
|
export interface MapPositions {
|
|
58
85
|
sections: Record<string, [number, number]>;
|
|
@@ -90,9 +117,11 @@ export declare function formatProjectFile(project: {
|
|
|
90
117
|
map?: MapPositions;
|
|
91
118
|
id?: string;
|
|
92
119
|
panels?: Panel[];
|
|
120
|
+
settings?: PlayerSetting[];
|
|
93
121
|
}): string;
|
|
94
122
|
export declare function updateProjectFile(text: string, changes: {
|
|
95
123
|
main?: string;
|
|
96
124
|
exclude?: string[];
|
|
97
125
|
map?: MapPositions;
|
|
98
126
|
}): string | undefined;
|
|
127
|
+
export {};
|
package/dist/project.js
CHANGED
|
@@ -12,6 +12,7 @@ export const PROJECT_FILENAME = "squiffy.json";
|
|
|
12
12
|
export const PROJECT_FILE_VERSION = 1;
|
|
13
13
|
// Where the project file's JSON schema is published
|
|
14
14
|
export const PROJECT_SCHEMA_URL = "https://squiffystory.com/schema/squiffy.json";
|
|
15
|
+
export const PLAYER_SETTING_TYPES = ["toggle", "list", "range"];
|
|
15
16
|
// The icons a panel's button can have. The runtime has the same list (PANEL_ICONS).
|
|
16
17
|
export const PANEL_ICONS = ["bag", "person", "map", "help", "info", "list", "book", "star", "heart"];
|
|
17
18
|
// The published page's text in English, which squiffy.json's "strings" can replace
|
|
@@ -48,6 +49,9 @@ export const PAGE_STRINGS = {
|
|
|
48
49
|
wrongStory: "This saved game is for a different story.",
|
|
49
50
|
notASave: "This file isn't a saved game.",
|
|
50
51
|
cantSave: "The game couldn't be saved. Try Save to file instead.",
|
|
52
|
+
// The Settings dialog, for a story with "settings"
|
|
53
|
+
settings: "Settings",
|
|
54
|
+
resetSettings: "Reset to defaults",
|
|
51
55
|
};
|
|
52
56
|
// Parses and checks a squiffy.json. An error is a sentence to show the author as is.
|
|
53
57
|
export function parseProjectFile(text) {
|
|
@@ -110,6 +114,9 @@ export function parseProjectFile(text) {
|
|
|
110
114
|
const panels = parsePanels(data.panels);
|
|
111
115
|
if (typeof panels == "string")
|
|
112
116
|
return fail(panels);
|
|
117
|
+
const settings = parseSettings(data.settings);
|
|
118
|
+
if (typeof settings == "string")
|
|
119
|
+
return fail(settings);
|
|
113
120
|
const map = parseMapPositions(data.map);
|
|
114
121
|
return {
|
|
115
122
|
success: true,
|
|
@@ -121,6 +128,7 @@ export function parseProjectFile(text) {
|
|
|
121
128
|
...(saveButton !== undefined ? { saveButton } : {}),
|
|
122
129
|
...(strings !== undefined ? { strings: pickPageStrings(strings) } : {}),
|
|
123
130
|
...(panels !== undefined ? { panels } : {}),
|
|
131
|
+
...(settings !== undefined ? { settings } : {}),
|
|
124
132
|
},
|
|
125
133
|
};
|
|
126
134
|
}
|
|
@@ -162,6 +170,91 @@ function parsePanels(value) {
|
|
|
162
170
|
}
|
|
163
171
|
return panels;
|
|
164
172
|
}
|
|
173
|
+
// squiffy.json's "settings", or an error to show the author
|
|
174
|
+
function parseSettings(value) {
|
|
175
|
+
if (value === undefined)
|
|
176
|
+
return undefined;
|
|
177
|
+
const example = "{ \"name\": \"warnings\", \"type\": \"toggle\", \"label\": \"Content warnings\" }";
|
|
178
|
+
if (!Array.isArray(value))
|
|
179
|
+
return `${PROJECT_FILENAME}'s "settings" should be a list, such as [${example}].`;
|
|
180
|
+
const settings = [];
|
|
181
|
+
const names = new Set();
|
|
182
|
+
for (const [i, item] of value.entries()) {
|
|
183
|
+
const which = `${PROJECT_FILENAME}'s setting ${i + 1}`;
|
|
184
|
+
if (!isObject(item))
|
|
185
|
+
return `${which} should be an object, such as ${example}.`;
|
|
186
|
+
const name = typeof item.name == "string" ? item.name.trim() : "";
|
|
187
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(name)) {
|
|
188
|
+
return `${which} needs a "name" of letters, digits and underscores, such as "warnings", which the story reads as settings.warnings.`;
|
|
189
|
+
}
|
|
190
|
+
if (names.has(name))
|
|
191
|
+
return `${PROJECT_FILENAME} has more than one setting called "${name}".`;
|
|
192
|
+
names.add(name);
|
|
193
|
+
const label = typeof item.label == "string" && item.label.trim() ? item.label.trim() : name;
|
|
194
|
+
if (item.description !== undefined && typeof item.description != "string") {
|
|
195
|
+
return `${which}'s "description" should be text.`;
|
|
196
|
+
}
|
|
197
|
+
const base = {
|
|
198
|
+
name, label,
|
|
199
|
+
...(typeof item.description == "string" && item.description.trim() ? { description: item.description.trim() } : {}),
|
|
200
|
+
};
|
|
201
|
+
const type = item.type ?? "toggle";
|
|
202
|
+
if (type == "toggle") {
|
|
203
|
+
if (item.default !== undefined && typeof item.default != "boolean") {
|
|
204
|
+
return `${which}'s "default" should be true or false.`;
|
|
205
|
+
}
|
|
206
|
+
settings.push({ ...base, type: "toggle", ...(item.default !== undefined ? { default: item.default } : {}) });
|
|
207
|
+
}
|
|
208
|
+
else if (type == "list") {
|
|
209
|
+
const options = Array.isArray(item.options) ? item.options.map(parseSettingOption) : null;
|
|
210
|
+
if (!options || !options.length || options.some(option => !option)) {
|
|
211
|
+
return `${which} needs "options" for the player to choose from, such as ["Small", "Medium", "Large"], or objects with a "value" and a "label".`;
|
|
212
|
+
}
|
|
213
|
+
const values = options.map(option => option.value);
|
|
214
|
+
if (new Set(values).size != values.length)
|
|
215
|
+
return `${which} has the same option more than once.`;
|
|
216
|
+
if (item.default !== undefined && !values.includes(item.default)) {
|
|
217
|
+
return `${which}'s "default" should be one of its options' values.`;
|
|
218
|
+
}
|
|
219
|
+
settings.push({
|
|
220
|
+
...base, type: "list", options: options,
|
|
221
|
+
...(item.default !== undefined ? { default: item.default } : {}),
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
else if (type == "range") {
|
|
225
|
+
const isNumber = (n) => typeof n == "number" && Number.isFinite(n);
|
|
226
|
+
if (!isNumber(item.min) || !isNumber(item.max) || item.min > item.max) {
|
|
227
|
+
return `${which} needs a "min" and a "max", such as "min": 1, "max": 5.`;
|
|
228
|
+
}
|
|
229
|
+
if (item.step !== undefined && (!isNumber(item.step) || item.step <= 0)) {
|
|
230
|
+
return `${which}'s "step" should be a number above 0.`;
|
|
231
|
+
}
|
|
232
|
+
if (item.default !== undefined && (!isNumber(item.default) || item.default < item.min || item.default > item.max)) {
|
|
233
|
+
return `${which}'s "default" should be a number from its "min" to its "max".`;
|
|
234
|
+
}
|
|
235
|
+
settings.push({
|
|
236
|
+
...base, type: "range", min: item.min, max: item.max,
|
|
237
|
+
...(item.step !== undefined ? { step: item.step } : {}),
|
|
238
|
+
...(item.default !== undefined ? { default: item.default } : {}),
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
else {
|
|
242
|
+
return `${which}'s "type" should be ${PLAYER_SETTING_TYPES.map(t => `"${t}"`).join(", ")}.`;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
return settings;
|
|
246
|
+
}
|
|
247
|
+
// A list setting's option: text, which is both its value and its label, or { value, label }
|
|
248
|
+
function parseSettingOption(option) {
|
|
249
|
+
if (typeof option == "string" && option.trim())
|
|
250
|
+
return { value: option.trim(), label: option.trim() };
|
|
251
|
+
if (typeof option == "number" || typeof option == "boolean")
|
|
252
|
+
return { value: String(option), label: String(option) };
|
|
253
|
+
if (!isObject(option) || typeof option.value != "string" || !option.value.trim())
|
|
254
|
+
return null;
|
|
255
|
+
const label = typeof option.label == "string" && option.label.trim() ? option.label.trim() : option.value.trim();
|
|
256
|
+
return { value: option.value.trim(), label };
|
|
257
|
+
}
|
|
165
258
|
const EXAMPLE_ID = "0b7c6d9e-3f1a-4e2b-9c5d-8a7f6e5d4c3b";
|
|
166
259
|
const isPosition = (value) => Array.isArray(value) && value.length == 2 && value.every(n => typeof n == "number" && Number.isFinite(n));
|
|
167
260
|
const isObject = (value) => !!value && typeof value == "object" && !Array.isArray(value);
|
|
@@ -321,6 +414,8 @@ export function formatProjectFile(project) {
|
|
|
321
414
|
json.exclude = project.exclude;
|
|
322
415
|
if (project.panels?.length)
|
|
323
416
|
json.panels = project.panels;
|
|
417
|
+
if (project.settings?.length)
|
|
418
|
+
json.settings = project.settings;
|
|
324
419
|
const text = JSON.stringify(json, null, 2) + "\n";
|
|
325
420
|
return project.map ? updateProjectFile(text, { map: project.map }) : text;
|
|
326
421
|
}
|
package/dist/quest/convert.js
CHANGED
|
@@ -93,9 +93,23 @@ function convertGame(game, options) {
|
|
|
93
93
|
return { section: sectionNames.get(p.name) };
|
|
94
94
|
};
|
|
95
95
|
// Quest keeps flags and counters as attributes of the game. Most can keep their names.
|
|
96
|
+
const attributeNames = new Map();
|
|
97
|
+
const usedAttributeNames = new Set();
|
|
96
98
|
const attribute = (questName) => {
|
|
97
99
|
const trimmed = questName.trim();
|
|
98
|
-
|
|
100
|
+
let name = attributeNames.get(trimmed);
|
|
101
|
+
if (name !== undefined)
|
|
102
|
+
return name;
|
|
103
|
+
name = squiffyAttributeName(trimmed);
|
|
104
|
+
// Two Quest names that become the same Squiffy name are kept apart
|
|
105
|
+
if (name != trimmed) {
|
|
106
|
+
const base = name;
|
|
107
|
+
for (let i = 2; usedAttributeNames.has(name); i++)
|
|
108
|
+
name = `${base}_${i}`;
|
|
109
|
+
}
|
|
110
|
+
attributeNames.set(trimmed, name);
|
|
111
|
+
usedAttributeNames.add(name);
|
|
112
|
+
return name;
|
|
99
113
|
};
|
|
100
114
|
const startPage = (game.start && sectionNames.has(game.start) ? game.start : undefined) ?? storyPages[0]?.name;
|
|
101
115
|
const title = game.game.name.replace(/\s+/g, " ").trim();
|
|
@@ -222,7 +236,7 @@ function convertGame(game, options) {
|
|
|
222
236
|
// Quest shows the title once, when the game starts, and many gamebooks go back to
|
|
223
237
|
// their first page to play again
|
|
224
238
|
if (initial.length || parts.length) {
|
|
225
|
-
lines.push(
|
|
239
|
+
lines.push("{{#once}}", ...initial.map(l => `{{set ${quote(l.attribute)} ${print(linkValue(l, l.initial))}}}`), ...parts, "{{/once}}");
|
|
226
240
|
}
|
|
227
241
|
}
|
|
228
242
|
const scriptOnly = scriptOnlyPages.has(page.name);
|
|
@@ -502,3 +516,16 @@ export function formatQuestProblems(result, sourceName) {
|
|
|
502
516
|
}
|
|
503
517
|
return lines.join("\n") + "\n";
|
|
504
518
|
}
|
|
519
|
+
// A Squiffy attribute name for a Quest flag, counter or attribute name. A name that isn't one
|
|
520
|
+
// word is written [like this] in an expression, which can't have a comma (that makes it a list,
|
|
521
|
+
// [a, b]), a square or curly bracket, a quote or a backslash in it, or start with a digit, a - or
|
|
522
|
+
// a (. A dot would make it a path to a value inside an object. Quest lets a flag be called
|
|
523
|
+
// anything, so those are changed to _.
|
|
524
|
+
function squiffyAttributeName(name) {
|
|
525
|
+
if (reservedAttributes.has(name))
|
|
526
|
+
return name + "_";
|
|
527
|
+
let result = name.replace(/[,.[\]{}"'`\\]/g, "_");
|
|
528
|
+
if (/^[\d(-]/.test(result))
|
|
529
|
+
result = "_" + result;
|
|
530
|
+
return result;
|
|
531
|
+
}
|
package/dist/quest/scripts.js
CHANGED
|
@@ -70,19 +70,19 @@ class Converter {
|
|
|
70
70
|
return lines;
|
|
71
71
|
}
|
|
72
72
|
case "firsttime": {
|
|
73
|
-
// At the top of a page's script,
|
|
74
|
-
// else,
|
|
73
|
+
// {{#once}} remembers by where it is in the text. At the top of a page's script,
|
|
74
|
+
// that's the page. Anywhere else, the script may be shown from several places
|
|
75
|
+
// (a function, say), so it has a name to remember by.
|
|
75
76
|
const lines = [];
|
|
76
77
|
if (topLevel && this.ctx.page !== undefined && !this.ctx.inCallback) {
|
|
77
|
-
lines.push(
|
|
78
|
+
lines.push("{{#once}}", ...this.statements(s.body, false));
|
|
78
79
|
}
|
|
79
80
|
else {
|
|
80
|
-
|
|
81
|
-
lines.push(`{{#unless ${attribute}}}`, `{{set ${quote(attribute)} true}}`, ...this.statements(s.body, false));
|
|
81
|
+
lines.push(`{{#once ${quote(this.ctx.newFirstTime())}}}`, ...this.statements(s.body, false));
|
|
82
82
|
}
|
|
83
83
|
if (s.otherwise)
|
|
84
84
|
lines.push("{{else}}", ...this.statements(s.otherwise, false));
|
|
85
|
-
lines.push(
|
|
85
|
+
lines.push("{{/once}}");
|
|
86
86
|
return lines;
|
|
87
87
|
}
|
|
88
88
|
case "switch": {
|
package/dist/quest/text.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// Converts the text of a Quest gamebook page, which is HTML with Quest's text processor commands
|
|
2
2
|
// in it ({if ...}, {once:...}, {page:...} and so on), into Squiffy text.
|
|
3
3
|
import { attributeRef, display, print, quote, Unconvertible } from "../twine/expression.js";
|
|
4
|
-
import { sectionLink, urlLink } from "../twine/convert.js";
|
|
4
|
+
import { escapeLinkBrackets, sectionLink, urlLink } from "../twine/convert.js";
|
|
5
5
|
// Finds the next {...} the way Quest does: the first {, and the } that matches it, counting the
|
|
6
6
|
// ones in between. Undefined if there isn't one.
|
|
7
7
|
function nextCommand(text, from) {
|
|
@@ -70,10 +70,11 @@ function convertCommand(section, ctx, mode) {
|
|
|
70
70
|
case "command:":
|
|
71
71
|
case "page:":
|
|
72
72
|
return convertPageLink(section, ctx, mode);
|
|
73
|
+
// Quest remembers each of these by its own text, as {{#once}} remembers each of its own
|
|
73
74
|
case "once:":
|
|
74
|
-
return `{{#
|
|
75
|
+
return `{{#once}}${nested(section.substring(5))}{{/once}}`;
|
|
75
76
|
case "notfirst:":
|
|
76
|
-
return `{{#
|
|
77
|
+
return `{{#once}}{{else}}${nested(section.substring(9))}{{/once}}`;
|
|
77
78
|
case "random:":
|
|
78
79
|
return convertRandom(section.substring(7), ctx);
|
|
79
80
|
case "img:": {
|
|
@@ -94,7 +95,7 @@ function convertCommand(section, ctx, mode) {
|
|
|
94
95
|
}
|
|
95
96
|
// A passage link shows the popup's text when it's clicked
|
|
96
97
|
const name = ctx.addPassage(link, convertQuestText(content, ctx, "block"));
|
|
97
|
-
return `[${nested(link)
|
|
98
|
+
return `[${escapeLinkBrackets(nested(link))}](${name})`;
|
|
98
99
|
}
|
|
99
100
|
case "either ":
|
|
100
101
|
case "eval:":
|
|
@@ -136,9 +137,6 @@ function literalBraces(inner) {
|
|
|
136
137
|
export function objectAttribute(object, attribute) {
|
|
137
138
|
return object == "game" ? attribute : `${object}_${attribute}`;
|
|
138
139
|
}
|
|
139
|
-
function visits(ctx) {
|
|
140
|
-
return { k: "call", fn: "visits", args: [{ k: "str", v: ctx.section }] };
|
|
141
|
-
}
|
|
142
140
|
// {if flag:text}, {if not flag:text}, {if counter>5:text}, {if game.flag:text}, {if Page3.visited:text}
|
|
143
141
|
function convertIf(section, ctx, mode) {
|
|
144
142
|
const command = section.substring(3);
|
|
@@ -168,7 +166,18 @@ function convertIf(section, ctx, mode) {
|
|
|
168
166
|
const rhs = /^-?\d+(\.\d+)?$/.test(rhsText) ? { k: "num", v: Number(rhsText) } : { k: "str", v: rhsText };
|
|
169
167
|
expr = { k: "bin", op: operator == "<>" ? "!=" : operator, l: lhs, r: rhs };
|
|
170
168
|
}
|
|
171
|
-
return `{{#if ${
|
|
169
|
+
return `{{#if ${printCondition(expr, mode)}}}${text}{{/if}}`;
|
|
170
|
+
}
|
|
171
|
+
// A condition, as it's written in an {{#if}}. A link's text goes in a text="..." string, where
|
|
172
|
+
// the compiler writes a " as \", which the condition can't then read, so text in it is quoted
|
|
173
|
+
// with single quotes.
|
|
174
|
+
function printCondition(expr, mode) {
|
|
175
|
+
if (mode == "block")
|
|
176
|
+
return print(expr);
|
|
177
|
+
const printed = print(expr, { preferSingle: true });
|
|
178
|
+
if (printed.includes("\""))
|
|
179
|
+
throw new Unconvertible("a condition in a link's text can't compare with text that has a quote mark in it");
|
|
180
|
+
return printed;
|
|
172
181
|
}
|
|
173
182
|
// A flag or counter, game.attribute, or page.visited, in an {if}
|
|
174
183
|
function reference(name, ctx) {
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { SectionNode, SquiffyFile } from "./syntax.js";
|
|
2
|
+
export declare function sectionDetail(node: SectionNode): string | undefined;
|
|
3
|
+
export declare const capitalise: (text: string) => string;
|
|
4
|
+
export declare function sectionLabels(files: Iterable<SquiffyFile>, nameOf: (node: SectionNode) => string | undefined): Map<string, string>;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// What messages call sections, so that a section without a name of its own isn't called by the
|
|
2
|
+
// name the compiler gives it. Used by the compiler's warnings, the story graph's and the CLI's.
|
|
3
|
+
// What the outline calls a section without a name of its own
|
|
4
|
+
export function sectionDetail(node) {
|
|
5
|
+
if (node.kind == "preamble")
|
|
6
|
+
return "(Start)";
|
|
7
|
+
if (node.header?.type == "continue")
|
|
8
|
+
return `+++${node.header.text}`;
|
|
9
|
+
if (node.header?.type == "separator")
|
|
10
|
+
return "---";
|
|
11
|
+
return undefined;
|
|
12
|
+
}
|
|
13
|
+
const ORDINALS = ["first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth"];
|
|
14
|
+
const ordinal = (n) => ORDINALS[n - 1] ?? `${n}th`;
|
|
15
|
+
export const capitalise = (text) => text.charAt(0).toUpperCase() + text.slice(1);
|
|
16
|
+
// What messages call each section, by name. A section without a name of its own is called by its
|
|
17
|
+
// +++ or --- line, not the name the compiler gives it (_continue1, ...), which the author never
|
|
18
|
+
// wrote and can't search for. As a story can have lots of "+++Continue..." lines, it also says
|
|
19
|
+
// which named section it comes after, and which one it is if there are several:
|
|
20
|
+
// 'the section "cave"', 'the "+++Continue..." after "cave"', 'the second "---" after "cave"',
|
|
21
|
+
// 'the third "+++Next"' (before any named section), 'the text before the first section'.
|
|
22
|
+
export function sectionLabels(files, nameOf) {
|
|
23
|
+
const labels = new Map();
|
|
24
|
+
for (const tree of files) {
|
|
25
|
+
// The sections without names of their own since the last named one
|
|
26
|
+
let after;
|
|
27
|
+
let run = [];
|
|
28
|
+
const finish = () => {
|
|
29
|
+
const total = new Map();
|
|
30
|
+
for (const { detail } of run)
|
|
31
|
+
total.set(detail, (total.get(detail) ?? 0) + 1);
|
|
32
|
+
const seen = new Map();
|
|
33
|
+
for (const { name, detail } of run) {
|
|
34
|
+
const n = (seen.get(detail) ?? 0) + 1;
|
|
35
|
+
seen.set(detail, n);
|
|
36
|
+
const which = total.get(detail) > 1 ? `${ordinal(n)} ` : "";
|
|
37
|
+
const where = after !== undefined ? ` after "${after}"` : "";
|
|
38
|
+
labels.set(name, `the ${which}"${detail}"${where}`);
|
|
39
|
+
}
|
|
40
|
+
run = [];
|
|
41
|
+
};
|
|
42
|
+
for (const node of [tree.preamble, ...tree.sections]) {
|
|
43
|
+
if (node.type != "section")
|
|
44
|
+
continue;
|
|
45
|
+
const name = nameOf(node);
|
|
46
|
+
if (name === undefined)
|
|
47
|
+
continue;
|
|
48
|
+
if (node.kind == "preamble") {
|
|
49
|
+
labels.set(name, "the text before the first section");
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
const detail = sectionDetail(node);
|
|
53
|
+
if (detail === undefined) {
|
|
54
|
+
finish();
|
|
55
|
+
after = name;
|
|
56
|
+
labels.set(name, `the section "${name}"`);
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
run.push({ name, detail });
|
|
60
|
+
}
|
|
61
|
+
finish();
|
|
62
|
+
}
|
|
63
|
+
return labels;
|
|
64
|
+
}
|
package/dist/setters.js
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
// Conversion isn't idempotent: a converted @set best = score would be converted again to
|
|
17
17
|
// @set best = "score". So it's only for stories without a squiffy.json.
|
|
18
18
|
import { parse } from "./parse.js";
|
|
19
|
-
// The same as the tokenizer's numbers and literals in
|
|
19
|
+
// The same as the tokenizer's numbers and literals in infix-expressions.ts
|
|
20
20
|
const numberPattern = /^-?\d+(\.\d+)?$/;
|
|
21
21
|
const reservedWords = new Set(["and", "or", "not", "true", "false", "null", "undefined"]);
|
|
22
22
|
// Text as a quoted string. preferSingle is for setters inside a set="..." string, where double
|
|
@@ -180,7 +180,7 @@ export function convertSetters(script) {
|
|
|
180
180
|
// A link's setters: everything after the first comma of its target. For a next link, the target
|
|
181
181
|
// is just the setters part, e.g. ", gold=1".
|
|
182
182
|
function convertLink(link, lines, replace, problem) {
|
|
183
|
-
if (link.form === "unnamed")
|
|
183
|
+
if (link.form === "unnamed" || link.form === "input")
|
|
184
184
|
return 0;
|
|
185
185
|
const comma = link.target.indexOf(",");
|
|
186
186
|
if (comma === -1)
|
|
@@ -242,7 +242,7 @@ export function findAtAttributes(file) {
|
|
|
242
242
|
for (const link of container.links) {
|
|
243
243
|
const comma = link.target.indexOf(",");
|
|
244
244
|
const { targetRange } = link;
|
|
245
|
-
if (link.form === "unnamed" || comma === -1 || targetRange.start.line !== targetRange.end.line)
|
|
245
|
+
if (link.form === "unnamed" || link.form === "input" || comma === -1 || targetRange.start.line !== targetRange.end.line)
|
|
246
246
|
continue;
|
|
247
247
|
scan(link.target.substring(comma + 1), targetRange.start.line, targetRange.start.character + comma + 1);
|
|
248
248
|
}
|
|
@@ -251,7 +251,7 @@ export function findAtAttributes(file) {
|
|
|
251
251
|
return result;
|
|
252
252
|
}
|
|
253
253
|
// Splits a list of setters with the rules of a story that uses expressions (as splitSetters in
|
|
254
|
-
//
|
|
254
|
+
// infix-expressions.ts does): on commas outside quotes, brackets and objects. Keeps
|
|
255
255
|
// offsets.
|
|
256
256
|
export function splitWithOffsets(setters) {
|
|
257
257
|
const result = [];
|
|
@@ -305,7 +305,7 @@ function setterSites(file) {
|
|
|
305
305
|
for (const link of container.links) {
|
|
306
306
|
const comma = link.target.indexOf(",");
|
|
307
307
|
const { targetRange } = link;
|
|
308
|
-
if (link.form === "unnamed" || comma === -1 || targetRange.start.line !== targetRange.end.line)
|
|
308
|
+
if (link.form === "unnamed" || link.form === "input" || comma === -1 || targetRange.start.line !== targetRange.end.line)
|
|
309
309
|
continue;
|
|
310
310
|
addList(link.target.substring(comma + 1), targetRange.start.line, targetRange.start.character + comma + 1, false);
|
|
311
311
|
}
|
package/dist/story-graph.d.ts
CHANGED
|
@@ -10,6 +10,7 @@ export interface GraphNode {
|
|
|
10
10
|
name: string;
|
|
11
11
|
section?: string;
|
|
12
12
|
detail?: string;
|
|
13
|
+
label?: string;
|
|
13
14
|
location: Location;
|
|
14
15
|
contentEnd: Position;
|
|
15
16
|
start?: boolean;
|
|
@@ -64,3 +65,4 @@ export interface GraphSource {
|
|
|
64
65
|
}
|
|
65
66
|
export declare function buildStoryGraph(source: GraphSource): StoryGraph;
|
|
66
67
|
export declare function storyGraphDiagnostics(graph: StoryGraph): Diagnostic[];
|
|
68
|
+
export declare function firstScreenSoundDiagnostics(source: GraphSource): Diagnostic[];
|