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/analyse.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Diagnostic, PassageNode, SectionNode, SquiffyFile } from "./syntax.js";
1
+ import type { Diagnostic, PassageNode, Range, SectionNode, SquiffyFile } from "./syntax.js";
2
2
  import { SourceLine } from "./text.js";
3
3
  export interface ExternalFiles {
4
4
  getMatchingFilenames(pattern: string): Promise<string[]>;
@@ -55,6 +55,17 @@ declare class TextContainer {
55
55
  js: string[];
56
56
  clear: boolean;
57
57
  attributes: string[];
58
+ music?: string | null;
59
+ sounds: string[];
60
+ classes: string[];
61
+ transition?: string;
62
+ transitionTime?: number;
63
+ leave?: string;
64
+ leaveTime?: number;
65
+ leaveNode?: {
66
+ filename: string | undefined;
67
+ range: Range;
68
+ };
58
69
  output: string;
59
70
  addText(text: string, source: SourceLine): void;
60
71
  }
package/dist/analyse.js CHANGED
@@ -2,6 +2,8 @@ import { parse } from "./parse.js";
2
2
  import { positionMapper, transformText } from "./text.js";
3
3
  import { isScriptPath, isSquiffyPath, isStylesheetPath, PROJECT_FILENAME } from "./project.js";
4
4
  import { findV5Syntax } from "./v5.js";
5
+ import { findTemplateError } from "./templates.js";
6
+ import { sectionLabels } from "./section-labels.js";
5
7
  import { attributesSetIn, BUILT_IN_ATTRIBUTES, findAtAttributes, findBuiltInAttributeSets, findUnquotedText } from "./setters.js";
6
8
  // Caches the syntax tree of each file by filename, reparsing a file only when its text changes.
7
9
  export class ParseCache {
@@ -109,6 +111,60 @@ class TextContainer {
109
111
  writable: true,
110
112
  value: []
111
113
  });
114
+ // @music (#444): the file to play, or null for @music stop. Undefined leaves it as it is.
115
+ Object.defineProperty(this, "music", {
116
+ enumerable: true,
117
+ configurable: true,
118
+ writable: true,
119
+ value: void 0
120
+ });
121
+ // @sound: the files to play once, in order, with "stop" for @sound stop
122
+ Object.defineProperty(this, "sounds", {
123
+ enumerable: true,
124
+ configurable: true,
125
+ writable: true,
126
+ value: []
127
+ });
128
+ // @class (#449): classes on the story's element while the section is the current one
129
+ Object.defineProperty(this, "classes", {
130
+ enumerable: true,
131
+ configurable: true,
132
+ writable: true,
133
+ value: []
134
+ });
135
+ // @transition (#489): how it appears, and how long that takes in milliseconds
136
+ Object.defineProperty(this, "transition", {
137
+ enumerable: true,
138
+ configurable: true,
139
+ writable: true,
140
+ value: void 0
141
+ });
142
+ Object.defineProperty(this, "transitionTime", {
143
+ enumerable: true,
144
+ configurable: true,
145
+ writable: true,
146
+ value: void 0
147
+ });
148
+ // @leave: how the old text goes when this clears the screen, and how long that takes
149
+ Object.defineProperty(this, "leave", {
150
+ enumerable: true,
151
+ configurable: true,
152
+ writable: true,
153
+ value: void 0
154
+ });
155
+ Object.defineProperty(this, "leaveTime", {
156
+ enumerable: true,
157
+ configurable: true,
158
+ writable: true,
159
+ value: void 0
160
+ });
161
+ // Where @leave is, to say if there's nothing for it to do
162
+ Object.defineProperty(this, "leaveNode", {
163
+ enumerable: true,
164
+ configurable: true,
165
+ writable: true,
166
+ value: void 0
167
+ });
112
168
  // The text after links have been converted
113
169
  Object.defineProperty(this, "output", {
114
170
  enumerable: true,
@@ -194,6 +250,13 @@ export class Passage extends TextContainer {
194
250
  }
195
251
  export const analysisInternals = new WeakMap();
196
252
  const lineRange = (node) => node.range;
253
+ // A @transition time (#489), such as "1s", "0.5s" or "400ms", in milliseconds
254
+ function parseTransitionTime(time) {
255
+ const match = /^(\d+(?:\.\d+)?|\.\d+)(s|ms)$/.exec(time);
256
+ if (!match)
257
+ return undefined;
258
+ return Math.round(parseFloat(match[1]) * (match[2] == "s" ? 1000 : 1));
259
+ }
197
260
  // Resolves @imports, builds the story's sections and passages, and reports diagnostics.
198
261
  export async function analyse(settings) {
199
262
  const story = new Story();
@@ -417,6 +480,53 @@ export async function analyse(settings) {
417
480
  }
418
481
  return true;
419
482
  }
483
+ case "sound": {
484
+ const target = passage ?? (section = ensureSectionExists(lineCount));
485
+ if (!target)
486
+ return sectionRequired(node);
487
+ const value = node.value.trim();
488
+ if (node.directive == "sound")
489
+ target.sounds.push(value);
490
+ else
491
+ target.music = value == "stop" ? null : value;
492
+ return true;
493
+ }
494
+ case "class": {
495
+ const target = passage ?? (section = ensureSectionExists(lineCount));
496
+ if (!target)
497
+ return sectionRequired(node);
498
+ for (const name of node.value.split(/\s+/)) {
499
+ if (name && !target.classes.includes(name))
500
+ target.classes.push(name);
501
+ }
502
+ return true;
503
+ }
504
+ case "transition": {
505
+ const target = passage ?? (section = ensureSectionExists(lineCount));
506
+ if (!target)
507
+ return sectionRequired(node);
508
+ if (!/^[A-Za-z][\w-]*$/.test(node.value)) {
509
+ report("warning", "squiffy/transition", filename, node.valueRange, `"${node.value}" can't be the name of a transition, so this was ignored. A name is letters, numbers, - and _, starting with a letter.`, lineCount);
510
+ return true;
511
+ }
512
+ let time;
513
+ if (node.time !== undefined) {
514
+ time = parseTransitionTime(node.time);
515
+ if (time === undefined) {
516
+ report("warning", "squiffy/transition", filename, lineRange(node), `"${node.time}" isn't a time, such as 1s or 400ms, so the transition takes its usual time.`, lineCount);
517
+ }
518
+ }
519
+ if (node.directive == "leave") {
520
+ target.leave = node.value;
521
+ target.leaveTime = time;
522
+ target.leaveNode = { filename, range: lineRange(node) };
523
+ }
524
+ else {
525
+ target.transition = node.value;
526
+ target.transitionTime = time;
527
+ }
528
+ return true;
529
+ }
420
530
  case "attribute": {
421
531
  section = ensureSectionExists(lineCount);
422
532
  let attribute;
@@ -542,6 +652,8 @@ export async function analyse(settings) {
542
652
  // Set once the sections are all known, before checking links
543
653
  let sectionNames = [];
544
654
  let sectionIndexes = new Map();
655
+ // What warnings call sections without names of their own, e.g. 'the "+++Continue..." after "cave"'
656
+ let labels = new Map();
545
657
  function checkText(container, section, passage) {
546
658
  const getNextSectionName = () => {
547
659
  return sectionNames[(sectionIndexes.get(section.name) ?? -1) + 1] || null;
@@ -550,32 +662,48 @@ export async function analyse(settings) {
550
662
  container.output = result.output;
551
663
  const toPosition = positionMapper(container.textSources, container.text.map(t => t.length));
552
664
  const toRange = (start, end) => ({ start: toPosition(start), end: toPosition(end) });
553
- const where = passage
554
- ? `In section '${section.name}', passage '${passage.name}'`
555
- : `In section '${section.name}'`;
665
+ const label = labels.get(section.name);
666
+ const inSection = label && section.node?.kind != "named" ? `In ${label}` : `In section '${section.name}'`;
667
+ const where = passage ? `${inSection}, passage '${passage.name}'` : inSection;
556
668
  const legacyLine = passage ? passage.line : section.line;
557
669
  const warn = (code, start, end, message, line = legacyLine) => {
558
670
  report("warning", code, section.filename, toRange(start, end), message, line);
559
671
  };
672
+ // Handlebars syntax errors, which would stop the text being shown at all
673
+ const templateError = findTemplateError(result.output);
674
+ if (templateError) {
675
+ const range = toRange(result.mapStart(templateError.start), result.mapEnd(templateError.end));
676
+ report("error", "squiffy/template-syntax", section.filename, range, `${where}, ${templateError.message}`, range.start.line + 1);
677
+ }
560
678
  for (const missing of result.noNextSection) {
561
- warn("squiffy/no-next-section", missing.start, missing.end, `In section '${section.name}', there is a ${missing.display} link but no following section exists`, section.line);
679
+ warn("squiffy/no-next-section", missing.start, missing.end, `${inSection}, there is a ${missing.display} link but no following section exists`, section.line);
562
680
  }
681
+ // Like the runtime, a passage link can go to one of the master section's passages
682
+ const masterPassages = story.sections[""]?.passages ?? {};
563
683
  for (const link of result.links) {
564
684
  if (link.form == "next")
565
685
  continue;
566
- if (link.linkType == "section") {
686
+ if (link.form == "input") {
687
+ const exists = link.linkType == "section"
688
+ ? hasOwn(story.sections, link.target)
689
+ : hasOwn(section.passages, link.target) || hasOwn(masterPassages, link.target);
690
+ if (!exists) {
691
+ warn(link.linkType == "section" ? "squiffy/unknown-section-link" : "squiffy/unknown-passage-link", link.start, link.end, `${where} there is a text box that goes to a ${link.linkType} called "${link.target}" when Enter is pressed, which doesn't exist`);
692
+ }
693
+ }
694
+ else if (link.linkType == "section") {
567
695
  if (!linkDestinationExists(link.target, story.sections)) {
568
696
  warn("squiffy/unknown-section-link", link.start, link.end, `${where} there is a link to a section called [[${link.target}]], which doesn't exist`);
569
697
  }
570
698
  }
571
- else if (!linkDestinationExists(link.target, section.passages)) {
699
+ else if (!linkDestinationExists(link.target, section.passages) && !linkDestinationExists(link.target, masterPassages)) {
572
700
  warn("squiffy/unknown-passage-link", link.start, link.end, `${where} there is a link to a passage called [${link.target}], which doesn't exist`);
573
701
  }
574
702
  }
575
703
  // Like the runtime's embed helper, a name can refer to a passage in the
576
- // current section or to any section.
704
+ // current section or the master section, or to any section.
577
705
  for (const embed of result.embeds) {
578
- if (!(embed.name in section.passages) && !(embed.name in story.sections)) {
706
+ if (!(embed.name in section.passages) && !(embed.name in story.sections) && !hasOwn(masterPassages, embed.name)) {
579
707
  warn("squiffy/unknown-embed", embed.start, embed.end, `${where} there is an embed of {{embed "${embed.name}"}}, but no section or passage with that name exists`);
580
708
  }
581
709
  }
@@ -587,7 +715,6 @@ export async function analyse(settings) {
587
715
  }
588
716
  }
589
717
  // {{deactivate}} disables links to a passage in this section, or in the master section
590
- const masterPassages = story.sections[""]?.passages ?? {};
591
718
  for (const deactivate of result.deactivates) {
592
719
  if (!(deactivate.name in section.passages) && !(deactivate.name in masterPassages)) {
593
720
  warn("squiffy/unknown-deactivate", deactivate.start, deactivate.end, `${where} there is a {{deactivate "${deactivate.name}"}}, but no passage with that name exists in this section`);
@@ -624,12 +751,15 @@ export async function analyse(settings) {
624
751
  }
625
752
  sectionNames = Object.keys(story.sections);
626
753
  sectionIndexes = new Map(sectionNames.map((name, i) => [name, i]));
754
+ const nodeNames = new Map(Object.values(story.sections).flatMap(section => section.node ? [[section.node, section.name]] : []));
755
+ labels = sectionLabels(files, node => nodeNames.get(node));
627
756
  for (const section of Object.values(story.sections)) {
628
757
  checkText(section, section, null);
629
758
  for (const passage of Object.values(section.passages)) {
630
759
  checkText(passage, section, passage);
631
760
  }
632
761
  }
762
+ success && (success = !legacyErrors.length);
633
763
  }
634
764
  if (settings.expressions) {
635
765
  warnAboutUnquotedText();
@@ -640,6 +770,23 @@ export async function analyse(settings) {
640
770
  report("warning", "squiffy/built-in-attribute", file.filename, found.range, `Squiffy sets ${found.name} itself: it's ${BUILT_IN_ATTRIBUTES.get(found.name)}. Use a different name for your own attribute.`, found.range.start.line + 1);
641
771
  }
642
772
  });
773
+ // @leave only does something when the screen is cleared: by the section or passage it's in, or by
774
+ // one that it's for, as the master section's @leave is for every section
775
+ const master = story.sections[""];
776
+ const clears = (s) => s.clear || Object.values(s.passages).some(p => p.clear);
777
+ const anyClear = Object.values(story.sections).some(clears);
778
+ const checkLeave = (target, used) => {
779
+ if (!target.leaveNode || target.leave == "none" || used)
780
+ return;
781
+ report("warning", "squiffy/transition", target.leaveNode.filename, target.leaveNode.range, "@leave is for when the screen is cleared, but nothing it's for has @clear, so it does nothing.", target.leaveNode.range.start.line + 1);
782
+ };
783
+ for (const section of Object.values(story.sections)) {
784
+ const masterPassageClears = !!(section.passages[""]?.clear || master?.passages[""]?.clear);
785
+ checkLeave(section, section === master ? anyClear : clears(section) || !!master?.clear || (master ? Object.values(master.passages).some(p => p.clear) : false));
786
+ for (const passage of Object.values(section.passages)) {
787
+ checkLeave(passage, passage.clear || masterPassageClears || (passage.name == "" && clears(section)));
788
+ }
789
+ }
643
790
  const analysis = {
644
791
  success,
645
792
  files,
@@ -1,7 +1,7 @@
1
1
  import { ExternalFiles, ParseCache } from "./analyse.js";
2
2
  import { CompileSuccess } from "./emit.js";
3
3
  import type { Diagnostic } from "./syntax.js";
4
- import type { Panel } from "./project.js";
4
+ import type { Panel, PlayerSetting } from "./project.js";
5
5
  export { findV5Syntax, convertV5Syntax } from "./v5.js";
6
6
  export type { V5SyntaxMatch, V5ConversionProblem, V5ConversionResult } from "./v5.js";
7
7
  export { convertSetters, convertSetter } from "./setters.js";
@@ -19,8 +19,9 @@ export type { AnalyseSettings, Analysis, ExternalFiles, SectionSymbol, PassageSy
19
19
  export { emit } from "./emit.js";
20
20
  export type { EmitSettings, Output, UiInfo, CompileSuccess } from "./emit.js";
21
21
  export { formatDiagnostic } from "./syntax.js";
22
- export { PROJECT_FILENAME, PROJECT_FILE_VERSION, PROJECT_SCHEMA_URL, parseProjectFile, getStoryFiles, isExcluded, isAlwaysSkipped, formatProjectFile, updateProjectFile, PAGE_STRINGS, PANEL_ICONS, storyFileList, BUILD_FOLDER, } from "./project.js";
23
- export type { MapPositions, PageStringName, PageStrings, Panel, PanelIcon, ProjectFile, ParsedProjectFile, StoryFiles } from "./project.js";
22
+ export { findCssUrls, isRelativeUrl, resolveCssUrl, rewriteCssUrls } from "./css-urls.js";
23
+ export { PROJECT_FILENAME, PROJECT_FILE_VERSION, PROJECT_SCHEMA_URL, parseProjectFile, getStoryFiles, isExcluded, isAlwaysSkipped, formatProjectFile, updateProjectFile, PAGE_STRINGS, PANEL_ICONS, PLAYER_SETTING_TYPES, storyFileList, BUILD_FOLDER, } from "./project.js";
24
+ export type { MapPositions, PageStringName, PageStrings, Panel, PanelIcon, PlayerSetting, PlayerSettingOption, ProjectFile, ParsedProjectFile, StoryFiles } from "./project.js";
24
25
  export type * from "./syntax.js";
25
26
  interface CompilerSettings {
26
27
  scriptBaseFilename?: string;
@@ -33,6 +34,7 @@ interface CompilerSettings {
33
34
  expressions?: boolean;
34
35
  projectId?: string;
35
36
  panels?: Panel[];
37
+ settings?: PlayerSetting[];
36
38
  }
37
39
  export interface CompileError {
38
40
  success: false;
package/dist/compiler.js CHANGED
@@ -14,7 +14,8 @@ export { parse } from "./parse.js";
14
14
  export { analyse, ParseCache } from "./analyse.js";
15
15
  export { emit } from "./emit.js";
16
16
  export { formatDiagnostic } from "./syntax.js";
17
- export { PROJECT_FILENAME, PROJECT_FILE_VERSION, PROJECT_SCHEMA_URL, parseProjectFile, getStoryFiles, isExcluded, isAlwaysSkipped, formatProjectFile, updateProjectFile, PAGE_STRINGS, PANEL_ICONS, storyFileList, BUILD_FOLDER, } from "./project.js";
17
+ export { findCssUrls, isRelativeUrl, resolveCssUrl, rewriteCssUrls } from "./css-urls.js";
18
+ export { PROJECT_FILENAME, PROJECT_FILE_VERSION, PROJECT_SCHEMA_URL, parseProjectFile, getStoryFiles, isExcluded, isAlwaysSkipped, formatProjectFile, updateProjectFile, PAGE_STRINGS, PANEL_ICONS, PLAYER_SETTING_TYPES, storyFileList, BUILD_FOLDER, } from "./project.js";
18
19
  export async function compile(settings) {
19
20
  const analysis = await analyse(settings);
20
21
  const { legacyErrors, legacyWarnings } = analysisInternals.get(analysis);
@@ -30,5 +31,5 @@ export async function compile(settings) {
30
31
  diagnostics: analysis.diagnostics,
31
32
  };
32
33
  }
33
- return emit(analysis, { globalJs: settings.globalJs, projectId: settings.projectId, panels: settings.panels });
34
+ return emit(analysis, { globalJs: settings.globalJs, projectId: settings.projectId, panels: settings.panels, settings: settings.settings });
34
35
  }
@@ -0,0 +1,4 @@
1
+ export declare function isRelativeUrl(url: string): boolean;
2
+ export declare function rewriteCssUrls(css: string, replace: (url: string) => string | undefined): string;
3
+ export declare function findCssUrls(css: string): string[];
4
+ export declare function resolveCssUrl(sheetPath: string, url: string): string | null;
@@ -0,0 +1,59 @@
1
+ // The url()s in a story's own stylesheets, such as a font's files or a background image. A
2
+ // stylesheet refers to them by their path from its own folder, as a web page's CSS does. That
3
+ // works as it is in a published story, whose stylesheets are separate files, but a one-file
4
+ // export or the editor's preview has to swap each for something it can load.
5
+ const URL_PATTERN = /url\(\s*(?:"((?:[^"\\\n]|\\.)*)"|'((?:[^'\\\n]|\\.)*)'|([^)"'\s]*))\s*\)/gi;
6
+ // A url() that's a path to a file beside the stylesheet, rather than a web address, a data: URI,
7
+ // an absolute path or a reference to an SVG element like "#gradient"
8
+ export function isRelativeUrl(url) {
9
+ return url != "" && !/^(?:[a-z][a-z\d+.-]*:|\/|#)/i.test(url);
10
+ }
11
+ // Replaces each relative url() in css with what replace returns for it, or leaves it as it is if
12
+ // replace returns undefined
13
+ export function rewriteCssUrls(css, replace) {
14
+ return css.replace(URL_PATTERN, (match, double, single, bare) => {
15
+ const url = (double ?? single ?? bare ?? "").trim();
16
+ if (!isRelativeUrl(url))
17
+ return match;
18
+ const replacement = replace(url);
19
+ return replacement === undefined ? match : `url("${replacement.replace(/["\\\n]/g, c => c == "\n" ? "\\a " : `\\${c}`)}")`;
20
+ });
21
+ }
22
+ // The relative url()s in css
23
+ export function findCssUrls(css) {
24
+ const urls = [];
25
+ rewriteCssUrls(css, url => {
26
+ urls.push(url);
27
+ return undefined;
28
+ });
29
+ return urls;
30
+ }
31
+ // The path, from the story's folder, of a url() in the stylesheet at sheetPath (also from the
32
+ // story's folder), e.g. "fonts/literata/latin-400-normal.woff2" for "latin-400-normal.woff2" in
33
+ // "fonts/literata/literata.css". Null if it's outside the story's folder. A query or #fragment,
34
+ // such as the "?#iefix" some older @font-face rules have, isn't part of the path.
35
+ export function resolveCssUrl(sheetPath, url) {
36
+ let path = url.replace(/[?#].*$/, "");
37
+ try {
38
+ path = decodeURI(path);
39
+ }
40
+ catch {
41
+ // Not valid percent-encoding: use it as it is
42
+ }
43
+ const sheet = sheetPath.replace(/\\/g, "/").split("/");
44
+ sheet.pop();
45
+ const parts = sheet.filter(part => part != "" && part != ".");
46
+ for (const part of path.replace(/\\/g, "/").split("/")) {
47
+ if (part == "" || part == ".")
48
+ continue;
49
+ if (part == "..") {
50
+ if (!parts.length || parts[parts.length - 1] == "..")
51
+ return null;
52
+ parts.pop();
53
+ }
54
+ else {
55
+ parts.push(part);
56
+ }
57
+ }
58
+ return parts.length ? parts.join("/") : null;
59
+ }
package/dist/emit.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Analysis } from "./analyse.js";
2
2
  import type { Diagnostic } from "./syntax.js";
3
- import type { Panel } from "./project.js";
3
+ import type { Panel, PlayerSetting } from "./project.js";
4
4
  export interface Output {
5
5
  story: OutputStory;
6
6
  js: string[][];
@@ -12,11 +12,20 @@ interface OutputStory {
12
12
  uiJsIndex?: number;
13
13
  expressions?: boolean;
14
14
  panels?: Panel[];
15
+ settings?: PlayerSetting[];
15
16
  sections: Record<string, OutputSection>;
17
+ audio?: string[];
16
18
  }
17
19
  interface OutputSection {
18
20
  text?: string;
19
21
  clear?: boolean;
22
+ music?: string | null;
23
+ sounds?: string[];
24
+ classes?: string[];
25
+ transition?: string;
26
+ transitionTime?: number;
27
+ leave?: string;
28
+ leaveTime?: number;
20
29
  attributes?: string[];
21
30
  jsIndex?: number;
22
31
  passages?: Record<string, OutputPassage>;
@@ -25,6 +34,13 @@ interface OutputSection {
25
34
  interface OutputPassage {
26
35
  text?: string;
27
36
  clear?: boolean;
37
+ music?: string | null;
38
+ sounds?: string[];
39
+ classes?: string[];
40
+ transition?: string;
41
+ transitionTime?: number;
42
+ leave?: string;
43
+ leaveTime?: number;
28
44
  repeat?: boolean;
29
45
  attributes?: string[];
30
46
  jsIndex?: number;
@@ -38,6 +54,7 @@ export interface EmitSettings {
38
54
  globalJs?: boolean;
39
55
  projectId?: string;
40
56
  panels?: Panel[];
57
+ settings?: PlayerSetting[];
41
58
  }
42
59
  export interface CompileSuccess {
43
60
  success: true;
package/dist/emit.js CHANGED
@@ -1,6 +1,31 @@
1
1
  import pkg from "../package.json" with { type: "json" };
2
2
  import { analysisInternals } from "./analyse.js";
3
3
  const version = pkg.version;
4
+ // {{music "file"}} or {{sound "file"}}
5
+ const SOUND_HELPER = /\{\{\s*(?:music|sound)\s+(["'])([^"'{}]+)\1/g;
6
+ // @music and @sound (#444), and the files they and the text name, for OutputStory.audio
7
+ function addSounds(output, from, audio) {
8
+ if (from.music !== undefined)
9
+ output.music = from.music;
10
+ if (from.sounds.length)
11
+ output.sounds = from.sounds;
12
+ if (from.music)
13
+ audio.add(from.music);
14
+ from.sounds.forEach(file => { if (file != "stop")
15
+ audio.add(file); });
16
+ for (const match of from.output.matchAll(SOUND_HELPER))
17
+ audio.add(match[2]);
18
+ }
19
+ function addTransition(output, from) {
20
+ if (from.transition !== undefined)
21
+ output.transition = from.transition;
22
+ if (from.transitionTime !== undefined)
23
+ output.transitionTime = from.transitionTime;
24
+ if (from.leave !== undefined)
25
+ output.leave = from.leave;
26
+ if (from.leaveTime !== undefined)
27
+ output.leaveTime = from.leaveTime;
28
+ }
4
29
  // Produces the story data and JavaScript for an analysis that succeeded.
5
30
  export function emit(analysis, settings = {}) {
6
31
  const internals = analysisInternals.get(analysis);
@@ -28,10 +53,14 @@ export function emit(analysis, settings = {}) {
28
53
  if (settings.panels?.length) {
29
54
  output.story.panels = settings.panels;
30
55
  }
56
+ if (settings.settings?.length) {
57
+ output.story.settings = settings.settings;
58
+ }
31
59
  if (story.uiJs.length > 0) {
32
60
  output.js.push(story.uiJs);
33
61
  output.story.uiJsIndex = output.js.length - 1;
34
62
  }
63
+ const audio = new Set();
35
64
  for (const sectionName of Object.keys(story.sections)) {
36
65
  const section = story.sections[sectionName];
37
66
  const outputSection = {};
@@ -40,6 +69,10 @@ export function emit(analysis, settings = {}) {
40
69
  outputSection.clear = true;
41
70
  }
42
71
  outputSection.text = section.output;
72
+ addSounds(outputSection, section, audio);
73
+ if (section.classes.length)
74
+ outputSection.classes = section.classes;
75
+ addTransition(outputSection, section);
43
76
  if (section.attributes.length > 0) {
44
77
  outputSection.attributes = section.attributes;
45
78
  }
@@ -71,6 +104,10 @@ export function emit(analysis, settings = {}) {
71
104
  outputPassage.repeat = true;
72
105
  }
73
106
  outputPassage.text = passage.output;
107
+ addSounds(outputPassage, passage, audio);
108
+ if (passage.classes.length)
109
+ outputPassage.classes = passage.classes;
110
+ addTransition(outputPassage, passage);
74
111
  if (passage.attributes.length > 0) {
75
112
  outputPassage.attributes = passage.attributes;
76
113
  }
@@ -80,6 +117,9 @@ export function emit(analysis, settings = {}) {
80
117
  }
81
118
  }
82
119
  }
120
+ if (audio.size) {
121
+ output.story.audio = [...audio];
122
+ }
83
123
  const getJs = async (excludeHeader) => {
84
124
  const outputJs = [];
85
125
  if (!excludeHeader) {
@@ -105,6 +145,12 @@ export function emit(analysis, settings = {}) {
105
145
  if (output.story.panels) {
106
146
  outputJs.push(`story.panels = ${JSON.stringify(output.story.panels, null, 4)};`);
107
147
  }
148
+ if (output.story.settings) {
149
+ outputJs.push(`story.settings = ${JSON.stringify(output.story.settings, null, 4)};`);
150
+ }
151
+ if (output.story.audio) {
152
+ outputJs.push(`story.audio = ${JSON.stringify(output.story.audio, null, 4)};`);
153
+ }
108
154
  outputJs.push(`story.start = ${JSON.stringify(output.story.start, null, 4)};`);
109
155
  outputJs.push(`story.sections = ${JSON.stringify(output.story.sections, null, 4)};`);
110
156
  outputJs.push("story.js = [");
@@ -0,0 +1,19 @@
1
+ export declare function rewriteInfixCondition(condition: string): string | null;
2
+ export declare function rewriteInfixArguments(args: string): string | null;
3
+ export declare function rewriteInfixEmbed(args: string, label?: string): string | null;
4
+ export declare function rewriteDisplayExpression(expression: string): string;
5
+ export declare function rewriteValueExpression(expression: string): string;
6
+ export declare function splitSetters(setters: string): string[];
7
+ export declare function findTagEnd(text: string, from: number): number;
8
+ export declare class InfixRewriteError extends Error {
9
+ readonly start: number;
10
+ readonly end: number;
11
+ constructor(message: string, start: number, end: number);
12
+ }
13
+ export interface InfixEdit {
14
+ start: number;
15
+ end: number;
16
+ length: number;
17
+ }
18
+ export declare function rewriteInfixExpressions(text: string, edits?: InfixEdit[]): string;
19
+ export declare function mapRewrittenPosition(edits: InfixEdit[], pos: number): number;