squiffy-compiler 7.0.0-beta.4 → 7.0.0-beta.5

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.js CHANGED
@@ -2,7 +2,7 @@ import { parse } from "./parse.js";
2
2
  import { positionMapper, transformText } from "./text.js";
3
3
  import { PROJECT_FILENAME } from "./project.js";
4
4
  import { findV5Syntax } from "./v5.js";
5
- import { attributesSetIn, findAtAttributes, findUnquotedText } from "./setters.js";
5
+ import { attributesSetIn, BUILT_IN_ATTRIBUTES, findAtAttributes, findBuiltInAttributeSets, findUnquotedText } from "./setters.js";
6
6
  // Caches the syntax tree of each file by filename, reparsing a file only when its text changes.
7
7
  export class ParseCache {
8
8
  constructor() {
@@ -545,6 +545,13 @@ export async function analyse(settings) {
545
545
  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`);
546
546
  }
547
547
  }
548
+ // {{goto}} only goes to sections
549
+ for (const goto of result.gotos) {
550
+ if (!goto.name || !(goto.name in story.sections)) {
551
+ const passage = goto.name in section.passages ? ` ({{goto}} can't go to a passage: use {{embed "${goto.name}"}} to show it here)` : "";
552
+ warn("squiffy/unknown-goto", goto.start, goto.end, `${where} there is a {{goto "${goto.name}"}}, but no section with that name exists${passage}`);
553
+ }
554
+ }
548
555
  }
549
556
  // With recover, processFile carries on after errors, so check for them too
550
557
  let success = await processFile(settings.script, settings.scriptBaseFilename, true);
@@ -581,6 +588,12 @@ export async function analyse(settings) {
581
588
  if (settings.expressions) {
582
589
  warnAboutUnquotedText();
583
590
  }
591
+ // A story setting an attribute that Squiffy sets itself, such as turns (#377)
592
+ files.forEach((file, i) => {
593
+ for (const found of findBuiltInAttributeSets(file, texts[i])) {
594
+ 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);
595
+ }
596
+ });
584
597
  const analysis = {
585
598
  success,
586
599
  files,
@@ -4,6 +4,10 @@ import type { Diagnostic } from "./syntax.js";
4
4
  export { findV5Syntax, convertV5Syntax } from "./v5.js";
5
5
  export type { V5SyntaxMatch, V5ConversionProblem, V5ConversionResult } from "./v5.js";
6
6
  export { convertSetters, convertSetter } from "./setters.js";
7
+ export { convertTwine, convertTwineStory, formatTwineProblems } from "./twine/convert.js";
8
+ export type { TwineConversionResult, TwineConversionProblem, TwineConversionOptions } from "./twine/convert.js";
9
+ export { parseTwine, isTwineSource, TwineParseError } from "./twine/story.js";
10
+ export type { TwineStory, TwinePassage } from "./twine/story.js";
7
11
  export type { SetterConversion, SetterConversionProblem, SetterConversionResult } from "./setters.js";
8
12
  export { parse } from "./parse.js";
9
13
  export { analyse, ParseCache } from "./analyse.js";
package/dist/compiler.js CHANGED
@@ -2,6 +2,8 @@ import { analyse, analysisInternals } from "./analyse.js";
2
2
  import { emit } from "./emit.js";
3
3
  export { findV5Syntax, convertV5Syntax } from "./v5.js";
4
4
  export { convertSetters, convertSetter } from "./setters.js";
5
+ export { convertTwine, convertTwineStory, formatTwineProblems } from "./twine/convert.js";
6
+ export { parseTwine, isTwineSource, TwineParseError } from "./twine/story.js";
5
7
  // The compiler works in three stages, which can also be used separately:
6
8
  // - parse() turns one file into a syntax tree, with a source range on every node
7
9
  // - analyse() resolves @imports, builds the story's sections and passages, and reports diagnostics
@@ -11,6 +11,7 @@ import { convertV5Syntax, findV5Syntax } from "./v5.js";
11
11
  import { findUnquotedText } from "./setters.js";
12
12
  import { buildStoryGraph, storyGraphDiagnostics } from "./story-graph.js";
13
13
  const sameSymbol = (a, b) => a.kind == b.kind && a.name == b.name && (a.kind == "section" || a.section == b.section);
14
+ const usageRange = (usage) => (usage.link ?? usage.embed ?? usage.goto).range;
14
15
  // Directives, for completion, with what they do
15
16
  const DIRECTIVES = [
16
17
  ["title", "Sets the story's title"],
@@ -234,6 +235,9 @@ export function createLanguageService(options) {
234
235
  for (const embed of container.embeds) {
235
236
  index.usages.push({ file: path, container, sectionNode: node, embed });
236
237
  }
238
+ for (const goto of container.gotos) {
239
+ index.usages.push({ file: path, container, sectionNode: node, goto });
240
+ }
237
241
  for (const statement of container.body) {
238
242
  if (statement.type == "attribute") {
239
243
  const name = attributeName(statement.value);
@@ -304,6 +308,10 @@ export function createLanguageService(options) {
304
308
  return { kind: "section", name };
305
309
  return undefined;
306
310
  }
311
+ if (usage.goto) {
312
+ const name = usage.goto.name;
313
+ return name && index.sections.has(name) ? { kind: "section", name } : undefined;
314
+ }
307
315
  const link = usage.link;
308
316
  if (link.linkType == "section") {
309
317
  if (link.form == "next") {
@@ -336,12 +344,12 @@ export function createLanguageService(options) {
336
344
  return undefined;
337
345
  return { file: section.file, range: passage.nameRange };
338
346
  }
339
- // What's at a position: a link, an embed, or the name in a section or passage header.
347
+ // What's at a position: a link, an embed, a goto, or the name in a section or passage header.
340
348
  async function symbolAt(index, path, position) {
341
349
  for (const usage of index.usages) {
342
350
  if (usage.file != path)
343
351
  continue;
344
- const range = usage.link?.range ?? usage.embed.range;
352
+ const range = usageRange(usage);
345
353
  if (!contains(range, position))
346
354
  continue;
347
355
  const symbol = resolveUsage(index, usage);
@@ -375,6 +383,8 @@ export function createLanguageService(options) {
375
383
  function nameRangeInLink(index, usage, name) {
376
384
  if (usage.embed)
377
385
  return usage.embed.nameRange;
386
+ if (usage.goto)
387
+ return usage.goto.nameRange;
378
388
  const link = usage.link;
379
389
  if (link.form == "next")
380
390
  return undefined;
@@ -398,7 +408,7 @@ export function createLanguageService(options) {
398
408
  for (const usage of index.usages) {
399
409
  const target = resolveUsage(index, usage);
400
410
  if (target && sameSymbol(target, symbol)) {
401
- result.push({ file: usage.file, range: usage.link?.range ?? usage.embed.range });
411
+ result.push({ file: usage.file, range: usageRange(usage) });
402
412
  }
403
413
  }
404
414
  return result;
@@ -572,6 +582,13 @@ export function createLanguageService(options) {
572
582
  if ((match = /\{\{~?\s*embed\s+["']([^"']*)$/.exec(prefix))) {
573
583
  return [...passageItems(match[1]), ...sectionItems(match[1])];
574
584
  }
585
+ // {{seen "name"}}, (visits "name") etc., which take a section or passage (#377)
586
+ if ((match = /(?:\{\{~?|\()\s*(?:seen|visits)\s+["']([^"']*)$/.exec(prefix))) {
587
+ return [...passageItems(match[1]), ...sectionItems(match[1])];
588
+ }
589
+ if ((match = /\{\{~?\s*goto\s+["']([^"']*)$/.exec(prefix))) {
590
+ return sectionItems(match[1]);
591
+ }
575
592
  if ((match = /\[\[[^\]]*\]\]\(([^),]*)$/.exec(prefix))) {
576
593
  return sectionItems(match[1]);
577
594
  }
package/dist/node.d.ts CHANGED
@@ -4,6 +4,7 @@ export interface StoryOnDisk {
4
4
  projectFile?: string;
5
5
  note?: string;
6
6
  expressions: boolean;
7
+ backButton?: boolean;
7
8
  }
8
9
  export declare class StoryNotFoundError extends Error {
9
10
  }
package/dist/node.js CHANGED
@@ -70,7 +70,10 @@ async function folderStory(projectFile, options) {
70
70
  throw new StoryNotFoundError(`The main file in ${projectFile}, ${project.main}, doesn't exist.`);
71
71
  }
72
72
  const story = getStoryFiles(project, await listSquiffyFiles(root), options.skip);
73
- return { mainFile, storyFiles: story.included.map(file => path.resolve(root, file)), projectFile, expressions: true };
73
+ return {
74
+ mainFile, storyFiles: story.included.map(file => path.resolve(root, file)), projectFile, expressions: true,
75
+ ...(project.backButton !== undefined ? { backButton: project.backButton } : {}),
76
+ };
74
77
  }
75
78
  // Finds the story to build from what the author gave: a folder with a squiffy.json in it, or a
76
79
  // .squiffy file.
package/dist/parse.js CHANGED
@@ -123,6 +123,7 @@ export function parse(text, filename) {
123
123
  body: [],
124
124
  links: [],
125
125
  embeds: [],
126
+ gotos: [],
126
127
  };
127
128
  section.passages.push(passage);
128
129
  textStarted = false;
@@ -232,9 +233,9 @@ export function parse(text, filename) {
232
233
  return file;
233
234
  }
234
235
  function newSection(kind, range) {
235
- return { type: "section", kind, range, body: [], passages: [], links: [], embeds: [] };
236
+ return { type: "section", kind, range, body: [], passages: [], links: [], embeds: [], gotos: [] };
236
237
  }
237
- // Fills in the links and embeds in a section's or passage's text.
238
+ // Fills in the links, embeds and gotos in a section's or passage's text.
238
239
  function findLinks(node, lines) {
239
240
  const textNodes = node.body.filter((n) => n.type == "text");
240
241
  if (!textNodes.length)
@@ -262,4 +263,10 @@ function findLinks(node, lines) {
262
263
  range: toRange(embed.start, embed.end),
263
264
  nameRange: toRange(embed.nameStart, embed.nameEnd),
264
265
  }));
266
+ node.gotos = result.gotos.map((goto) => ({
267
+ type: "goto",
268
+ name: goto.name,
269
+ range: toRange(goto.start, goto.end),
270
+ nameRange: toRange(goto.nameStart, goto.nameEnd),
271
+ }));
265
272
  }
package/dist/project.d.ts CHANGED
@@ -6,6 +6,7 @@ export interface ProjectFile {
6
6
  main: string;
7
7
  exclude: string[];
8
8
  map?: MapPositions;
9
+ backButton?: boolean;
9
10
  }
10
11
  export interface MapPositions {
11
12
  sections: Record<string, [number, number]>;
@@ -32,6 +33,7 @@ export declare function getStoryFiles(project: ProjectFile, paths: Iterable<stri
32
33
  export declare function formatProjectFile(project: {
33
34
  main: string;
34
35
  exclude?: string[];
36
+ map?: MapPositions;
35
37
  }): string;
36
38
  export declare function updateProjectFile(text: string, changes: {
37
39
  main?: string;
package/dist/project.js CHANGED
@@ -54,8 +54,19 @@ export function parseProjectFile(text) {
54
54
  if (!Array.isArray(exclude) || exclude.some(pattern => typeof pattern != "string")) {
55
55
  return fail(`${PROJECT_FILENAME}'s "exclude" should be a list of file patterns, such as ["drafts/**"].`);
56
56
  }
57
+ const backButton = data.backButton;
58
+ if (backButton !== undefined && typeof backButton != "boolean") {
59
+ return fail(`${PROJECT_FILENAME}'s "backButton" should be true or false.`);
60
+ }
57
61
  const map = parseMapPositions(data.map);
58
- return { success: true, project: { version, main: mainPath, exclude: exclude, ...(map ? { map } : {}) } };
62
+ return {
63
+ success: true,
64
+ project: {
65
+ version, main: mainPath, exclude: exclude,
66
+ ...(map ? { map } : {}),
67
+ ...(backButton !== undefined ? { backButton } : {}),
68
+ },
69
+ };
59
70
  }
60
71
  const isPosition = (value) => Array.isArray(value) && value.length == 2 && value.every(n => typeof n == "number" && Number.isFinite(n));
61
72
  const isObject = (value) => !!value && typeof value == "object" && !Array.isArray(value);
@@ -177,7 +188,8 @@ export function formatProjectFile(project) {
177
188
  };
178
189
  if (project.exclude?.length)
179
190
  json.exclude = project.exclude;
180
- return JSON.stringify(json, null, 2) + "\n";
191
+ const text = JSON.stringify(json, null, 2) + "\n";
192
+ return project.map ? updateProjectFile(text, { map: project.map }) : text;
181
193
  }
182
194
  // Changes a project file's text, keeping any other properties it has. Returns undefined if the
183
195
  // text isn't a JSON object. A `map` with no positions removes it.
package/dist/setters.d.ts CHANGED
@@ -32,3 +32,9 @@ export interface UnquotedText {
32
32
  quoted: string | null;
33
33
  }
34
34
  export declare function findUnquotedText(file: SquiffyFile, isSet: (name: string) => boolean): UnquotedText[];
35
+ export declare const BUILT_IN_ATTRIBUTES: ReadonlyMap<string, string>;
36
+ export interface BuiltInAttributeSet {
37
+ name: string;
38
+ range: Range;
39
+ }
40
+ export declare function findBuiltInAttributeSets(file: SquiffyFile, text: string): BuiltInAttributeSet[];
package/dist/setters.js CHANGED
@@ -369,3 +369,43 @@ export function findUnquotedText(file, isSet) {
369
369
  }
370
370
  return result;
371
371
  }
372
+ // Attributes that Squiffy sets itself, which a story shouldn't set as well, with what they are
373
+ export const BUILT_IN_ATTRIBUTES = new Map([
374
+ // #377: stories written before Squiffy counted turns often kept their own count called "turns"
375
+ ["turns", "the number of links clicked in the story so far"],
376
+ ]);
377
+ // Finds where a file sets one of the BUILT_IN_ATTRIBUTES: on the left of a setter, in @inc, @dec
378
+ // and @unset, and as the quoted first argument of set, inc, dec or unset, which covers
379
+ // {{set "turns" 1}} and squiffy.set("turns", 1) in JavaScript.
380
+ export function findBuiltInAttributeSets(file, text) {
381
+ const result = [];
382
+ const add = (name, line, character) => {
383
+ if (BUILT_IN_ATTRIBUTES.has(name)) {
384
+ result.push({ name, range: { start: { line, character }, end: { line, character: character + name.length } } });
385
+ }
386
+ };
387
+ for (const site of setterSites(file)) {
388
+ const match = /^(\s*(?:not\s+)?)(\w+)/.exec(site.text);
389
+ if (match)
390
+ add(match[2], site.line, site.character + match[1].length);
391
+ }
392
+ for (const section of [file.preamble, ...file.sections]) {
393
+ if (section.type !== "section")
394
+ continue;
395
+ for (const container of [section, ...section.passages]) {
396
+ for (const node of container.body) {
397
+ if (node.type !== "attribute" || node.directive === "set")
398
+ continue;
399
+ const match = /^(\s*)(\w+)/.exec(node.value);
400
+ if (match)
401
+ add(match[2], node.valueRange.start.line, node.valueRange.start.character + match[1].length);
402
+ }
403
+ }
404
+ }
405
+ text.replace(/\r/g, "").split("\n").forEach((line, i) => {
406
+ for (const match of line.matchAll(/\b(?:set|inc|dec|unset)\s*\(?\s*(["'])(\w+)\1/g)) {
407
+ add(match[2], i, match.index + match[0].length - match[2].length - 1);
408
+ }
409
+ });
410
+ return result.sort((a, b) => a.range.start.line - b.range.start.line || a.range.start.character - b.range.start.character);
411
+ }
@@ -1,5 +1,5 @@
1
1
  import type { Location } from "./language-service.js";
2
- import type { Diagnostic, EmbedNode, LinkNode, PassageNode, Position, SectionNode, SquiffyFile } from "./syntax.js";
2
+ import type { Diagnostic, EmbedNode, GotoNode, LinkNode, PassageNode, Position, SectionNode, SquiffyFile } from "./syntax.js";
3
3
  export interface StoryGraph {
4
4
  nodes: GraphNode[];
5
5
  edges: GraphEdge[];
@@ -39,6 +39,7 @@ export interface GraphUsage {
39
39
  sectionNode: SectionNode;
40
40
  link?: LinkNode;
41
41
  embed?: EmbedNode;
42
+ goto?: GotoNode;
42
43
  }
43
44
  export type GraphTarget = {
44
45
  kind: "section";
@@ -105,6 +105,9 @@ export function buildStoryGraph(source) {
105
105
  else if (statement.type == "text") {
106
106
  if (/\{\{~?\s*section\s/.test(statement.text))
107
107
  unknown.add(from);
108
+ // {{goto}} with an expression, e.g. {{goto (random (array "a" "b"))}}
109
+ if (/\{\{~?\s*goto\s+[^\s"'}]/.test(statement.text))
110
+ unknown.add(from);
108
111
  // {{live "x" section="name"}} and passage="name" show that section's or
109
112
  // passage's text, and so its links, as {{embed}} does
110
113
  for (const match of statement.text.matchAll(/\{\{~?\s*live\s[^}]*?\b(section|passage)\s*=\s*(["'])([^"']*)\2[^}]*\}\}/g)) {
@@ -176,13 +179,17 @@ export function buildStoryGraph(source) {
176
179
  continue;
177
180
  const target = source.resolve(usage);
178
181
  const to = target && (target.kind == "section" ? sectionId(target.name) : passageId(target.section, target.name));
179
- const range = usage.link?.range ?? usage.embed.range;
182
+ const range = (usage.link ?? usage.embed ?? usage.goto).range;
180
183
  const conditional = isConditional(usage.container, range.start);
181
184
  const location = { file: usage.file, range };
182
185
  if (usage.embed) {
183
186
  edges.push({ from, ...(to ? { to } : { target: usage.embed.name }), kind: "embed", conditional, location });
184
187
  continue;
185
188
  }
189
+ if (usage.goto) {
190
+ edges.push({ from, ...(to ? { to } : { target: usage.goto.name }), kind: "goto", conditional, location });
191
+ continue;
192
+ }
186
193
  const link = usage.link;
187
194
  const dynamic = !to && link.target.includes("{{");
188
195
  if (dynamic)
@@ -342,7 +349,7 @@ export function storyGraphDiagnostics(graph) {
342
349
  range: node.location.range,
343
350
  severity: "warning",
344
351
  code: "squiffy/unreachable-section",
345
- message: "This section can't be reached from the start of the story, as nothing the player can reach links to it.",
352
+ message: `The section "${node.name}" can't be reached from the start of the story, as nothing the player can reach links to it.`,
346
353
  });
347
354
  }
348
355
  else if (node.deadEnd) {
@@ -351,7 +358,7 @@ export function storyGraphDiagnostics(graph) {
351
358
  range: node.location.range,
352
359
  severity: "warning",
353
360
  code: "squiffy/dead-end",
354
- message: "This section has no way out, as it has no links to another section. If it's one of the story's endings, add @end to say so.",
361
+ message: `The section "${node.name}" has no way out, as it has no links to another section. If it's one of the story's endings, add @end to say so.`,
355
362
  });
356
363
  }
357
364
  }
package/dist/syntax.d.ts CHANGED
@@ -7,7 +7,7 @@ export interface Range {
7
7
  end: Position;
8
8
  }
9
9
  export type DiagnosticSeverity = "error" | "warning" | "hint";
10
- export type DiagnosticCode = "squiffy/passage-without-section" | "squiffy/text-in-ui-block" | "squiffy/content-before-section" | "squiffy/v5-syntax" | "squiffy/at-attribute" | "squiffy/unquoted-text" | "squiffy/external-files-unavailable" | "squiffy/no-next-section" | "squiffy/unknown-embed" | "squiffy/unknown-section-link" | "squiffy/unknown-passage-link" | "squiffy/duplicate-section" | "squiffy/duplicate-passage" | "squiffy/not-in-main-file" | "squiffy/redundant-import" | "squiffy/project-file" | "squiffy/unreachable-section" | "squiffy/dead-end";
10
+ export type DiagnosticCode = "squiffy/passage-without-section" | "squiffy/text-in-ui-block" | "squiffy/content-before-section" | "squiffy/v5-syntax" | "squiffy/at-attribute" | "squiffy/unquoted-text" | "squiffy/built-in-attribute" | "squiffy/external-files-unavailable" | "squiffy/no-next-section" | "squiffy/unknown-embed" | "squiffy/unknown-goto" | "squiffy/unknown-section-link" | "squiffy/unknown-passage-link" | "squiffy/duplicate-section" | "squiffy/duplicate-passage" | "squiffy/not-in-main-file" | "squiffy/redundant-import" | "squiffy/project-file" | "squiffy/unreachable-section" | "squiffy/dead-end";
11
11
  export interface Diagnostic {
12
12
  file: string;
13
13
  range: Range;
@@ -34,6 +34,7 @@ export interface SectionNode {
34
34
  passages: PassageNode[];
35
35
  links: LinkNode[];
36
36
  embeds: EmbedNode[];
37
+ gotos: GotoNode[];
37
38
  }
38
39
  export interface PassageNode {
39
40
  type: "passage";
@@ -44,6 +45,7 @@ export interface PassageNode {
44
45
  body: StatementNode[];
45
46
  links: LinkNode[];
46
47
  embeds: EmbedNode[];
48
+ gotos: GotoNode[];
47
49
  }
48
50
  export interface SnippetNode {
49
51
  type: "snippet";
@@ -148,3 +150,9 @@ export interface EmbedNode {
148
150
  range: Range;
149
151
  nameRange: Range;
150
152
  }
153
+ export interface GotoNode {
154
+ type: "goto";
155
+ name: string;
156
+ range: Range;
157
+ nameRange: Range;
158
+ }
package/dist/text.d.ts CHANGED
@@ -15,6 +15,7 @@ export interface TextEmbed {
15
15
  nameStart: number;
16
16
  nameEnd: number;
17
17
  }
18
+ export type TextGoto = TextEmbed;
18
19
  export interface NoNextSection {
19
20
  display: string;
20
21
  start: number;
@@ -24,6 +25,7 @@ export interface TransformResult {
24
25
  output: string;
25
26
  links: TextLink[];
26
27
  embeds: TextEmbed[];
28
+ gotos: TextGoto[];
27
29
  noNextSection: NoNextSection[];
28
30
  }
29
31
  export declare const balancedTarget: string;
package/dist/text.js CHANGED
@@ -263,23 +263,28 @@ export function transformText(text, getNextSectionName, expressions = false) {
263
263
  return `{{passage "${escapeForHandlebarsString(name)}"}}${suffix}`;
264
264
  });
265
265
  input.replace(/(\d+)/g, ([, i]) => mustaches[Number(i)]);
266
- // embedRegex matches {{embed "name"}} or {{embed 'name'}} with a literal name.
267
- const embedRegex = /\{\{~?\s*embed\s+(["'])(.*?)\1/g;
268
- let match;
269
- while ((match = embedRegex.exec(input.value))) {
270
- const start = match.index;
271
- const end = start + match[0].length;
272
- const nameEnd = end - 1;
273
- const nameStart = nameEnd - match[2].length;
274
- embeds.push({
275
- name: match[2],
276
- start: input.mapStart(start),
277
- end: input.mapEnd(end),
278
- nameStart: input.mapStart(nameStart),
279
- nameEnd: input.mapEnd(nameEnd),
280
- });
281
- }
282
- return { output: input.value, links, embeds, noNextSection };
266
+ // Matches {{embed "name"}} or {{goto 'name'}} with a literal name.
267
+ const findNamed = (helper) => {
268
+ const result = [];
269
+ const regex = new RegExp(`\\{\\{~?\\s*${helper}\\s+(["'])(.*?)\\1`, "g");
270
+ let match;
271
+ while ((match = regex.exec(input.value))) {
272
+ const start = match.index;
273
+ const end = start + match[0].length;
274
+ const nameEnd = end - 1;
275
+ const nameStart = nameEnd - match[2].length;
276
+ result.push({
277
+ name: match[2],
278
+ start: input.mapStart(start),
279
+ end: input.mapEnd(end),
280
+ nameStart: input.mapStart(nameStart),
281
+ nameEnd: input.mapEnd(nameEnd),
282
+ });
283
+ }
284
+ return result;
285
+ };
286
+ embeds.push(...findNamed("embed"));
287
+ return { output: input.value, links, embeds, gotos: findNamed("goto"), noNextSection };
283
288
  }
284
289
  // Returns a function that converts an offset into lines.join("\n") to a position in the source file.
285
290
  export function positionMapper(lines, lineLengths) {
@@ -0,0 +1,75 @@
1
+ import type { Expr } from "./expression.js";
2
+ import type { TwinePassage, TwineStory } from "./story.js";
3
+ export interface TwineConversionProblem {
4
+ passage?: string;
5
+ text: string;
6
+ reason: string;
7
+ }
8
+ export interface TwineConversionResult {
9
+ title: string;
10
+ format: string;
11
+ files: Record<string, string>;
12
+ main: string;
13
+ passages: number;
14
+ problems: TwineConversionProblem[];
15
+ }
16
+ export interface TwineConversionOptions {
17
+ name?: string;
18
+ }
19
+ export interface PassageContext {
20
+ readonly story: StoryContext;
21
+ readonly passage: TwinePassage;
22
+ readonly section: string;
23
+ unconvertible(text: string, reason: string): string;
24
+ approximate(text: string, reason: string): void;
25
+ addPassage(nameHint: string, content: ConvertedPassage | string): string;
26
+ reservePassage(nameHint: string): {
27
+ name: string;
28
+ fill: (content: ConvertedPassage | string) => void;
29
+ };
30
+ addSection(nameHint: string, content: ConvertedPassage | string): string;
31
+ newLabel(hint: string): string;
32
+ }
33
+ export interface ConvertedPassage {
34
+ directives: string[];
35
+ body: string;
36
+ }
37
+ export declare class StoryContext {
38
+ readonly twine: TwineStory;
39
+ readonly problems: TwineConversionProblem[];
40
+ private readonly sectionNames;
41
+ private readonly usedSectionNames;
42
+ private readonly usedLabels;
43
+ readonly extraSections: string[];
44
+ private readonly permanentVars;
45
+ readonly passagesByName: Map<string, TwinePassage>;
46
+ constructor(twine: TwineStory, passages: TwinePassage[]);
47
+ private readonly problemKeys;
48
+ addProblem(problem: TwineConversionProblem): void;
49
+ notePermanentVar(name: string): void;
50
+ sectionFor(passageName: string): string | undefined;
51
+ private trimmed?;
52
+ private trimmedNames;
53
+ private readonly cache;
54
+ cached<T>(key: string, make: () => T): T;
55
+ private readonly notes;
56
+ note(key: string, text: string, reason: string): void;
57
+ noteProblems(): TwineConversionProblem[];
58
+ sectionForUnknown(passageName: string): string;
59
+ uniqueSectionName(hint: string): string;
60
+ newLabel(hint: string): string;
61
+ attribute(name: string, temporary: boolean): string;
62
+ }
63
+ export declare function sanitiseName(name: string): string;
64
+ export declare function escapeText(text: string): string;
65
+ export declare function sectionLink(text: string | null, target: string, setters?: string[]): string;
66
+ export declare function isUrl(target: string): boolean;
67
+ export declare function urlLink(text: string, url: string): string;
68
+ export declare function dynamicSectionLink(text: string, target: Expr, setters?: string[]): string;
69
+ export declare function passageLink(text: string, target: string): string;
70
+ export declare function setter(name: string, op: "=" | "+=" | "-=" | "*=" | "/=", value: Expr, inLink: boolean): string;
71
+ export declare function comment(text: string): string;
72
+ export declare function formatTwineProblems(result: TwineConversionResult, sourceName?: string): string;
73
+ export declare function convertTwine(source: string, options?: TwineConversionOptions): TwineConversionResult;
74
+ export declare function convertTwineStory(twine: TwineStory, options?: TwineConversionOptions): TwineConversionResult;
75
+ export declare function finishText(text: string): string;