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/twine/convert.js
CHANGED
|
@@ -7,6 +7,7 @@ import { attributeRef, print, quote, Unconvertible } from "./expression.js";
|
|
|
7
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
|
|
@@ -18,7 +19,7 @@ export const reservedAttributes = new Set([
|
|
|
18
19
|
"join", "sort", "shuffle", "slice", "first", "last", "indexof", "count", "range",
|
|
19
20
|
"if", "unless", "each", "with", "lookup", "log", "else", "this", "turns", "previous", "previous_section",
|
|
20
21
|
"true", "false", "null", "undefined", "music", "stopmusic", "sound", "stopsounds",
|
|
21
|
-
"addclass", "removeclass", "toggleclass", "hasclass", "content",
|
|
22
|
+
"addclass", "removeclass", "toggleclass", "hasclass", "content", "restart", "saves", "settings", "style",
|
|
22
23
|
]);
|
|
23
24
|
export class StoryContext {
|
|
24
25
|
constructor(twine, passages) {
|
|
@@ -72,6 +73,19 @@ export class StoryContext {
|
|
|
72
73
|
writable: true,
|
|
73
74
|
value: new Map()
|
|
74
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
|
+
});
|
|
75
89
|
Object.defineProperty(this, "problemKeys", {
|
|
76
90
|
enumerable: true,
|
|
77
91
|
configurable: true,
|
|
@@ -307,8 +321,13 @@ export function escapeText(text) {
|
|
|
307
321
|
.replace(/\}\}/g, "}}");
|
|
308
322
|
}
|
|
309
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
|
+
}
|
|
310
329
|
export function sectionLink(text, target, setters = []) {
|
|
311
|
-
const linkText = text == null ? null : text
|
|
330
|
+
const linkText = text == null ? null : escapeLinkBrackets(text);
|
|
312
331
|
if (setters.length == 0 && (linkText == null || linkText == target) && !target.endsWith(">")) {
|
|
313
332
|
return `[[${target}]]`;
|
|
314
333
|
}
|
|
@@ -592,22 +611,37 @@ export function convertTwineStory(twine, options = {}) {
|
|
|
592
611
|
header.push(`@title ${twine.title.replace(/\s+/g, " ").trim()}`);
|
|
593
612
|
if (startSection !== undefined)
|
|
594
613
|
header.push(`@start ${startSection}`);
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
const
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
reason
|
|
605
|
-
}
|
|
606
|
-
|
|
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);
|
|
607
638
|
// The story's JavaScript is left out of it, as it was written for another story format
|
|
608
639
|
const exclude = [];
|
|
609
|
-
|
|
610
|
-
|
|
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";
|
|
611
645
|
exclude.push(`${name}.js`);
|
|
612
646
|
problems.push({
|
|
613
647
|
text: `${name}.js`,
|
|
@@ -623,7 +657,7 @@ export function convertTwineStory(twine, options = {}) {
|
|
|
623
657
|
].filter(s => s.length).join("\n\n") + "\n";
|
|
624
658
|
files[`${name}.squiffy`] = script;
|
|
625
659
|
const map = mapPositions(sectionPassages, story);
|
|
626
|
-
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 } : {}) });
|
|
627
661
|
return {
|
|
628
662
|
title: twine.title,
|
|
629
663
|
format: `${twine.formatName} ${twine.formatVersion}`.trim(),
|
|
@@ -704,10 +738,166 @@ export function convertTagSelectors(css) {
|
|
|
704
738
|
})
|
|
705
739
|
.replace(/(^|[\s,>+~(}])body((?:\.[\w-]+)+)/g, "$1body:has($2)");
|
|
706
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
|
+
}
|
|
707
895
|
// The text of a section or passage: its directives, then its text
|
|
708
896
|
function block(content) {
|
|
709
897
|
const c = typeof content == "string" ? { directives: [], body: content } : content;
|
|
710
|
-
|
|
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");
|
|
711
901
|
}
|
|
712
902
|
// A passage or section made for a link. It's shown on its own, after the link's loop has finished,
|
|
713
903
|
// so it can't use the loop's item or position: StoryContext.linkBody makes them attributes instead.
|
|
@@ -760,7 +950,7 @@ function oneLine(text) {
|
|
|
760
950
|
// Directives the compiler would read at the start of a line
|
|
761
951
|
const directiveLine = /^@(title|import|start|set|unset|inc|dec|ui|clear|end|class)\b/;
|
|
762
952
|
// Lines that only hold helpers that write nothing
|
|
763
|
-
const silentHelper = /\{\{(?:!--[\s\S]*?--|![^}]*|~?\s*(?:#if|#unless|else|\/if|\/unless|set|unset|inc|dec|goto|music|stopmusic|sound|stopsounds|addclass|removeclass|toggleclass|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;
|
|
764
954
|
function isSilent(line) {
|
|
765
955
|
return line.replace(silentHelper, "").trim() == "";
|
|
766
956
|
}
|
package/dist/twine/expression.js
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
// Expressions shared by the Harlowe and SugarCube converters: each parses its own format's
|
|
2
2
|
// expressions into this tree, which is written out as a Squiffy expression, as {{#if}}, @set and
|
|
3
|
-
// {{set}} take (see
|
|
3
|
+
// {{set}} take (see ../infix-expressions.ts).
|
|
4
|
+
// A delay for after=, start= or {{#every}} (#451): a number of milliseconds, or an expression,
|
|
5
|
+
// whose value can be milliseconds or a time like "3s", in parentheses if it's a calculation
|
|
6
|
+
export function delayValue(e) {
|
|
7
|
+
const text = print(e);
|
|
8
|
+
return e.k == "bin" || e.k == "cond" || e.k == "not" || e.k == "neg" ? `(${text})` : text;
|
|
9
|
+
}
|
|
4
10
|
// Thrown when something can't be converted. The message says why, for the list of problems.
|
|
5
11
|
export class Unconvertible extends Error {
|
|
6
12
|
}
|
package/dist/twine/harlowe.js
CHANGED
|
@@ -4,9 +4,9 @@
|
|
|
4
4
|
// variables), which is then written out: (if:) chains become {{#if}}, (set:) becomes @set or
|
|
5
5
|
// {{set}}, (link:) and friends become links to generated passages and sections, and Harlowe's
|
|
6
6
|
// markup becomes Markdown and HTML.
|
|
7
|
-
import { conditional, display, isBoolean, isListCall, lengthOf, member, print, randomInteger, targetName, skipBalanced, skipString, Tokens, Unconvertible, unescapeString } from "./expression.js";
|
|
7
|
+
import { conditional, delayValue, display, isBoolean, isDynamicTarget, isListCall, lengthOf, member, print, randomInteger, targetName, skipBalanced, skipString, Tokens, Unconvertible, unescapeString } from "./expression.js";
|
|
8
8
|
import { audioHelper, leadingAudio, pickSource, STOP_ALL_AUDIO } from "./audio.js";
|
|
9
|
-
import { dynamicSectionLink, escapeText, isUrl, urlLink, passageLink, sectionLink, setter } from "./convert.js";
|
|
9
|
+
import { dynamicSectionLink, escapeText, isUrl, urlLink, passageLink, sectionLink, setter, passageHtmlBlock, readHtmlBlock, topScripts } from "./convert.js";
|
|
10
10
|
// Macro names ignore case, - and _
|
|
11
11
|
function normalise(name) {
|
|
12
12
|
return name.toLowerCase().replace(/[-_]/g, "");
|
|
@@ -59,14 +59,11 @@ class Parser {
|
|
|
59
59
|
i = stop;
|
|
60
60
|
continue;
|
|
61
61
|
}
|
|
62
|
-
const
|
|
63
|
-
if (
|
|
64
|
-
const closeTag = `</${scriptMatch[1].toLowerCase()}>`;
|
|
65
|
-
const end = src.toLowerCase().indexOf(closeTag, i);
|
|
66
|
-
const stop = end == -1 ? src.length : end + closeTag.length;
|
|
62
|
+
const html = readHtmlBlock(src, i);
|
|
63
|
+
if (html) {
|
|
67
64
|
flush();
|
|
68
|
-
nodes.push(
|
|
69
|
-
i =
|
|
65
|
+
nodes.push(html.block);
|
|
66
|
+
i = html.end;
|
|
70
67
|
continue;
|
|
71
68
|
}
|
|
72
69
|
if (c == "`") {
|
|
@@ -843,10 +840,52 @@ class Writer {
|
|
|
843
840
|
passage(nodes) {
|
|
844
841
|
this.collectHooks(nodes);
|
|
845
842
|
const state = { leading: true, directives: [] };
|
|
843
|
+
nodes = this.passageTransition(nodes, state);
|
|
844
|
+
nodes = topScripts(this.ctx, nodes, state.directives);
|
|
846
845
|
let body = this.nodes(nodes, state);
|
|
847
846
|
body += this.closeAll();
|
|
848
847
|
return { directives: state.directives, body };
|
|
849
848
|
}
|
|
849
|
+
// (transition:)[...] around the whole passage becomes @transition (#489), with its
|
|
850
|
+
// (transition-time:), and the passage is the hook's text
|
|
851
|
+
passageTransition(nodes, state) {
|
|
852
|
+
const content = nodes.filter(n => !(n.t == "text" && n.s.trim() == ""));
|
|
853
|
+
const m = content[0];
|
|
854
|
+
if (content.length != 1 || m.t != "macro" || !m.hook)
|
|
855
|
+
return nodes;
|
|
856
|
+
let name;
|
|
857
|
+
let time;
|
|
858
|
+
let hook = { nodes: [{ ...m, hook: foldChangers(m), changers: [] }], raw: m.raw };
|
|
859
|
+
// The changers, from outside in, until the passage's text
|
|
860
|
+
for (;;) {
|
|
861
|
+
const inner = hook?.nodes.filter(n => !(n.t == "text" && n.s.trim() == ""));
|
|
862
|
+
const changer = inner?.length == 1 && inner[0].t == "macro" && inner[0].hook ? inner[0] : undefined;
|
|
863
|
+
if (!changer)
|
|
864
|
+
break;
|
|
865
|
+
const arg = splitArgs(changer.args)[0]?.trim() ?? "";
|
|
866
|
+
const macro = normalise(changer.name);
|
|
867
|
+
const value = literal(arg);
|
|
868
|
+
if ((macro == "transition" || macro == "t8n") && typeof value == "string")
|
|
869
|
+
name = value;
|
|
870
|
+
// A time such as 2s isn't a string in Harlowe
|
|
871
|
+
else if (macro == "transitiontime" || macro == "t8ntime")
|
|
872
|
+
time = arg;
|
|
873
|
+
else
|
|
874
|
+
break;
|
|
875
|
+
hook = changer.hook;
|
|
876
|
+
}
|
|
877
|
+
// A named hook stays a hook, so that it can still be changed
|
|
878
|
+
if (!name || !hook || hook.name || hook.hidden)
|
|
879
|
+
return nodes;
|
|
880
|
+
const transition = harloweTransitions.get(name.toLowerCase());
|
|
881
|
+
if (!transition) {
|
|
882
|
+
this.ctx.story.note("harlowe-transition", `"${name}"`, "Squiffy has no equivalent for these Harlowe transitions, so they've been left out");
|
|
883
|
+
return hook.nodes;
|
|
884
|
+
}
|
|
885
|
+
const ms = time ? /^(\d+(?:\.\d+)?|\.\d+)(s|ms)$/i.exec(time.trim()) : null;
|
|
886
|
+
state.directives.push(`@transition ${transition}` + (ms ? ` ${ms[1]}${ms[2].toLowerCase()}` : ""));
|
|
887
|
+
return hook.nodes;
|
|
888
|
+
}
|
|
850
889
|
closeAll() {
|
|
851
890
|
let s = "";
|
|
852
891
|
for (const tag of [...this.open].reverse())
|
|
@@ -960,6 +999,8 @@ class Writer {
|
|
|
960
999
|
return node.s;
|
|
961
1000
|
case "comment":
|
|
962
1001
|
return this.ctx.unconvertible(node.text, node.reason);
|
|
1002
|
+
case "html":
|
|
1003
|
+
return passageHtmlBlock(this.ctx, node, false).text;
|
|
963
1004
|
case "call":
|
|
964
1005
|
state.leading = false;
|
|
965
1006
|
return this.call(node);
|
|
@@ -1275,28 +1316,30 @@ class Writer {
|
|
|
1275
1316
|
}
|
|
1276
1317
|
case "after": {
|
|
1277
1318
|
state.leading = false;
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1319
|
+
// time is how long the passage has been shown, which is no time at all when this
|
|
1320
|
+
// is worked out, so (after: time + 4s) is (after: 4s)
|
|
1321
|
+
const delay = delayValue(this.expr.parse((args[0] ?? "").replace(/^\s*time\s*\+/, "")));
|
|
1281
1322
|
if (!m.hook)
|
|
1282
1323
|
return "";
|
|
1283
1324
|
const goto = this.gotoOnly(m.hook);
|
|
1284
1325
|
if (goto)
|
|
1285
|
-
return goto.replace(/\}\}$/, ` after=${delay
|
|
1286
|
-
return `{{#animate "fadeIn" start=${delay
|
|
1326
|
+
return goto.replace(/\}\}$/, ` after=${delay}}}`);
|
|
1327
|
+
return `{{#animate "fadeIn" start=${delay}}}${this.hookContent(m.hook, state)}{{/animate}}`;
|
|
1287
1328
|
}
|
|
1288
1329
|
case "live": {
|
|
1289
1330
|
state.leading = false;
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1331
|
+
if (!m.hook)
|
|
1332
|
+
throw new Unconvertible("(live:) without a hook");
|
|
1333
|
+
const delay = delayValue(this.expr.parse(args[0] ?? "1s"));
|
|
1334
|
+
// A hook that only goes somewhere else goes there once
|
|
1293
1335
|
const goto = this.gotoOnly(m.hook, true);
|
|
1294
1336
|
if (goto)
|
|
1295
|
-
return goto.replace(/\}\}$/, ` after=${delay
|
|
1296
|
-
|
|
1337
|
+
return goto.replace(/\}\}$/, ` after=${delay}}}`);
|
|
1338
|
+
// Its hook is shown again every so often, until a (stop:) (#451)
|
|
1339
|
+
return `{{#every ${delay}}}${this.hookContent(m.hook, state)}{{/every}}`;
|
|
1297
1340
|
}
|
|
1298
1341
|
case "stop":
|
|
1299
|
-
return "";
|
|
1342
|
+
return "{{stop}}";
|
|
1300
1343
|
case "inputbox":
|
|
1301
1344
|
case "forceinputbox":
|
|
1302
1345
|
case "input":
|
|
@@ -1307,7 +1350,7 @@ class Writer {
|
|
|
1307
1350
|
state.leading = false;
|
|
1308
1351
|
const bound = this.bound(args[0] ?? "");
|
|
1309
1352
|
if (!bound)
|
|
1310
|
-
throw new Unconvertible("(dropdown:) without bind");
|
|
1353
|
+
throw new Unconvertible("(dropdown:) without bind, or bound to something other than a variable or a value inside one");
|
|
1311
1354
|
const options = args.slice(1).map(a => this.expr.parse(a));
|
|
1312
1355
|
if (options.some(o => o.k != "str" && o.k != "num"))
|
|
1313
1356
|
throw new Unconvertible("(dropdown:) with options that aren't fixed");
|
|
@@ -1401,11 +1444,14 @@ class Writer {
|
|
|
1401
1444
|
case "t8ndelay":
|
|
1402
1445
|
case "transitionskip":
|
|
1403
1446
|
case "t8nskip":
|
|
1447
|
+
// Around the whole passage, this became @transition (#489). Squiffy has none for part of one.
|
|
1448
|
+
this.ctx.story.note("harlowe-transition-hook", m.raw.substring(0, m.raw.indexOf(":") + 1) + ")", "Squiffy's @transition is for a whole passage, so these transitions for part of one have been left out, and the text they changed has been kept");
|
|
1449
|
+
return m.hook ? this.hookContent(m.hook, state) : "";
|
|
1404
1450
|
case "transitiondepart":
|
|
1405
1451
|
case "t8ndepart":
|
|
1406
1452
|
case "transitionarrive":
|
|
1407
1453
|
case "t8narrive":
|
|
1408
|
-
|
|
1454
|
+
this.ctx.story.note("harlowe-transition-link", m.raw.substring(0, m.raw.indexOf(":") + 1) + ")", "Squiffy sets how a passage appears and goes with @transition and @leave in a passage, not on a link, so these have been left out");
|
|
1409
1455
|
return m.hook ? this.hookContent(m.hook, state) : "";
|
|
1410
1456
|
case "nobr":
|
|
1411
1457
|
case "collapse":
|
|
@@ -1750,17 +1796,21 @@ class Writer {
|
|
|
1750
1796
|
const content = this.nodes(named.hook.nodes, { leading: false, directives: [] });
|
|
1751
1797
|
return `{{#replace "${label}"}}${content}{{/replace}}`;
|
|
1752
1798
|
}
|
|
1753
|
-
// The attribute for "bind $x", or
|
|
1799
|
+
// The attribute for "bind $x", or the value inside an object for "bind $hero's name" (#454),
|
|
1800
|
+
// or null
|
|
1754
1801
|
bound(arg) {
|
|
1755
|
-
const match = /^2?bind\s+([$_]
|
|
1802
|
+
const match = /^2?bind\s+([$_][\s\S]*)$/.exec(arg.trim());
|
|
1756
1803
|
if (!match)
|
|
1757
1804
|
return null;
|
|
1758
|
-
|
|
1805
|
+
const e = this.expr.parse(match[1]);
|
|
1806
|
+
if (e.k != "var" || isDynamicTarget(e.name))
|
|
1807
|
+
return null;
|
|
1808
|
+
return e.name;
|
|
1759
1809
|
}
|
|
1760
1810
|
input(args, raw) {
|
|
1761
1811
|
const bound = this.bound(args[0] ?? "");
|
|
1762
1812
|
if (!bound)
|
|
1763
|
-
throw new Unconvertible("an input box without bind");
|
|
1813
|
+
throw new Unconvertible("an input box without bind, or bound to something other than a variable or a value inside one");
|
|
1764
1814
|
let defaultText = null;
|
|
1765
1815
|
let rows = 1;
|
|
1766
1816
|
for (const a of args.slice(1)) {
|
|
@@ -1797,17 +1847,32 @@ class Writer {
|
|
|
1797
1847
|
case "textstyle": {
|
|
1798
1848
|
let open = "";
|
|
1799
1849
|
let close = "";
|
|
1850
|
+
// The {{#style}} effects (#453) with the same look as Harlowe's
|
|
1851
|
+
const effects = [];
|
|
1800
1852
|
for (const a of args) {
|
|
1801
1853
|
const style = str(this.expr.parse(a)).toLowerCase().replace(/[\s-]/g, "");
|
|
1802
|
-
const tag = { bold: "b", italic: "i", underline: "u", strike: "s", superscript: "sup", subscript: "sub" }[style];
|
|
1854
|
+
const tag = { bold: "b", italic: "i", underline: "u", strike: "s", superscript: "sup", subscript: "sub", mark: "mark" }[style];
|
|
1855
|
+
const effect = {
|
|
1856
|
+
shudder: "shudder", rumble: "rumble", sway: "sway", buoy: "buoy", fidget: "shake",
|
|
1857
|
+
blink: "blink", fadeinout: "fade-in-out", blur: "blur", blurrier: "blurrier",
|
|
1858
|
+
smear: "smear", mirror: "mirror", upsidedown: "upside-down",
|
|
1859
|
+
}[style];
|
|
1803
1860
|
if (tag) {
|
|
1804
1861
|
open += `<${tag}>`;
|
|
1805
1862
|
close = `</${tag}>` + close;
|
|
1806
1863
|
}
|
|
1864
|
+
else if (effect) {
|
|
1865
|
+
if (!effects.includes(effect))
|
|
1866
|
+
effects.push(effect);
|
|
1867
|
+
}
|
|
1807
1868
|
else if (style != "none") {
|
|
1808
1869
|
this.ctx.story.note("harlowe-text-style", `"${style}"`, "Squiffy has no equivalent for these Harlowe text styles, so they've been left out");
|
|
1809
1870
|
}
|
|
1810
1871
|
}
|
|
1872
|
+
if (effects.length) {
|
|
1873
|
+
open = `{{#style "${effects.join(" ")}"}}` + open;
|
|
1874
|
+
close += "{{/style}}";
|
|
1875
|
+
}
|
|
1811
1876
|
return open + content + close;
|
|
1812
1877
|
}
|
|
1813
1878
|
case "align": {
|
|
@@ -1984,6 +2049,13 @@ class Writer {
|
|
|
1984
2049
|
return out;
|
|
1985
2050
|
}
|
|
1986
2051
|
}
|
|
2052
|
+
// Harlowe's transitions, as Squiffy's @transition (#489)
|
|
2053
|
+
const harloweTransitions = new Map([
|
|
2054
|
+
["instant", "none"], ["dissolve", "fade"], ["fade", "fade"], ["rumble", "rumble"], ["shudder", "shudder"],
|
|
2055
|
+
["pulse", "pulse"], ["zoom", "zoom"], ["flicker", "flicker"], ["blur", "blur"],
|
|
2056
|
+
["slide-left", "slide-left"], ["slide-right", "slide-right"], ["slide-up", "slide-up"], ["slide-down", "slide-down"],
|
|
2057
|
+
["fade-left", "slide-left"], ["fade-right", "slide-right"], ["fade-up", "slide-up"], ["fade-down", "slide-down"],
|
|
2058
|
+
]);
|
|
1987
2059
|
// (a:)+(b:)[hook] is (a:) applied to (b:)[hook]: the hook, with the changers after the first
|
|
1988
2060
|
// applied to it
|
|
1989
2061
|
function foldChangers(m) {
|