squiffy-compiler 7.0.0-beta.6 → 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.
Files changed (46) hide show
  1. package/dist/analyse.d.ts +12 -1
  2. package/dist/analyse.js +156 -9
  3. package/dist/compiler.d.ts +5 -3
  4. package/dist/compiler.js +3 -2
  5. package/dist/css-urls.d.ts +4 -0
  6. package/dist/css-urls.js +59 -0
  7. package/dist/emit.d.ts +18 -1
  8. package/dist/emit.js +46 -0
  9. package/dist/infix-expressions.d.ts +19 -0
  10. package/dist/infix-expressions.js +713 -0
  11. package/dist/language-service.d.ts +1 -0
  12. package/dist/language-service.js +34 -15
  13. package/dist/node.d.ts +2 -1
  14. package/dist/node.js +1 -0
  15. package/dist/parse.d.ts +4 -0
  16. package/dist/parse.js +29 -0
  17. package/dist/project.d.ts +31 -0
  18. package/dist/project.js +98 -0
  19. package/dist/quest/convert.js +67 -11
  20. package/dist/quest/scripts.js +13 -9
  21. package/dist/quest/text.js +17 -8
  22. package/dist/section-labels.d.ts +4 -0
  23. package/dist/section-labels.js +64 -0
  24. package/dist/setters.d.ts +4 -0
  25. package/dist/setters.js +11 -9
  26. package/dist/story-graph.d.ts +2 -0
  27. package/dist/story-graph.js +61 -16
  28. package/dist/syntax.d.ts +24 -3
  29. package/dist/templates.d.ts +6 -0
  30. package/dist/templates.js +128 -0
  31. package/dist/text.d.ts +3 -1
  32. package/dist/text.js +45 -11
  33. package/dist/twine/audio.d.ts +21 -0
  34. package/dist/twine/audio.js +80 -0
  35. package/dist/twine/convert.d.ts +45 -3
  36. package/dist/twine/convert.js +305 -37
  37. package/dist/twine/expression.d.ts +21 -1
  38. package/dist/twine/expression.js +105 -8
  39. package/dist/twine/harlowe.d.ts +9 -0
  40. package/dist/twine/harlowe.js +461 -86
  41. package/dist/twine/settings.d.ts +9 -0
  42. package/dist/twine/settings.js +322 -0
  43. package/dist/twine/sugarcube.d.ts +3 -0
  44. package/dist/twine/sugarcube.js +438 -114
  45. package/package.json +9 -2
  46. package/syntaxes/squiffy.tmLanguage.json +8 -3
package/dist/text.js CHANGED
@@ -178,6 +178,12 @@ export function transformText(text, getNextSectionName, expressions = false) {
178
178
  return `${mustaches.length - 1}`;
179
179
  });
180
180
  const restore = (s) => s.replace(/(\d+)/g, (_match, i) => mustaches[Number(i)]);
181
+ // A link once it's converted, kept out of the way of the patterns for later kinds of link, which
182
+ // would otherwise find links in its setters, such as the [1] in [[Go]](next, party[1].hp += 2)
183
+ const converted = (helper) => {
184
+ mustaches.push(helper);
185
+ return `${mustaches.length - 1}`;
186
+ };
181
187
  // A link's target, inside its (...). With expressions, a setter can have brackets in it, e.g.
182
188
  // [[Buy]](shop, gold -= (price - 1) * 2), so the target can contain balanced brackets, up to
183
189
  // three deep. Without, it ends at the first ")", as in Squiffy 6.
@@ -193,7 +199,7 @@ export function transformText(text, getNextSectionName, expressions = false) {
193
199
  return `[[${text}]]`; // fallback
194
200
  }
195
201
  addLink({ linkType: "section", form: "next", text, target: "" }, offset, match.length, offset + 2 + rawText.length, 0);
196
- return `{{section "${escapeForHandlebarsString(nextSectionName)}" text="${escapeForHandlebarsString(text)}"}}`;
202
+ return converted(`{{section "${escapeForHandlebarsString(nextSectionName)}" text="${escapeForHandlebarsString(text)}"}}`);
197
203
  });
198
204
  // namedNextSectionLinkRegex matches [[text]](>) or [[text]](>, setter=value)
199
205
  // - a named link where target starts with >
@@ -209,7 +215,7 @@ export function transformText(text, getNextSectionName, expressions = false) {
209
215
  addLink({ linkType: "section", form: "next", text, target: rest }, offset, match.length, offset + 2 + rawText.length + 4, rawRest.length);
210
216
  // rest could be empty or ", setter=value"
211
217
  const parsedName = getAdditionalLinkParameters(nextSectionName + rest, expressions);
212
- return `{{section "${escapeForHandlebarsString(parsedName.target)}" text="${escapeForHandlebarsString(text)}"${parsedName.additionalParameters}}}`;
218
+ return converted(`{{section "${escapeForHandlebarsString(parsedName.target)}" text="${escapeForHandlebarsString(text)}"${parsedName.additionalParameters}}}`);
213
219
  });
214
220
  // namedSectionLinkRegex matches:
215
221
  // open [[
@@ -224,7 +230,7 @@ export function transformText(text, getNextSectionName, expressions = false) {
224
230
  const name = restore(rawName);
225
231
  addLink({ linkType: "section", form: "named", text, target: name }, offset, match.length, offset + 2 + rawText.length + 3, rawName.length);
226
232
  const parsedName = getAdditionalLinkParameters(name, expressions);
227
- return `{{section "${escapeForHandlebarsString(parsedName.target)}" text="${escapeForHandlebarsString(text)}"${parsedName.additionalParameters}}}`;
233
+ return converted(`{{section "${escapeForHandlebarsString(parsedName.target)}" text="${escapeForHandlebarsString(text)}"${parsedName.additionalParameters}}}`);
228
234
  });
229
235
  // namedPassageLinkRegex matches:
230
236
  // not preceded by ! (to avoid matching image links like ![alt](src))
@@ -240,7 +246,7 @@ export function transformText(text, getNextSectionName, expressions = false) {
240
246
  const name = restore(rawName);
241
247
  addLink({ linkType: "passage", form: "named", text, target: name }, offset, match.length, offset + 1 + rawText.length + 2, rawName.length);
242
248
  const parsedName = getAdditionalLinkParameters(name, expressions);
243
- return `{{passage "${escapeForHandlebarsString(parsedName.target)}" text="${escapeForHandlebarsString(text)}"${parsedName.additionalParameters}}}`;
249
+ return converted(`{{passage "${escapeForHandlebarsString(parsedName.target)}" text="${escapeForHandlebarsString(text)}"${parsedName.additionalParameters}}}`);
244
250
  });
245
251
  // unnamedSectionLinkRegex matches:
246
252
  // open [[
@@ -250,7 +256,7 @@ export function transformText(text, getNextSectionName, expressions = false) {
250
256
  input.replace(unnamedSectionLinkRegex, ([match, rawName], offset) => {
251
257
  const name = restore(rawName);
252
258
  addLink({ linkType: "section", form: "unnamed", target: name }, offset, match.length, offset + 2, rawName.length);
253
- return `{{section "${escapeForHandlebarsString(name)}"}}`;
259
+ return converted(`{{section "${escapeForHandlebarsString(name)}"}}`);
254
260
  });
255
261
  // unnamedPassageLinkRegex matches:
256
262
  // not preceded by ! (to avoid matching image links like ![alt])
@@ -262,13 +268,14 @@ export function transformText(text, getNextSectionName, expressions = false) {
262
268
  input.replace(unnamedPassageLinkRegex, ([match, rawName, suffix], offset) => {
263
269
  const name = restore(rawName);
264
270
  addLink({ linkType: "passage", form: "unnamed", target: name }, offset, match.length - suffix.length, offset + 1, rawName.length);
265
- return `{{passage "${escapeForHandlebarsString(name)}"}}${suffix}`;
271
+ return converted(`{{passage "${escapeForHandlebarsString(name)}"}}`) + suffix;
266
272
  });
267
273
  input.replace(/(\d+)/g, ([, i]) => mustaches[Number(i)]);
268
- // Matches {{embed "name"}} or {{goto 'name'}} with a literal name.
269
- const findNamed = (helper) => {
274
+ // Matches {{embed "name"}} or {{goto 'name'}} with a literal name. An embed can also be a
275
+ // block, {{#embed "name"}}...{{/embed}}, which wraps text (#450).
276
+ const findNamed = (helper, block = false) => {
270
277
  const result = [];
271
- const regex = new RegExp(`\\{\\{~?\\s*${helper}\\s+(["'])(.*?)\\1`, "g");
278
+ const regex = new RegExp(`\\{\\{~?\\s*${block ? "#?" : ""}${helper}\\s+(["'])(.*?)\\1`, "g");
272
279
  let match;
273
280
  while ((match = regex.exec(input.value))) {
274
281
  const start = match.index;
@@ -285,8 +292,35 @@ export function transformText(text, getNextSectionName, expressions = false) {
285
292
  }
286
293
  return result;
287
294
  };
288
- embeds.push(...findNamed("embed"));
289
- return { output: input.value, links, embeds, gotos: findNamed("goto"), deactivates: findNamed("deactivate"), noNextSection };
295
+ embeds.push(...findNamed("embed", true));
296
+ // A text box that goes to a section or passage when Enter is pressed (#454), as in
297
+ // <input data-attribute="name" data-section="next">. It's left as it is, and only recorded.
298
+ const inputTag = /<input\b(?:[^>"']|"[^"]*"|'[^']*')*>/gi;
299
+ const targetAttribute = /\sdata-(section|passage)\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'>]+))/i;
300
+ let tag;
301
+ while ((tag = inputTag.exec(input.value))) {
302
+ const attribute = targetAttribute.exec(tag[0]);
303
+ if (!attribute)
304
+ continue;
305
+ const name = attribute[2] ?? attribute[3] ?? attribute[4];
306
+ const start = tag.index;
307
+ const nameEnd = start + attribute.index + attribute[0].length - (attribute[4] === undefined ? 1 : 0);
308
+ const nameStart = nameEnd - name.length;
309
+ links.push({
310
+ linkType: attribute[1].toLowerCase() == "section" ? "section" : "passage",
311
+ form: "input",
312
+ target: name,
313
+ start: input.mapStart(start),
314
+ end: input.mapEnd(start + tag[0].length),
315
+ targetStart: input.mapStart(nameStart),
316
+ targetEnd: input.mapEnd(nameEnd),
317
+ });
318
+ }
319
+ return {
320
+ output: input.value, links, embeds, gotos: findNamed("goto"), deactivates: findNamed("deactivate"), noNextSection,
321
+ mapStart: pos => input.mapStart(pos),
322
+ mapEnd: pos => input.mapEnd(pos),
323
+ };
290
324
  }
291
325
  // Returns a function that converts an offset into lines.join("\n") to a position in the source file.
292
326
  export function positionMapper(lines, lineLengths) {
@@ -0,0 +1,21 @@
1
+ import type { PassageContext } from "./convert.js";
2
+ export interface StoryAudio {
3
+ files: Map<string, string>;
4
+ looping: Set<string>;
5
+ }
6
+ export declare function trackFile(audio: StoryAudio, track: string): string;
7
+ export declare function pickSource(sources: string[]): string | undefined;
8
+ export interface AudioAction {
9
+ play: boolean;
10
+ stop: boolean;
11
+ loop: boolean;
12
+ fade?: number;
13
+ volume?: number;
14
+ }
15
+ export declare function audioHelper(ctx: PassageContext, raw: string, audio: StoryAudio, track: string, action: AudioAction): string;
16
+ export declare const STOP_ALL_AUDIO = "{{stopsounds}}{{stopmusic}}";
17
+ export declare function audioDirective(helper: string): string | null;
18
+ export declare function leadingAudio(out: string, state: {
19
+ leading: boolean;
20
+ directives: string[];
21
+ }): string;
@@ -0,0 +1,80 @@
1
+ // Converts Twine's sound to Squiffy's music and sound helpers (#444). Squiffy has two kinds of
2
+ // sound: music, which loops and carries on from one section to the next until it's changed, and
3
+ // sounds, which play once. A Twine track that's set to loop anywhere in the story is music, and
4
+ // any other is a sound.
5
+ import { quote, Unconvertible } from "./expression.js";
6
+ // The file a track plays: the one the story gives it, or the track name itself if that looks
7
+ // like a file, such as "rain.mp3"
8
+ export function trackFile(audio, track) {
9
+ const file = audio.files.get(track);
10
+ if (file !== undefined)
11
+ return file;
12
+ if (/\.\w{2,4}$/.test(track))
13
+ return track;
14
+ throw new Unconvertible(`a track ("${track}") that the story doesn't give a file for`);
15
+ }
16
+ // The best of a track's sources: MP3 plays in every browser
17
+ export function pickSource(sources) {
18
+ return sources.find(s => /\.mp3(\?|#|$)/i.test(s)) ?? sources[0];
19
+ }
20
+ // What a track does, as Squiffy: {{music}}, {{stopmusic}}, {{sound}}, {{stopsounds}}, or nothing
21
+ export function audioHelper(ctx, raw, audio, track, action) {
22
+ const looping = action.loop || audio.looping.has(track);
23
+ const fade = action.fade === undefined ? "" : ` fade=${action.fade}`;
24
+ if (action.stop) {
25
+ if (looping)
26
+ return `{{stopmusic${fade}}}`;
27
+ // Squiffy stops every sound playing once, which is the same thing if there's only one
28
+ const sounds = new Set([...audio.files.keys()].filter(t => !audio.looping.has(t)));
29
+ sounds.add(track);
30
+ if (sounds.size > 1) {
31
+ ctx.story.note("audio-stop-sound", raw, "Squiffy stops every sound that's playing once, not just one track, so these stop the others too");
32
+ }
33
+ return "{{stopsounds}}";
34
+ }
35
+ if (!action.play)
36
+ return "";
37
+ const file = quote(trackFile(audio, track));
38
+ if (looping) {
39
+ if (action.volume !== undefined && action.volume != 1) {
40
+ ctx.story.note("audio-music-volume", raw, "Squiffy plays music at full volume, so the music's volume has been left out");
41
+ }
42
+ return `{{music ${file}${fade}}}`;
43
+ }
44
+ const volume = action.volume !== undefined && action.volume != 1 ? ` volume=${Math.max(0, Math.min(1, action.volume))}` : "";
45
+ return `{{sound ${file}${volume}}}`;
46
+ }
47
+ // Stops every sound and the music, as <<masteraudio stop>> and (masteraudio: "stopall") do
48
+ export const STOP_ALL_AUDIO = "{{stopsounds}}{{stopmusic}}";
49
+ // A helper that can be a directive, @music or @sound, for the start of a passage, or null
50
+ export function audioDirective(helper) {
51
+ if (helper == "{{stopmusic}}")
52
+ return "@music stop";
53
+ if (helper == "{{stopsounds}}")
54
+ return "@sound stop";
55
+ const m = /^\{\{(music|sound) "([^"&{}]+)"\}\}$/.exec(helper);
56
+ if (!m || m[2].trim() != m[2] || m[2] == "stop")
57
+ return null;
58
+ return `@${m[1]} ${m[2]}`;
59
+ }
60
+ // Audio helpers as directives where they can be, at the start of a passage
61
+ export function leadingAudio(out, state) {
62
+ if (!state.leading)
63
+ return out;
64
+ const helpers = out.match(/\{\{[^{}]*\}\}/g) ?? [];
65
+ const directives = helpers.map(audioDirective);
66
+ if (helpers.join("") != out || directives.some(d => d === null)) {
67
+ state.leading = false;
68
+ return out;
69
+ }
70
+ for (const directive of directives) {
71
+ // Only the last @music counts, as in Harlowe stopping one track and playing another
72
+ if (directive.startsWith("@music ")) {
73
+ const earlier = state.directives.findIndex(d => d.startsWith("@music "));
74
+ if (earlier != -1)
75
+ state.directives.splice(earlier, 1);
76
+ }
77
+ state.directives.push(directive);
78
+ }
79
+ return "";
80
+ }
@@ -28,6 +28,7 @@ export interface PassageContext {
28
28
  fill: (content: ConvertedPassage | string) => void;
29
29
  };
30
30
  addSection(nameHint: string, content: ConvertedPassage | string): string;
31
+ fillSection(name: string, content: ConvertedPassage | string): void;
31
32
  newLabel(hint: string): string;
32
33
  }
33
34
  export interface ConvertedPassage {
@@ -44,6 +45,14 @@ export declare class StoryContext {
44
45
  readonly extraSections: string[];
45
46
  private readonly permanentVars;
46
47
  readonly passagesByName: Map<string, TwinePassage>;
48
+ readonly passageStyles: {
49
+ passage: string;
50
+ css: string;
51
+ }[];
52
+ readonly passageScripts: {
53
+ passage: string;
54
+ js: string;
55
+ }[];
47
56
  constructor(twine: TwineStory, passages: TwinePassage[]);
48
57
  private readonly problemKeys;
49
58
  addProblem(problem: TwineConversionProblem): void;
@@ -60,23 +69,56 @@ export declare class StoryContext {
60
69
  uniqueSectionName(hint: string): string;
61
70
  newLabel(hint: string): string;
62
71
  private readonly loops;
63
- get looping(): boolean;
64
- inLoop<T>(vars: Map<string, "item" | "index">, array: string | undefined, convert: () => T): T;
72
+ inLoop<T>(vars: Map<string, "item" | "index">, array: string | undefined, convert: () => T, numbers?: boolean): T;
73
+ linkBody<T>(convert: () => T): {
74
+ result: T;
75
+ setters: string[];
76
+ };
65
77
  private loopVariable;
66
78
  variable(name: string, temporary: boolean): Expr;
67
79
  attribute(name: string, temporary: boolean): string;
80
+ private attributeName;
68
81
  }
69
82
  export declare function sanitiseName(name: string): string;
70
83
  export declare function escapeText(text: string): string;
84
+ export declare function escapeLinkBrackets(text: string): string;
71
85
  export declare function sectionLink(text: string | null, target: string, setters?: string[]): string;
72
86
  export declare function isUrl(target: string): boolean;
73
87
  export declare function urlLink(text: string, url: string): string;
74
88
  export declare function dynamicSectionLink(text: string, target: Expr, setters?: string[]): string;
75
- export declare function passageLink(text: string, target: string): string;
89
+ export declare function passageLink(text: string, target: string, setters?: string[]): string;
76
90
  export declare function setter(name: string, op: "=" | "+=" | "-=" | "*=" | "/=", value: Expr, inLink: boolean): string;
77
91
  export declare function comment(text: string): string;
78
92
  export declare function formatTwineProblems(result: TwineConversionResult, sourceName?: string): string;
79
93
  export declare function convertTwine(source: string, options?: TwineConversionOptions): TwineConversionResult;
80
94
  export declare function convertTwineStory(twine: TwineStory, options?: TwineConversionOptions): TwineConversionResult;
95
+ export declare function tagClasses(tags: string[]): string[];
96
+ export declare function convertTagSelectors(css: string): string;
97
+ export interface HtmlBlock {
98
+ t: "html";
99
+ tag: "script" | "style";
100
+ raw: string;
101
+ attributes: string;
102
+ content: string;
103
+ }
104
+ export declare function readHtmlBlock(src: string, i: number): {
105
+ block: HtmlBlock;
106
+ end: number;
107
+ } | undefined;
108
+ export declare function passageHtmlBlock(ctx: PassageContext, block: HtmlBlock, top: boolean): {
109
+ text: string;
110
+ directive?: string;
111
+ };
112
+ export declare function topScripts<N extends {
113
+ t: string;
114
+ }>(ctx: PassageContext, nodes: (N | HtmlBlock)[], directives: string[]): (N | HtmlBlock | {
115
+ t: "raw";
116
+ s: string;
117
+ })[];
118
+ export declare function convertTwineSelectors(css: string): {
119
+ css: string;
120
+ translated: string[];
121
+ untranslated: string[];
122
+ };
81
123
  export declare function usesOuterLoop(text: string): boolean;
82
124
  export declare function finishText(text: string): string;