squiffy-compiler 7.0.0-beta.1 → 7.0.0-beta.11

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.
Files changed (61) hide show
  1. package/README.md +10 -2
  2. package/dist/analyse.d.ts +20 -1
  3. package/dist/analyse.js +457 -26
  4. package/dist/compiler.d.ts +20 -0
  5. package/dist/compiler.js +9 -1
  6. package/dist/css-urls.d.ts +4 -0
  7. package/dist/css-urls.js +59 -0
  8. package/dist/emit.d.ts +28 -0
  9. package/dist/emit.js +79 -1
  10. package/dist/helpers.d.ts +1 -0
  11. package/dist/helpers.js +14 -0
  12. package/dist/ignore.d.ts +8 -0
  13. package/dist/ignore.js +71 -0
  14. package/dist/infix-expressions.d.ts +19 -0
  15. package/dist/infix-expressions.js +713 -0
  16. package/dist/language-service.d.ts +14 -1
  17. package/dist/language-service.js +266 -52
  18. package/dist/node.d.ts +23 -0
  19. package/dist/node.js +122 -0
  20. package/dist/parse.d.ts +5 -0
  21. package/dist/parse.js +62 -4
  22. package/dist/project.d.ts +129 -0
  23. package/dist/project.js +488 -0
  24. package/dist/quest/aslx.d.ts +51 -0
  25. package/dist/quest/aslx.js +184 -0
  26. package/dist/quest/convert.d.ts +20 -0
  27. package/dist/quest/convert.js +552 -0
  28. package/dist/quest/script.d.ts +92 -0
  29. package/dist/quest/script.js +453 -0
  30. package/dist/quest/scripts.d.ts +24 -0
  31. package/dist/quest/scripts.js +506 -0
  32. package/dist/quest/text.d.ts +18 -0
  33. package/dist/quest/text.js +323 -0
  34. package/dist/section-labels.d.ts +4 -0
  35. package/dist/section-labels.js +64 -0
  36. package/dist/setters.d.ts +49 -0
  37. package/dist/setters.js +477 -0
  38. package/dist/story-graph.d.ts +71 -0
  39. package/dist/story-graph.js +445 -0
  40. package/dist/syntax.d.ts +56 -4
  41. package/dist/templates.d.ts +17 -0
  42. package/dist/templates.js +180 -0
  43. package/dist/text.d.ts +9 -2
  44. package/dist/text.js +99 -39
  45. package/dist/twine/audio.d.ts +21 -0
  46. package/dist/twine/audio.js +80 -0
  47. package/dist/twine/convert.d.ts +131 -0
  48. package/dist/twine/convert.js +1057 -0
  49. package/dist/twine/expression.d.ts +88 -0
  50. package/dist/twine/expression.js +318 -0
  51. package/dist/twine/harlowe.d.ts +27 -0
  52. package/dist/twine/harlowe.js +2341 -0
  53. package/dist/twine/settings.d.ts +9 -0
  54. package/dist/twine/settings.js +322 -0
  55. package/dist/twine/story.d.ts +24 -0
  56. package/dist/twine/story.js +231 -0
  57. package/dist/twine/sugarcube.d.ts +40 -0
  58. package/dist/twine/sugarcube.js +2172 -0
  59. package/dist/v5.js +2 -1
  60. package/package.json +23 -5
  61. package/syntaxes/squiffy.tmLanguage.json +1003 -0
package/dist/node.js ADDED
@@ -0,0 +1,122 @@
1
+ /// <reference types="node" />
2
+ // Finding a story on disk, for the CLI and the Vite plugin. This is the only part of the compiler
3
+ // that touches the file system, and it's a separate entry point (squiffy-compiler/node), so the
4
+ // rest of the compiler stays free of Node dependencies.
5
+ import fs from "fs/promises";
6
+ import path from "path";
7
+ import { getStoryFiles, isAlwaysSkipped, isSquiffyPath, isStoryFilePath, parseProjectFile, PROJECT_FILENAME, storyFileList, } from "./project.js";
8
+ // A problem finding the story, with a message to show the author as is
9
+ export class StoryNotFoundError extends Error {
10
+ }
11
+ const exists = async (filename) => {
12
+ try {
13
+ await fs.access(filename);
14
+ return true;
15
+ }
16
+ catch {
17
+ return false;
18
+ }
19
+ };
20
+ const isDirectory = async (filename) => {
21
+ try {
22
+ return (await fs.stat(filename)).isDirectory();
23
+ }
24
+ catch {
25
+ return false;
26
+ }
27
+ };
28
+ // The nearest squiffy.json in a folder or the folders above it
29
+ async function findProjectFile(folder) {
30
+ for (let dir = path.resolve(folder);; dir = path.dirname(dir)) {
31
+ const candidate = path.join(dir, PROJECT_FILENAME);
32
+ if (await exists(candidate))
33
+ return candidate;
34
+ if (path.dirname(dir) == dir)
35
+ return undefined;
36
+ }
37
+ }
38
+ // Every .squiffy file in a folder and its subfolders, relative to it with "/" separators, without
39
+ // looking inside the folders that are never part of a story.
40
+ export const listSquiffyFiles = (folder) => listFiles(folder, isSquiffyPath);
41
+ // Every .squiffy, .css and .js file in a folder and its subfolders, as listSquiffyFiles, and any
42
+ // index.html, which getStoryFiles needs to see to leave out an old build
43
+ export const listStoryFiles = (folder) => listFiles(folder, name => isStoryFilePath(name) || name == "index.html");
44
+ async function listFiles(folder, include) {
45
+ const result = [];
46
+ const walk = async (relative) => {
47
+ const entries = await fs.readdir(path.join(folder, relative), { withFileTypes: true });
48
+ for (const entry of entries) {
49
+ const child = relative ? `${relative}/${entry.name}` : entry.name;
50
+ if (entry.isDirectory()) {
51
+ if (!isAlwaysSkipped(`${child}/x`))
52
+ await walk(child);
53
+ }
54
+ else if (include(entry.name)) {
55
+ result.push(child);
56
+ }
57
+ }
58
+ };
59
+ await walk("");
60
+ return result;
61
+ }
62
+ async function readProject(projectFile) {
63
+ const parsed = parseProjectFile(await fs.readFile(projectFile, "utf8"));
64
+ if (!parsed.success) {
65
+ throw new StoryNotFoundError(`${parsed.error} (${projectFile})`);
66
+ }
67
+ return parsed.project;
68
+ }
69
+ async function folderStory(projectFile, options) {
70
+ const root = path.dirname(projectFile);
71
+ const project = await readProject(projectFile);
72
+ const mainFile = path.resolve(root, project.main);
73
+ if (!await exists(mainFile)) {
74
+ throw new StoryNotFoundError(`The main file in ${projectFile}, ${project.main}, doesn't exist.`);
75
+ }
76
+ // Absolute paths to skip are made relative to the project, and any outside it are ignored
77
+ const skip = (options.skip ?? []).map(file => path.isAbsolute(file) ? path.relative(root, file).split(path.sep).join("/") : file)
78
+ .filter(file => file && !file.startsWith("..") && !path.isAbsolute(file));
79
+ const story = getStoryFiles(project, await listStoryFiles(root), skip);
80
+ return {
81
+ mainFile, storyFiles: storyFileList(story).map(file => path.resolve(root, file)), projectFile, expressions: true,
82
+ ...(project.backButton !== undefined ? { backButton: project.backButton } : {}),
83
+ ...(project.strings !== undefined ? { strings: project.strings } : {}),
84
+ ...(project.saveButton !== undefined ? { saveButton: project.saveButton } : {}),
85
+ ...(project.id !== undefined ? { projectId: project.id } : {}),
86
+ ...(project.panels !== undefined ? { panels: project.panels } : {}),
87
+ ...(project.settings !== undefined ? { settings: project.settings } : {}),
88
+ ...(project.persistent !== undefined ? { persistent: project.persistent } : {}),
89
+ };
90
+ }
91
+ // Finds the story to build from what the author gave: a folder with a squiffy.json in it, or a
92
+ // .squiffy file.
93
+ //
94
+ // - A folder builds the project in its squiffy.json.
95
+ // - A project's main file (with a squiffy.json in its folder or above) builds the project.
96
+ // - Any other .squiffy file builds just that file and its @imports, as it would with no
97
+ // squiffy.json, with a note saying so. So several stories can share a folder.
98
+ export async function findStory(input, options = {}) {
99
+ const resolved = path.resolve(input);
100
+ if (await isDirectory(resolved)) {
101
+ const projectFile = path.join(resolved, PROJECT_FILENAME);
102
+ if (!await exists(projectFile)) {
103
+ throw new StoryNotFoundError(`There's no ${PROJECT_FILENAME} in ${resolved}. Give the story's .squiffy file instead, or add a ${PROJECT_FILENAME} to make the folder one story.`);
104
+ }
105
+ return folderStory(projectFile, options);
106
+ }
107
+ if (!await exists(resolved)) {
108
+ throw new StoryNotFoundError(`${input} doesn't exist.`);
109
+ }
110
+ const projectFile = await findProjectFile(path.dirname(resolved));
111
+ if (!projectFile)
112
+ return { mainFile: resolved, expressions: false };
113
+ const project = await readProject(projectFile);
114
+ if (path.resolve(path.dirname(projectFile), project.main) == resolved) {
115
+ return folderStory(projectFile, options);
116
+ }
117
+ return {
118
+ mainFile: resolved,
119
+ note: `Building just ${path.basename(resolved)}, as it isn't the main file in ${projectFile} (${project.main}).`,
120
+ expressions: true,
121
+ };
122
+ }
package/dist/parse.d.ts CHANGED
@@ -13,5 +13,10 @@ export declare const lineRegexes: {
13
13
  js: RegExp;
14
14
  continue: RegExp;
15
15
  ui: RegExp;
16
+ music: RegExp;
17
+ sound: RegExp;
18
+ class: RegExp;
19
+ tag: RegExp;
20
+ transition: RegExp;
16
21
  };
17
22
  export declare function parse(text: string, filename?: string): SquiffyFile;
package/dist/parse.js CHANGED
@@ -13,6 +13,11 @@ export const lineRegexes = {
13
13
  js: /^(\t| {4})(.*)$/,
14
14
  continue: /^\+\+\+(.*)$/,
15
15
  ui: /^@ui (.*)$/,
16
+ music: /^@music (.*)$/,
17
+ sound: /^@sound (.*)$/,
18
+ class: /^@class (.*)$/,
19
+ tag: /^@tag (.*)$/,
20
+ transition: /^@(transition|leave) (\S+)(?: (\S+))?$/,
16
21
  };
17
22
  // Parses one .squiffy file into a syntax tree. This never fails: problems such as a passage
18
23
  // with no section are reported by analyse(), which knows how the file fits into the story.
@@ -123,6 +128,7 @@ export function parse(text, filename) {
123
128
  body: [],
124
129
  links: [],
125
130
  embeds: [],
131
+ gotos: [], deactivates: [],
126
132
  };
127
133
  section.passages.push(passage);
128
134
  textStarted = false;
@@ -158,6 +164,14 @@ export function parse(text, filename) {
158
164
  haveSection = true;
159
165
  add({ type: "clear", range });
160
166
  }
167
+ else if (stripLine == "@repeat") {
168
+ add({ type: "repeat", range });
169
+ }
170
+ else if (stripLine == "@end") {
171
+ if (!passage)
172
+ haveSection = true;
173
+ add({ type: "end", range });
174
+ }
161
175
  else if (match.title) {
162
176
  add({ type: "title", value: match.title[1], valueRange: valueRange(i, match.title[1]), range });
163
177
  }
@@ -167,6 +181,36 @@ export function parse(text, filename) {
167
181
  else if (match.import) {
168
182
  add({ type: "import", pattern: match.import[1], patternRange: valueRange(i, match.import[1]), range });
169
183
  }
184
+ else if (match.music || match.sound) {
185
+ if (!passage)
186
+ haveSection = true;
187
+ const value = (match.music ?? match.sound)[1];
188
+ add({ type: "sound", directive: match.music ? "music" : "sound", value, valueRange: valueRange(i, value), range });
189
+ }
190
+ else if (match.class) {
191
+ if (!passage)
192
+ haveSection = true;
193
+ add({ type: "class", value: match.class[1], valueRange: valueRange(i, match.class[1]), range });
194
+ }
195
+ else if (match.tag) {
196
+ if (!passage)
197
+ haveSection = true;
198
+ add({ type: "tag", value: match.tag[1], valueRange: valueRange(i, match.tag[1]), range });
199
+ }
200
+ else if (match.transition) {
201
+ if (!passage)
202
+ haveSection = true;
203
+ const [, directive, value, time] = match.transition;
204
+ const start = line.indexOf(value, line.indexOf("@") + directive.length + 1);
205
+ add({
206
+ type: "transition",
207
+ directive: directive,
208
+ value,
209
+ valueRange: { start: { line: i, character: start }, end: { line: i, character: start + value.length } },
210
+ ...(time === undefined ? {} : { time }),
211
+ range,
212
+ });
213
+ }
170
214
  else if (match.attributes || match.unset) {
171
215
  haveSection = true;
172
216
  const value = (match.attributes ?? match.unset)[1];
@@ -227,9 +271,9 @@ export function parse(text, filename) {
227
271
  return file;
228
272
  }
229
273
  function newSection(kind, range) {
230
- return { type: "section", kind, range, body: [], passages: [], links: [], embeds: [] };
274
+ return { type: "section", kind, range, body: [], passages: [], links: [], embeds: [], gotos: [], deactivates: [] };
231
275
  }
232
- // Fills in the links and embeds in a section's or passage's text.
276
+ // Fills in the links, embeds and gotos in a section's or passage's text.
233
277
  function findLinks(node, lines) {
234
278
  const textNodes = node.body.filter((n) => n.type == "text");
235
279
  if (!textNodes.length)
@@ -238,8 +282,10 @@ function findLinks(node, lines) {
238
282
  const toPosition = positionMapper(sources, textNodes.map(n => n.text.length));
239
283
  const toRange = (start, end) => ({ start: toPosition(start), end: toPosition(end) });
240
284
  // Which section comes next isn't known until the story is analysed, but all that matters
241
- // here is that there is one, so that [[text>]] is treated as a link.
242
- const result = transformText(textNodes.map(n => n.text).join("\n"), () => "");
285
+ // here is that there is one, so that [[text>]] is treated as a link. Link
286
+ // targets can have brackets in them, as in a story with a squiffy.json, whether or not this
287
+ // one has one: the two only differ when a target has a "(" in it.
288
+ const result = transformText(textNodes.map(n => n.text).join("\n"), () => "", true);
243
289
  node.links = result.links.map((link) => ({
244
290
  type: "link",
245
291
  linkType: link.linkType,
@@ -255,4 +301,16 @@ function findLinks(node, lines) {
255
301
  range: toRange(embed.start, embed.end),
256
302
  nameRange: toRange(embed.nameStart, embed.nameEnd),
257
303
  }));
304
+ node.gotos = result.gotos.map((goto) => ({
305
+ type: "goto",
306
+ name: goto.name,
307
+ range: toRange(goto.start, goto.end),
308
+ nameRange: toRange(goto.nameStart, goto.nameEnd),
309
+ }));
310
+ node.deactivates = result.deactivates.map((deactivate) => ({
311
+ type: "deactivate",
312
+ name: deactivate.name,
313
+ range: toRange(deactivate.start, deactivate.end),
314
+ nameRange: toRange(deactivate.nameStart, deactivate.nameEnd),
315
+ }));
258
316
  }
@@ -0,0 +1,129 @@
1
+ export declare const PROJECT_FILENAME = "squiffy.json";
2
+ export declare const PROJECT_FILE_VERSION = 1;
3
+ export declare const PROJECT_SCHEMA_URL = "https://squiffystory.com/schema/squiffy.json";
4
+ export interface ProjectFile {
5
+ version: number;
6
+ id?: string;
7
+ main: string;
8
+ exclude: string[];
9
+ map?: MapPositions;
10
+ backButton?: boolean;
11
+ saveButton?: boolean;
12
+ strings?: PageStrings;
13
+ panels?: Panel[];
14
+ settings?: PlayerSetting[];
15
+ persistent?: string[];
16
+ }
17
+ export type PlayerSetting = PlayerSettingBase & {
18
+ type: "toggle";
19
+ default?: boolean;
20
+ } | PlayerSettingBase & {
21
+ type: "list";
22
+ options: PlayerSettingOption[];
23
+ default?: string;
24
+ } | PlayerSettingBase & {
25
+ type: "range";
26
+ min: number;
27
+ max: number;
28
+ step?: number;
29
+ default?: number;
30
+ };
31
+ interface PlayerSettingBase {
32
+ name: string;
33
+ label: string;
34
+ description?: string;
35
+ }
36
+ export interface PlayerSettingOption {
37
+ value: string;
38
+ label: string;
39
+ }
40
+ export declare const PLAYER_SETTING_TYPES: readonly ["toggle", "list", "range"];
41
+ export declare const PANEL_ICONS: readonly ["bag", "person", "map", "help", "info", "list", "book", "star", "heart"];
42
+ export type PanelIcon = typeof PANEL_ICONS[number];
43
+ export interface Panel {
44
+ passage: string;
45
+ label: string;
46
+ position?: "side" | "button";
47
+ icon?: PanelIcon;
48
+ }
49
+ export type PageStringName = keyof typeof PAGE_STRINGS;
50
+ export type PageStrings = Partial<Record<PageStringName, string>>;
51
+ export declare const PAGE_STRINGS: {
52
+ back: string;
53
+ restart: string;
54
+ restartConfirm: string;
55
+ switchToDark: string;
56
+ switchToLight: string;
57
+ mute: string;
58
+ unmute: string;
59
+ saves: string;
60
+ saveName: string;
61
+ save: string;
62
+ noSaves: string;
63
+ savedGame: string;
64
+ savedToday: string;
65
+ savedYesterday: string;
66
+ load: string;
67
+ loadConfirm: string;
68
+ saveOptions: string;
69
+ overwrite: string;
70
+ overwriteConfirm: string;
71
+ rename: string;
72
+ newName: string;
73
+ saveToFile: string;
74
+ loadFromFile: string;
75
+ delete: string;
76
+ deleteConfirm: string;
77
+ cancel: string;
78
+ close: string;
79
+ wrongStory: string;
80
+ notASave: string;
81
+ cantSave: string;
82
+ settings: string;
83
+ resetSettings: string;
84
+ };
85
+ export interface MapPositions {
86
+ sections: Record<string, [number, number]>;
87
+ passages: Record<string, Record<string, [number, number]>>;
88
+ }
89
+ export type ParsedProjectFile = {
90
+ success: true;
91
+ project: ProjectFile;
92
+ } | {
93
+ success: false;
94
+ error: string;
95
+ };
96
+ export declare function parseProjectFile(text: string): ParsedProjectFile;
97
+ export declare function globToRegExp(pattern: string): RegExp;
98
+ export declare function isExcluded(path: string, exclude: string[]): boolean;
99
+ export declare function isAlwaysSkipped(path: string): boolean;
100
+ export declare const BUILD_FOLDER = "build";
101
+ export declare const isSquiffyPath: (path: string) => boolean;
102
+ export declare const isStylesheetPath: (path: string) => boolean;
103
+ export declare const isScriptPath: (path: string) => boolean;
104
+ export declare const isStoryFilePath: (path: string) => boolean;
105
+ export interface StoryFiles {
106
+ main: string;
107
+ included: string[];
108
+ stylesheets: string[];
109
+ scripts: string[];
110
+ excluded: string[];
111
+ }
112
+ export declare const comparePaths: (a: string, b: string) => 1 | 0 | -1;
113
+ export declare function getStoryFiles(project: ProjectFile, paths: Iterable<string>, skip?: string[]): StoryFiles;
114
+ export declare const storyFileList: (story: StoryFiles) => string[];
115
+ export declare function formatProjectFile(project: {
116
+ main: string;
117
+ exclude?: string[];
118
+ map?: MapPositions;
119
+ id?: string;
120
+ panels?: Panel[];
121
+ settings?: PlayerSetting[];
122
+ persistent?: string[];
123
+ }): string;
124
+ export declare function updateProjectFile(text: string, changes: {
125
+ main?: string;
126
+ exclude?: string[];
127
+ map?: MapPositions;
128
+ }): string | undefined;
129
+ export {};