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.
- package/dist/analyse.d.ts +12 -1
- package/dist/analyse.js +156 -9
- package/dist/compiler.d.ts +5 -3
- package/dist/compiler.js +3 -2
- package/dist/css-urls.d.ts +4 -0
- package/dist/css-urls.js +59 -0
- package/dist/emit.d.ts +18 -1
- package/dist/emit.js +46 -0
- package/dist/infix-expressions.d.ts +19 -0
- package/dist/infix-expressions.js +713 -0
- package/dist/language-service.d.ts +1 -0
- package/dist/language-service.js +34 -15
- package/dist/node.d.ts +2 -1
- package/dist/node.js +1 -0
- package/dist/parse.d.ts +4 -0
- package/dist/parse.js +29 -0
- package/dist/project.d.ts +31 -0
- package/dist/project.js +98 -0
- package/dist/quest/convert.js +67 -11
- package/dist/quest/scripts.js +13 -9
- 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.d.ts +4 -0
- package/dist/setters.js +11 -9
- package/dist/story-graph.d.ts +2 -0
- package/dist/story-graph.js +61 -16
- package/dist/syntax.d.ts +24 -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 +45 -11
- package/dist/twine/audio.d.ts +21 -0
- package/dist/twine/audio.js +80 -0
- package/dist/twine/convert.d.ts +45 -3
- package/dist/twine/convert.js +305 -37
- package/dist/twine/expression.d.ts +21 -1
- package/dist/twine/expression.js +105 -8
- package/dist/twine/harlowe.d.ts +9 -0
- package/dist/twine/harlowe.js +461 -86
- package/dist/twine/settings.d.ts +9 -0
- package/dist/twine/settings.js +322 -0
- package/dist/twine/sugarcube.d.ts +3 -0
- package/dist/twine/sugarcube.js +438 -114
- package/package.json +9 -2
- package/syntaxes/squiffy.tmLanguage.json +8 -3
package/dist/twine/convert.js
CHANGED
|
@@ -4,9 +4,10 @@
|
|
|
4
4
|
// editor write the files it returns. Anything that can't be converted is kept as a comment in
|
|
5
5
|
// the story and listed as a problem, so the author can deal with it by hand.
|
|
6
6
|
import { attributeRef, print, quote, Unconvertible } from "./expression.js";
|
|
7
|
-
import { convertHarlowePassage } from "./harlowe.js";
|
|
7
|
+
import { convertHarlowePassage, harloweAudioPassages, harloweMacros } from "./harlowe.js";
|
|
8
8
|
import { convertSugarCubePassage, convertSugarCubeWidgets, sugarCubeWidgets } from "./sugarcube.js";
|
|
9
9
|
import { parseTwine } from "./story.js";
|
|
10
|
+
import { sugarCubeSettings } from "./settings.js";
|
|
10
11
|
import { formatProjectFile } from "../project.js";
|
|
11
12
|
// The Squiffy names that would be read as a helper, rather than an attribute, in {{name}} and
|
|
12
13
|
// expressions, and the ones Squiffy sets itself
|
|
@@ -14,8 +15,11 @@ export const reservedAttributes = new Set([
|
|
|
14
15
|
"get", "set", "unset", "inc", "dec", "seen", "visits", "at", "goto", "embed", "array", "random", "contains",
|
|
15
16
|
"length", "append", "prepend", "pop", "remove", "section", "passage", "label", "replace", "rotate", "sequence",
|
|
16
17
|
"live", "animate", "eq", "ne", "gt", "gte", "lt", "lte", "and", "or", "not", "add", "sub", "mul", "div", "mod",
|
|
18
|
+
"round", "floor", "ceil", "abs", "min", "max", "upper", "lower", "capitalise", "capitalize", "cond",
|
|
19
|
+
"join", "sort", "shuffle", "slice", "first", "last", "indexof", "count", "range",
|
|
17
20
|
"if", "unless", "each", "with", "lookup", "log", "else", "this", "turns", "previous", "previous_section",
|
|
18
|
-
"true", "false", "null", "undefined",
|
|
21
|
+
"true", "false", "null", "undefined", "music", "stopmusic", "sound", "stopsounds",
|
|
22
|
+
"addclass", "removeclass", "toggleclass", "hasclass", "content", "restart", "saves", "settings", "style",
|
|
19
23
|
]);
|
|
20
24
|
export class StoryContext {
|
|
21
25
|
constructor(twine, passages) {
|
|
@@ -69,6 +73,19 @@ export class StoryContext {
|
|
|
69
73
|
writable: true,
|
|
70
74
|
value: new Map()
|
|
71
75
|
});
|
|
76
|
+
// <style>s and <script>s from passages, for the story's CSS and JavaScript files (#455)
|
|
77
|
+
Object.defineProperty(this, "passageStyles", {
|
|
78
|
+
enumerable: true,
|
|
79
|
+
configurable: true,
|
|
80
|
+
writable: true,
|
|
81
|
+
value: []
|
|
82
|
+
});
|
|
83
|
+
Object.defineProperty(this, "passageScripts", {
|
|
84
|
+
enumerable: true,
|
|
85
|
+
configurable: true,
|
|
86
|
+
writable: true,
|
|
87
|
+
value: []
|
|
88
|
+
});
|
|
72
89
|
Object.defineProperty(this, "problemKeys", {
|
|
73
90
|
enumerable: true,
|
|
74
91
|
configurable: true,
|
|
@@ -96,7 +113,8 @@ export class StoryContext {
|
|
|
96
113
|
value: new Map()
|
|
97
114
|
});
|
|
98
115
|
// The loops being converted to {{#each}}, innermost last, with the Twine variables (with their
|
|
99
|
-
// $ or _) that stand for each one's item and position
|
|
116
|
+
// $ or _) that stand for each one's item and position. numbers: its items are numbers, as in
|
|
117
|
+
// a loop that counts. A link's body (see linkBody) is an entry with setters, and no variables.
|
|
100
118
|
Object.defineProperty(this, "loops", {
|
|
101
119
|
enumerable: true,
|
|
102
120
|
configurable: true,
|
|
@@ -194,11 +212,8 @@ export class StoryContext {
|
|
|
194
212
|
this.usedLabels.add(name);
|
|
195
213
|
return name;
|
|
196
214
|
}
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
}
|
|
200
|
-
inLoop(vars, array, convert) {
|
|
201
|
-
this.loops.push({ vars, array });
|
|
215
|
+
inLoop(vars, array, convert, numbers = false) {
|
|
216
|
+
this.loops.push({ vars, array, numbers });
|
|
202
217
|
try {
|
|
203
218
|
return convert();
|
|
204
219
|
}
|
|
@@ -206,12 +221,44 @@ export class StoryContext {
|
|
|
206
221
|
this.loops.pop();
|
|
207
222
|
}
|
|
208
223
|
}
|
|
224
|
+
// Converts the body of a link, which runs when it's clicked, after its loop has finished. In
|
|
225
|
+
// there, the loop's item and position are attributes, which the link sets to that item's (#448).
|
|
226
|
+
// Returns the setters for the link.
|
|
227
|
+
linkBody(convert) {
|
|
228
|
+
if (!this.loops.length || this.loops[this.loops.length - 1].setters)
|
|
229
|
+
return { result: convert(), setters: [] };
|
|
230
|
+
const setters = new Map();
|
|
231
|
+
this.loops.push({ vars: new Map(), setters });
|
|
232
|
+
try {
|
|
233
|
+
return { result: convert(), setters: [...setters.values()] };
|
|
234
|
+
}
|
|
235
|
+
finally {
|
|
236
|
+
this.loops.pop();
|
|
237
|
+
}
|
|
238
|
+
}
|
|
209
239
|
loopVariable(name, temporary) {
|
|
210
240
|
const key = (temporary ? "_" : "$") + name;
|
|
211
241
|
for (let i = this.loops.length - 1; i >= 0; i--) {
|
|
212
|
-
const
|
|
242
|
+
const loop = this.loops[i];
|
|
243
|
+
if (loop.setters) {
|
|
244
|
+
// In a link's body: the variables of the link's own loop are attributes now
|
|
245
|
+
const own = this.loops[i - 1];
|
|
246
|
+
const kind = own.vars.get(key);
|
|
247
|
+
if (kind) {
|
|
248
|
+
const attribute = this.attributeName(name, temporary);
|
|
249
|
+
// Like a link's other setters, an item is text, unless the loop counts
|
|
250
|
+
const value = kind == "index" ? "{{@index}}" : own.numbers ? "{{this}}" : "'{{this}}'";
|
|
251
|
+
loop.setters.set(attribute, `${attribute} = ${value}`);
|
|
252
|
+
return { k: "var", name: attribute };
|
|
253
|
+
}
|
|
254
|
+
if (this.loops.slice(0, i - 1).some(l => l.vars.has(key))) {
|
|
255
|
+
throw new Unconvertible("a link in a loop inside another loop that uses the outer loop's item");
|
|
256
|
+
}
|
|
257
|
+
return undefined;
|
|
258
|
+
}
|
|
259
|
+
const kind = loop.vars.get(key);
|
|
213
260
|
if (kind)
|
|
214
|
-
return { k: "loop", kind, up: this.loops.length - 1 - i, array:
|
|
261
|
+
return { k: "loop", kind, up: this.loops.length - 1 - i, array: loop.array, numbers: loop.numbers };
|
|
215
262
|
}
|
|
216
263
|
return undefined;
|
|
217
264
|
}
|
|
@@ -224,6 +271,9 @@ export class StoryContext {
|
|
|
224
271
|
attribute(name, temporary) {
|
|
225
272
|
if (this.loopVariable(name, temporary))
|
|
226
273
|
throw new Unconvertible("changing a loop's variable inside the loop");
|
|
274
|
+
return this.attributeName(name, temporary);
|
|
275
|
+
}
|
|
276
|
+
attributeName(name, temporary) {
|
|
227
277
|
let result = name;
|
|
228
278
|
if (temporary && this.permanentVars.has(name))
|
|
229
279
|
result = "temp_" + name;
|
|
@@ -271,8 +321,13 @@ export function escapeText(text) {
|
|
|
271
321
|
.replace(/\}\}/g, "}}");
|
|
272
322
|
}
|
|
273
323
|
// A link to a section. text is Squiffy text (already escaped); target is a Squiffy section name.
|
|
324
|
+
// Square brackets in a link's text, which would end the link, as entities. Those in a {{...}} are
|
|
325
|
+
// kept, as the compiler leaves them alone, and an expression needs them, as in {{#if [my flag]}}.
|
|
326
|
+
export function escapeLinkBrackets(text) {
|
|
327
|
+
return text.split(/(\{\{[\s\S]*?\}\})/).map((part, i) => i % 2 ? part : part.replace(/\]/g, "]").replace(/\[/g, "[")).join("");
|
|
328
|
+
}
|
|
274
329
|
export function sectionLink(text, target, setters = []) {
|
|
275
|
-
const linkText = text == null ? null : text
|
|
330
|
+
const linkText = text == null ? null : escapeLinkBrackets(text);
|
|
276
331
|
if (setters.length == 0 && (linkText == null || linkText == target) && !target.endsWith(">")) {
|
|
277
332
|
return `[[${target}]]`;
|
|
278
333
|
}
|
|
@@ -294,9 +349,9 @@ export function dynamicSectionLink(text, target, setters = []) {
|
|
|
294
349
|
const set = setters.length ? ` set=${quote(setters.join(", "))}` : "";
|
|
295
350
|
return `{{section ${print(target)} text=${quote(text)}${set}}}`;
|
|
296
351
|
}
|
|
297
|
-
export function passageLink(text, target) {
|
|
352
|
+
export function passageLink(text, target, setters = []) {
|
|
298
353
|
const linkText = text.replace(/\]/g, "]").replace(/\[/g, "[");
|
|
299
|
-
return `[${linkText}](${target})`;
|
|
354
|
+
return `[${linkText}](${[target, ...setters].join(", ")})`;
|
|
300
355
|
}
|
|
301
356
|
// A Squiffy setter, as in @set or a link, for name = value
|
|
302
357
|
export function setter(name, op, value, inLink) {
|
|
@@ -373,6 +428,9 @@ export function convertTwineStory(twine, options = {}) {
|
|
|
373
428
|
startup.push(p);
|
|
374
429
|
else if (p.tags.some(t => t.startsWith("debug-")))
|
|
375
430
|
skipped.push(p);
|
|
431
|
+
// The Harlowe Audio Library's tracks and settings, which are read where they're used
|
|
432
|
+
else if (harloweAudioPassages.has(p.name))
|
|
433
|
+
continue;
|
|
376
434
|
else
|
|
377
435
|
storyPassages.push(p);
|
|
378
436
|
}
|
|
@@ -408,6 +466,9 @@ export function convertTwineStory(twine, options = {}) {
|
|
|
408
466
|
// Widgets become sections, which calls to them embed. They're named before anything's
|
|
409
467
|
// converted, so calls know where they go.
|
|
410
468
|
sugarCubeWidgets(story);
|
|
469
|
+
// Harlowe's custom macros (#450) too
|
|
470
|
+
if (harlowe)
|
|
471
|
+
harloweMacros(story);
|
|
411
472
|
for (const p of skipped) {
|
|
412
473
|
problems.push({
|
|
413
474
|
passage: p.name, text: p.name,
|
|
@@ -433,7 +494,6 @@ export function convertTwineStory(twine, options = {}) {
|
|
|
433
494
|
return reserved.name;
|
|
434
495
|
},
|
|
435
496
|
reservePassage(hint) {
|
|
436
|
-
const looping = story.looping;
|
|
437
497
|
const base = sanitisePassageName(hint) || "more";
|
|
438
498
|
let name = base;
|
|
439
499
|
for (let n = 2; extraPassages.some(p => p.startsWith(`[${name}]:\n`)) || name == section; n++)
|
|
@@ -446,20 +506,17 @@ export function convertTwineStory(twine, options = {}) {
|
|
|
446
506
|
// Left out, rather than empty, if it can't be converted
|
|
447
507
|
extraPassages[index] = "";
|
|
448
508
|
extraPassages[index] = `[${name}]:\n${generated(content)}`;
|
|
449
|
-
if (looping) {
|
|
450
|
-
story.addProblem({
|
|
451
|
-
passage: passage.name, text: oneLine(hint),
|
|
452
|
-
reason: "This link is in a loop, and every item's link goes to the same passage, so once one has been clicked, they all have.",
|
|
453
|
-
});
|
|
454
|
-
}
|
|
455
509
|
},
|
|
456
510
|
};
|
|
457
511
|
},
|
|
458
512
|
addSection(hint, content) {
|
|
459
513
|
const name = story.uniqueSectionName(hint);
|
|
460
|
-
|
|
514
|
+
this.fillSection(name, content);
|
|
461
515
|
return name;
|
|
462
516
|
},
|
|
517
|
+
fillSection(name, content) {
|
|
518
|
+
story.extraSections.push(`[[${name}]]:\n${generated(content)}`);
|
|
519
|
+
},
|
|
463
520
|
newLabel: hint => story.newLabel(hint),
|
|
464
521
|
});
|
|
465
522
|
const convert = (passage, section, extraPassages) => {
|
|
@@ -517,6 +574,10 @@ export function convertTwineStory(twine, options = {}) {
|
|
|
517
574
|
const extraPassages = [];
|
|
518
575
|
const converted = convert(p, section, extraPassages);
|
|
519
576
|
const directives = [...converted.directives];
|
|
577
|
+
// Its tags (#449), which Twine puts on the page for the stylesheet
|
|
578
|
+
const classes = footers.includes(p) ? [] : tagClasses(p.tags);
|
|
579
|
+
if (classes.length)
|
|
580
|
+
directives.push(`@class ${classes.join(" ")}`);
|
|
520
581
|
let body = converted.body.trim();
|
|
521
582
|
if (p.name == startPassage && (init.directives.length || init.body)) {
|
|
522
583
|
directives.unshift(...init.directives);
|
|
@@ -550,18 +611,37 @@ export function convertTwineStory(twine, options = {}) {
|
|
|
550
611
|
header.push(`@title ${twine.title.replace(/\s+/g, " ").trim()}`);
|
|
551
612
|
if (startSection !== undefined)
|
|
552
613
|
header.push(`@start ${startSection}`);
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
}
|
|
560
|
-
|
|
614
|
+
// The stylesheet, with the <style>s from passages (#455). squiffy.json makes it part of the story.
|
|
615
|
+
const passageStyles = story.passageStyles.filter(s => s.css).map(s => `/* From the passage "${s.passage.replace(/\*\//g, "* /")}" */\n${s.css}`);
|
|
616
|
+
const stylesheet = [twine.stylesheet, ...passageStyles].filter(s => s).join("\n\n");
|
|
617
|
+
if (stylesheet) {
|
|
618
|
+
const tagged = convertTagSelectors(stylesheet);
|
|
619
|
+
const converted = convertTwineSelectors(tagged);
|
|
620
|
+
files[`${name}.css`] = converted.css + "\n";
|
|
621
|
+
const reason = [`The story's stylesheet is in ${name}.css${passageStyles.length ? ", with the <style>s from its passages" : ""}.`];
|
|
622
|
+
if (converted.translated.length) {
|
|
623
|
+
reason.push(`Its rules for Twine's ${list(converted.translated)} are now for Squiffy's own page, sections and links.`);
|
|
624
|
+
}
|
|
625
|
+
if (converted.untranslated.length) {
|
|
626
|
+
// SugarCube's sidebar and dialogs can have a dozen, so only the first few are named
|
|
627
|
+
const shown = converted.untranslated.length > 5 ? [...converted.untranslated.slice(0, 4), `${converted.untranslated.length - 4} more`] : converted.untranslated;
|
|
628
|
+
reason.push(`Its rules for ${list(shown)} have nothing to match in Squiffy.`);
|
|
629
|
+
}
|
|
630
|
+
if (tagged != stylesheet)
|
|
631
|
+
reason.push("Its rules for passage tags now use the classes that @class puts on the story's element.");
|
|
632
|
+
reason.push("Check how it looks.");
|
|
633
|
+
problems.push({ text: `${name}.css`, reason: reason.join(" ") });
|
|
634
|
+
}
|
|
635
|
+
// SugarCube's player settings (#452), from the story's JavaScript
|
|
636
|
+
const settings = !harlowe && twine.script ? sugarCubeSettings(twine.script) : { settings: [], problems: [] };
|
|
637
|
+
problems.push(...settings.problems);
|
|
561
638
|
// The story's JavaScript is left out of it, as it was written for another story format
|
|
562
639
|
const exclude = [];
|
|
563
|
-
|
|
564
|
-
|
|
640
|
+
// With the <script>s from passages that can't run in Squiffy as they are (#455)
|
|
641
|
+
const passageScripts = story.passageScripts.map(s => `// From the passage "${s.passage}"\n${s.js}`);
|
|
642
|
+
const javaScript = [twine.script, ...passageScripts].filter(s => s).join("\n\n");
|
|
643
|
+
if (javaScript) {
|
|
644
|
+
files[`${name}.js`] = javaScript + "\n";
|
|
565
645
|
exclude.push(`${name}.js`);
|
|
566
646
|
problems.push({
|
|
567
647
|
text: `${name}.js`,
|
|
@@ -577,7 +657,7 @@ export function convertTwineStory(twine, options = {}) {
|
|
|
577
657
|
].filter(s => s.length).join("\n\n") + "\n";
|
|
578
658
|
files[`${name}.squiffy`] = script;
|
|
579
659
|
const map = mapPositions(sectionPassages, story);
|
|
580
|
-
files["squiffy.json"] = formatProjectFile({ main: `${name}.squiffy`, exclude, panels, ...(map ? { map } : {}) });
|
|
660
|
+
files["squiffy.json"] = formatProjectFile({ main: `${name}.squiffy`, exclude, panels, settings: settings.settings, ...(map ? { map } : {}) });
|
|
581
661
|
return {
|
|
582
662
|
title: twine.title,
|
|
583
663
|
format: `${twine.formatName} ${twine.formatVersion}`.trim(),
|
|
@@ -626,13 +706,201 @@ function unarranged(passages) {
|
|
|
626
706
|
const columns = new Set(onGrid.map(([x]) => x));
|
|
627
707
|
return columns.size == 10 && onGrid.length >= positions.length * 0.9;
|
|
628
708
|
}
|
|
709
|
+
// Tags that mean something to Harlowe, SugarCube or Twine itself, rather than ones the story
|
|
710
|
+
// gave its passages
|
|
711
|
+
const SPECIAL_TAGS = new Set(["header", "footer", "startup", "widget", "nobr", "init", "stylesheet", "script"]);
|
|
712
|
+
// A passage's tags, which become @class (#449): Harlowe and SugarCube put them on the page
|
|
713
|
+
export function tagClasses(tags) {
|
|
714
|
+
return [...new Set(tags.filter(t => !SPECIAL_TAGS.has(t) && !t.startsWith("debug-") && !t.startsWith("Twine.")))];
|
|
715
|
+
}
|
|
716
|
+
// A class name in a CSS selector, escaped if it needs to be
|
|
717
|
+
function cssClass(name) {
|
|
718
|
+
if (/^-?[A-Za-z_][\w-]*$/.test(name))
|
|
719
|
+
return "." + name;
|
|
720
|
+
return "." + [...name].map((c, i) => {
|
|
721
|
+
if (/[\w-]/.test(c) && !(i == 0 && /\d/.test(c)))
|
|
722
|
+
return c;
|
|
723
|
+
if (/\d/.test(c))
|
|
724
|
+
return `\\${c.charCodeAt(0).toString(16)} `;
|
|
725
|
+
return "\\" + c;
|
|
726
|
+
}).join("");
|
|
727
|
+
}
|
|
728
|
+
// Rules for passage tags (#449), e.g. tw-story[tags~="dark"] in Harlowe or
|
|
729
|
+
// body[data-tags~="dark"] in SugarCube, become rules for the classes @class puts on the story's
|
|
730
|
+
// element, as do SugarCube's classes on the body (from tags, or <<addclass "body">>). In Twine,
|
|
731
|
+
// tw-story, html and body are the whole page, so they become body:has(.dark), and the passage
|
|
732
|
+
// becomes the story's element.
|
|
733
|
+
export function convertTagSelectors(css) {
|
|
734
|
+
return css
|
|
735
|
+
.replace(/(\b(?:tw-story|html|body)|tw-passage|\.passage|#passages|#story)?((?:\[\s*(?:data-)?tags\s*~=\s*(["']?)[^"'\]\s]+\3\s*\])+)/g, (_, element, attributes) => {
|
|
736
|
+
const classes = [...attributes.matchAll(/~=\s*(["']?)([^"'\]\s]+)\1/g)].map(m => cssClass(m[2])).join("");
|
|
737
|
+
return element && /^(?:tw-story|html|body)$/.test(element) ? `body:has(${classes})` : classes;
|
|
738
|
+
})
|
|
739
|
+
.replace(/(^|[\s,>+~(}])body((?:\.[\w-]+)+)/g, "$1body:has($2)");
|
|
740
|
+
}
|
|
741
|
+
// The <script> or <style> at src[i], and where it ends
|
|
742
|
+
export function readHtmlBlock(src, i) {
|
|
743
|
+
const open = /^<(script|style)\b([^>]*)>/i.exec(src.substring(i));
|
|
744
|
+
if (!open)
|
|
745
|
+
return undefined;
|
|
746
|
+
const tag = open[1].toLowerCase();
|
|
747
|
+
const close = src.toLowerCase().indexOf(`</${tag}>`, i + open[0].length);
|
|
748
|
+
const end = close == -1 ? src.length : close + tag.length + 3;
|
|
749
|
+
const content = src.substring(i + open[0].length, close == -1 ? src.length : close);
|
|
750
|
+
return { block: { t: "html", tag, raw: src.substring(i, end), attributes: open[2], content }, end };
|
|
751
|
+
}
|
|
752
|
+
// JavaScript that uses the story format's own objects, such as SugarCube's State and setup, or
|
|
753
|
+
// jQuery, which Squiffy doesn't have
|
|
754
|
+
const formatJavaScript = /\b(?:State|setup|Engine|Story|Config|Macro|Save|UI|UIBar|Dialog|Settings?|SimpleAudio|Wikifier|Template|LoadScreen|Scripting|Harlowe|SugarCube|V|T)\s*\.|\b(?:variables|temporary|visited|visitedTags|passage|previous|tags|turns|jQuery|either|random|randomFloat|lastVisited|memorize|recall|forget)\s*\(|\$\s*\(|\$\.\w/;
|
|
755
|
+
// A <style> in a passage goes in the story's stylesheet. A <script> at the start of a passage
|
|
756
|
+
// (top) runs when its section is shown, once its text is on the page, as it would in Twine, and
|
|
757
|
+
// the directive that does that is returned. One that uses the story format's JavaScript, or is
|
|
758
|
+
// inside a macro or hook so it only ran some of the time, goes in the story's JavaScript file,
|
|
759
|
+
// which isn't part of the story.
|
|
760
|
+
export function passageHtmlBlock(ctx, block, top) {
|
|
761
|
+
const story = ctx.story;
|
|
762
|
+
if (block.tag == "style") {
|
|
763
|
+
story.passageStyles.push({ passage: ctx.passage.name, css: block.content.trim() });
|
|
764
|
+
ctx.approximate("<style>", "This is in the story's stylesheet now, so it applies all the time, not only while this passage is shown.");
|
|
765
|
+
return { text: "" };
|
|
766
|
+
}
|
|
767
|
+
if (/\bsrc\s*=/i.test(block.attributes)) {
|
|
768
|
+
return { text: ctx.unconvertible(block.raw, "A <script> that loads a file isn't run by Squiffy. Add the file to the story's folder instead.") };
|
|
769
|
+
}
|
|
770
|
+
const js = block.content.replace(/^\s*\n|\s+$/g, "");
|
|
771
|
+
if (!js.trim())
|
|
772
|
+
return { text: "" };
|
|
773
|
+
if (!top || formatJavaScript.test(js)) {
|
|
774
|
+
story.passageScripts.push({ passage: ctx.passage.name, js });
|
|
775
|
+
ctx.approximate("<script>", top
|
|
776
|
+
? "This <script> uses the story format's JavaScript, which Squiffy doesn't have, so it's in the story's JavaScript file, which isn't part of the story."
|
|
777
|
+
: "This <script> only ran some of the time, so it's in the story's JavaScript file, which isn't part of the story.");
|
|
778
|
+
return { text: "" };
|
|
779
|
+
}
|
|
780
|
+
ctx.approximate("<script>", "This runs when the section is shown, once its text is on the page. Check it still works.");
|
|
781
|
+
// A blank line would end the JavaScript, so blank lines are left out
|
|
782
|
+
const lines = dedent(js).split("\n").filter(l => l.trim());
|
|
783
|
+
return { text: "", directive: [" squiffy.ui.afterWrite(() => {", ...lines.map(l => " " + l), " });"].join("\n") };
|
|
784
|
+
}
|
|
785
|
+
// A passage's nodes, with the <script>s that aren't inside a macro or hook taken out and their
|
|
786
|
+
// JavaScript added to its directives
|
|
787
|
+
export function topScripts(ctx, nodes, directives) {
|
|
788
|
+
return nodes.map(n => {
|
|
789
|
+
if (n.t != "html" || n.tag != "script")
|
|
790
|
+
return n;
|
|
791
|
+
const { text, directive } = passageHtmlBlock(ctx, n, true);
|
|
792
|
+
if (directive)
|
|
793
|
+
directives.push(directive);
|
|
794
|
+
return { t: "raw", s: text };
|
|
795
|
+
});
|
|
796
|
+
}
|
|
797
|
+
// Lines with the indent they all have taken off
|
|
798
|
+
function dedent(text) {
|
|
799
|
+
const lines = text.split("\n");
|
|
800
|
+
const indent = Math.min(...lines.filter(l => l.trim()).map(l => /^[ \t]*/.exec(l)[0].length));
|
|
801
|
+
return lines.map(l => l.substring(Math.min(indent, /^[ \t]*/.exec(l)[0].length))).join("\n");
|
|
802
|
+
}
|
|
803
|
+
// Twine's elements in a stylesheet, and the Squiffy elements their rules are for now (#455). In
|
|
804
|
+
// Harlowe, tw-story is the whole page and tw-passage the passage; in SugarCube, #story holds
|
|
805
|
+
// #passages, which holds the .passage. A Twine passage is a Squiffy section.
|
|
806
|
+
const twineSelectors = [
|
|
807
|
+
[/(?<![\w#.-])tw-story(?![\w-])/g, "body", "tw-story"],
|
|
808
|
+
[/(?<![\w#.-])tw-passage(?![\w-])/g, ".squiffy-output-section", "tw-passage"],
|
|
809
|
+
[/(?<![\w#.-])tw-link(?![\w-])/g, ".squiffy-link", "tw-link"],
|
|
810
|
+
[/\.enchantment-link(?![\w-])/g, ".squiffy-link", ".enchantment-link"],
|
|
811
|
+
[/(?<![\w#.-])tw-sidebar(?![\w-])/g, "#squiffy-header", "tw-sidebar"],
|
|
812
|
+
[/#story(?![\w-])/g, "#squiffy-container", "#story"],
|
|
813
|
+
[/#passages(?![\w-])/g, "#squiffy", "#passages"],
|
|
814
|
+
[/\.passage(?![\w-])/g, ".squiffy-output-section", ".passage"],
|
|
815
|
+
[/\.(?:link-internal|macro-link)(?![\w-])/g, ".squiffy-link", ".link-internal"],
|
|
816
|
+
];
|
|
817
|
+
// Twine's elements that have nothing like them in Squiffy
|
|
818
|
+
const otherTwineSelectors = /(?<![\w#.-])tw-[\w-]+|#(?:ui-[\w-]+|menu[\w-]*|story-[\w-]+)(?![\w-])/g;
|
|
819
|
+
// A stylesheet with its rules for Twine's elements changed to be for Squiffy's (#455), and the
|
|
820
|
+
// Twine elements it had rules for, changed or not. Only selectors change, not what's in the rules.
|
|
821
|
+
export function convertTwineSelectors(css) {
|
|
822
|
+
const translated = new Set();
|
|
823
|
+
const untranslated = new Set();
|
|
824
|
+
const selector = (text) => {
|
|
825
|
+
for (const [pattern, squiffy, twine] of twineSelectors) {
|
|
826
|
+
text = text.replace(pattern, () => {
|
|
827
|
+
translated.add(twine);
|
|
828
|
+
return squiffy;
|
|
829
|
+
});
|
|
830
|
+
}
|
|
831
|
+
for (const m of text.matchAll(otherTwineSelectors))
|
|
832
|
+
untranslated.add(m[0]);
|
|
833
|
+
return text;
|
|
834
|
+
};
|
|
835
|
+
let out = "";
|
|
836
|
+
let prelude = "";
|
|
837
|
+
// Whether each block that's open is a rule's declarations, or holds rules, like @media
|
|
838
|
+
const blocks = [];
|
|
839
|
+
for (let i = 0; i < css.length; i++) {
|
|
840
|
+
const c = css[i];
|
|
841
|
+
const inRule = blocks.length > 0 && blocks[blocks.length - 1];
|
|
842
|
+
let skip = 0;
|
|
843
|
+
if (c == "/" && css[i + 1] == "*") {
|
|
844
|
+
const end = css.indexOf("*/", i + 2);
|
|
845
|
+
skip = (end == -1 ? css.length : end + 2) - i;
|
|
846
|
+
}
|
|
847
|
+
else if (c == "\"" || c == "'") {
|
|
848
|
+
let j = i + 1;
|
|
849
|
+
while (j < css.length && css[j] != c && css[j] != "\n")
|
|
850
|
+
j += css[j] == "\\" ? 2 : 1;
|
|
851
|
+
skip = j + 1 - i;
|
|
852
|
+
}
|
|
853
|
+
if (skip) {
|
|
854
|
+
// A comment or string goes into the prelude, so a selector isn't changed inside one
|
|
855
|
+
if (inRule)
|
|
856
|
+
out += css.substring(i, i + skip);
|
|
857
|
+
else
|
|
858
|
+
prelude += css.substring(i, i + skip);
|
|
859
|
+
i += skip - 1;
|
|
860
|
+
continue;
|
|
861
|
+
}
|
|
862
|
+
if (inRule) {
|
|
863
|
+
if (c == "{")
|
|
864
|
+
blocks.push(true);
|
|
865
|
+
else if (c == "}")
|
|
866
|
+
blocks.pop();
|
|
867
|
+
out += c;
|
|
868
|
+
continue;
|
|
869
|
+
}
|
|
870
|
+
if (c == "{") {
|
|
871
|
+
const atRule = prelude.trimStart().startsWith("@");
|
|
872
|
+
out += (atRule ? prelude : selectorOutsideComments(prelude, selector)) + c;
|
|
873
|
+
prelude = "";
|
|
874
|
+
blocks.push(!atRule);
|
|
875
|
+
}
|
|
876
|
+
else if (c == "}" || c == ";") {
|
|
877
|
+
out += prelude + c;
|
|
878
|
+
prelude = "";
|
|
879
|
+
if (c == "}")
|
|
880
|
+
blocks.pop();
|
|
881
|
+
}
|
|
882
|
+
else
|
|
883
|
+
prelude += c;
|
|
884
|
+
}
|
|
885
|
+
return { css: out + prelude, translated: [...translated], untranslated: [...untranslated] };
|
|
886
|
+
}
|
|
887
|
+
// "a, b and c"
|
|
888
|
+
function list(items) {
|
|
889
|
+
return items.length < 2 ? items.join("") : `${items.slice(0, -1).join(", ")} and ${items[items.length - 1]}`;
|
|
890
|
+
}
|
|
891
|
+
// A selector changed, leaving its comments and strings as they are
|
|
892
|
+
function selectorOutsideComments(text, change) {
|
|
893
|
+
return text.split(/(\/\*[\s\S]*?(?:\*\/|$)|"(?:[^"\\\n]|\\.)*"?|'(?:[^'\\\n]|\\.)*'?)/).map((part, n) => n % 2 ? part : change(part)).join("");
|
|
894
|
+
}
|
|
629
895
|
// The text of a section or passage: its directives, then its text
|
|
630
896
|
function block(content) {
|
|
631
897
|
const c = typeof content == "string" ? { directives: [], body: content } : content;
|
|
632
|
-
|
|
898
|
+
// JavaScript goes after the directives, before the text
|
|
899
|
+
const js = (d) => /^\s/.test(d);
|
|
900
|
+
return [...c.directives.filter(d => !js(d)), ...c.directives.filter(js), finishText(c.body)].filter(l => l.length).join("\n");
|
|
633
901
|
}
|
|
634
|
-
// A passage or section made for a link
|
|
635
|
-
//
|
|
902
|
+
// A passage or section made for a link. It's shown on its own, after the link's loop has finished,
|
|
903
|
+
// so it can't use the loop's item or position: StoryContext.linkBody makes them attributes instead.
|
|
636
904
|
function generated(content) {
|
|
637
905
|
const c = typeof content == "string" ? { directives: [], body: content } : content;
|
|
638
906
|
// Directives are expressions, rather than text with {{...}} in it
|
|
@@ -680,9 +948,9 @@ function oneLine(text) {
|
|
|
680
948
|
return line.length > 120 ? line.substring(0, 117) + "..." : line;
|
|
681
949
|
}
|
|
682
950
|
// Directives the compiler would read at the start of a line
|
|
683
|
-
const directiveLine = /^@(title|import|start|set|unset|inc|dec|ui|clear|end)\b/;
|
|
951
|
+
const directiveLine = /^@(title|import|start|set|unset|inc|dec|ui|clear|end|class)\b/;
|
|
684
952
|
// Lines that only hold helpers that write nothing
|
|
685
|
-
const silentHelper = /\{\{(?:!--[\s\S]*?--|![^}]*|~?\s*(?:#if|#unless|else|\/if|\/unless|set|unset|inc|dec|goto|append|prepend|remove|#label|\/label|#replace|\/replace|#animate|\/animate)\b(?:[^{}]|\{\{[^{}]*\}\})*)\}\}/g;
|
|
953
|
+
const silentHelper = /\{\{(?:!--[\s\S]*?--|![^}]*|~?\s*(?:#if|#unless|#once|#firsttime|else|\/if|\/unless|\/once|\/firsttime|set|unset|inc|dec|goto|music|stopmusic|sound|stopsounds|addclass|removeclass|toggleclass|append|prepend|remove|#label|\/label|#replace|\/replace|#animate|\/animate)\b(?:[^{}]|\{\{[^{}]*\}\})*)\}\}/g;
|
|
686
954
|
function isSilent(line) {
|
|
687
955
|
return line.replace(silentHelper, "").trim() == "";
|
|
688
956
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export type BinaryOp = "or" | "and" | "=" | "!=" | ">" | ">=" | "<" | "<=" | "+" | "-" | "*" | "/" | "%";
|
|
1
|
+
export type BinaryOp = "or" | "and" | "=" | "!=" | ">" | ">=" | "<" | "<=" | "contains" | "+" | "-" | "*" | "/" | "%";
|
|
2
2
|
export type Expr = {
|
|
3
3
|
k: "num";
|
|
4
4
|
v: number;
|
|
@@ -24,6 +24,11 @@ export type Expr = {
|
|
|
24
24
|
} | {
|
|
25
25
|
k: "neg";
|
|
26
26
|
e: Expr;
|
|
27
|
+
} | {
|
|
28
|
+
k: "cond";
|
|
29
|
+
test: Expr;
|
|
30
|
+
then: Expr;
|
|
31
|
+
otherwise: Expr;
|
|
27
32
|
} | {
|
|
28
33
|
k: "call";
|
|
29
34
|
fn: string;
|
|
@@ -31,12 +36,21 @@ export type Expr = {
|
|
|
31
36
|
} | {
|
|
32
37
|
k: "array";
|
|
33
38
|
items: Expr[];
|
|
39
|
+
} | {
|
|
40
|
+
k: "object";
|
|
41
|
+
entries: [string, Expr][];
|
|
42
|
+
} | {
|
|
43
|
+
k: "member";
|
|
44
|
+
e: Expr;
|
|
45
|
+
key: Expr;
|
|
34
46
|
} | {
|
|
35
47
|
k: "loop";
|
|
36
48
|
kind: "item" | "index";
|
|
37
49
|
up: number;
|
|
38
50
|
array?: string;
|
|
51
|
+
numbers?: boolean;
|
|
39
52
|
};
|
|
53
|
+
export declare function delayValue(e: Expr): string;
|
|
40
54
|
export declare class Unconvertible extends Error {
|
|
41
55
|
}
|
|
42
56
|
export interface PrintOptions {
|
|
@@ -45,8 +59,14 @@ export interface PrintOptions {
|
|
|
45
59
|
}
|
|
46
60
|
export declare function quote(text: string, options?: PrintOptions): string;
|
|
47
61
|
export declare function attributeRef(name: string): string;
|
|
62
|
+
export declare function isDynamicTarget(name: string): boolean;
|
|
63
|
+
export declare function member(e: Expr, key: Expr): Expr;
|
|
64
|
+
export declare function targetName(e: Expr): string | null;
|
|
48
65
|
export declare function print(e: Expr, options?: PrintOptions): string;
|
|
66
|
+
export declare function isListCall(e: Expr): boolean;
|
|
49
67
|
export declare function isBoolean(e: Expr): boolean;
|
|
68
|
+
export declare function lengthOf(e: Expr): Expr;
|
|
69
|
+
export declare function conditional(values: Expr[], what: string): Expr;
|
|
50
70
|
export declare function randomInteger(min: Expr, max: Expr): Expr;
|
|
51
71
|
export declare function display(e: Expr): string;
|
|
52
72
|
export declare class Tokens<T extends {
|