squiffy-compiler 6.0.1 → 7.0.0-beta.1
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 +77 -0
- package/dist/analyse.js +520 -0
- package/dist/compiler.d.ts +12 -40
- package/dist/compiler.js +18 -691
- package/dist/emit.d.ts +43 -0
- package/dist/emit.js +114 -0
- package/dist/language-service.d.ts +77 -0
- package/dist/language-service.js +669 -0
- package/dist/parse.d.ts +17 -0
- package/dist/parse.js +258 -0
- package/dist/syntax.d.ts +146 -0
- package/dist/syntax.js +8 -0
- package/dist/text.d.ts +37 -0
- package/dist/text.js +288 -0
- package/package.json +16 -5
package/dist/compiler.js
CHANGED
|
@@ -1,701 +1,28 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
const version = pkg.version;
|
|
1
|
+
import { analyse, analysisInternals } from "./analyse.js";
|
|
2
|
+
import { emit } from "./emit.js";
|
|
4
3
|
export { findV5Syntax, convertV5Syntax } from "./v5.js";
|
|
4
|
+
// The compiler works in three stages, which can also be used separately:
|
|
5
|
+
// - parse() turns one file into a syntax tree, with a source range on every node
|
|
6
|
+
// - analyse() resolves @imports, builds the story's sections and passages, and reports diagnostics
|
|
7
|
+
// - emit() turns a successful analysis into the story data and JavaScript the runtime needs
|
|
8
|
+
export { parse } from "./parse.js";
|
|
9
|
+
export { analyse, ParseCache } from "./analyse.js";
|
|
10
|
+
export { emit } from "./emit.js";
|
|
11
|
+
export { formatDiagnostic } from "./syntax.js";
|
|
5
12
|
export async function compile(settings) {
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
const outputJs = [];
|
|
11
|
-
if (!excludeHeader) {
|
|
12
|
-
outputJs.push(`// Created with Squiffy ${version}`);
|
|
13
|
-
outputJs.push("// https://github.com/textadventures/squiffy");
|
|
14
|
-
}
|
|
15
|
-
if (settings.globalJs) {
|
|
16
|
-
outputJs.push("var story = {};");
|
|
17
|
-
}
|
|
18
|
-
else {
|
|
19
|
-
outputJs.push("export const story = {};");
|
|
20
|
-
}
|
|
21
|
-
outputJs.push(`story.id = ${JSON.stringify(storyData.story.id, null, 4)};`);
|
|
22
|
-
if (storyData.story.uiJsIndex !== undefined) {
|
|
23
|
-
outputJs.push(`story.uiJsIndex = ${storyData.story.uiJsIndex};`);
|
|
24
|
-
}
|
|
25
|
-
outputJs.push(`story.start = ${JSON.stringify(storyData.story.start, null, 4)};`);
|
|
26
|
-
outputJs.push(`story.sections = ${JSON.stringify(storyData.story.sections, null, 4)};`);
|
|
27
|
-
outputJs.push("story.js = [");
|
|
28
|
-
for (const js of storyData.js) {
|
|
29
|
-
writeJs(outputJs, 1, js);
|
|
30
|
-
}
|
|
31
|
-
outputJs.push("];");
|
|
32
|
-
return outputJs.join("\n");
|
|
33
|
-
}
|
|
34
|
-
async function getStoryData() {
|
|
35
|
-
if (!story.start) {
|
|
36
|
-
story.start = Object.keys(story.sections)[0];
|
|
37
|
-
}
|
|
38
|
-
const output = {
|
|
39
|
-
story: {
|
|
40
|
-
start: story.start,
|
|
41
|
-
id: story.id,
|
|
42
|
-
sections: {},
|
|
43
|
-
},
|
|
44
|
-
js: [],
|
|
45
|
-
};
|
|
46
|
-
if (story.uiJs.length > 0) {
|
|
47
|
-
output.js.push(story.uiJs);
|
|
48
|
-
output.story.uiJsIndex = output.js.length - 1;
|
|
49
|
-
}
|
|
50
|
-
for (const sectionName of Object.keys(story.sections)) {
|
|
51
|
-
const section = story.sections[sectionName];
|
|
52
|
-
const outputSection = {};
|
|
53
|
-
output.story.sections[sectionName] = outputSection;
|
|
54
|
-
if (section.clear) {
|
|
55
|
-
outputSection.clear = true;
|
|
56
|
-
}
|
|
57
|
-
outputSection.text = await processText(section.text.join("\n"), section, null);
|
|
58
|
-
if (section.attributes.length > 0) {
|
|
59
|
-
outputSection.attributes = section.attributes;
|
|
60
|
-
}
|
|
61
|
-
if (section.js.length > 0) {
|
|
62
|
-
output.js.push(section.js);
|
|
63
|
-
outputSection.jsIndex = output.js.length - 1;
|
|
64
|
-
}
|
|
65
|
-
if ("@last" in section.passages) {
|
|
66
|
-
let passageCount = 0;
|
|
67
|
-
for (const passageName of Object.keys(section.passages)) {
|
|
68
|
-
if (passageName.length > 0 && passageName?.substring(0, 1) !== "@") {
|
|
69
|
-
passageCount++;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
outputSection.passageCount = passageCount;
|
|
73
|
-
}
|
|
74
|
-
if (Object.keys(section.passages).length == 0)
|
|
75
|
-
continue;
|
|
76
|
-
outputSection.passages = {};
|
|
77
|
-
for (const passageName of Object.keys(section.passages)) {
|
|
78
|
-
const passage = section.passages[passageName];
|
|
79
|
-
const outputPassage = {};
|
|
80
|
-
outputSection.passages[passageName] = outputPassage;
|
|
81
|
-
if (passage.clear) {
|
|
82
|
-
outputPassage.clear = true;
|
|
83
|
-
}
|
|
84
|
-
outputPassage.text = await processText(passage.text.join("\n"), section, passage);
|
|
85
|
-
if (passage.attributes.length > 0) {
|
|
86
|
-
outputPassage.attributes = passage.attributes;
|
|
87
|
-
}
|
|
88
|
-
if (passage.js.length > 0) {
|
|
89
|
-
output.js.push(passage.js);
|
|
90
|
-
outputPassage.jsIndex = output.js.length - 1;
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
return output;
|
|
95
|
-
}
|
|
96
|
-
const regexes = {
|
|
97
|
-
section: /^\[\[(.*)\]\]:$/,
|
|
98
|
-
snippet: /^\[snippet:\s*(.+)\]:$/,
|
|
99
|
-
passage: /^\[(.*)\]:$/,
|
|
100
|
-
title: /^@title (.*)$/,
|
|
101
|
-
import: /^@import (.*)$/,
|
|
102
|
-
start: /^@start (.*)$/,
|
|
103
|
-
attributes: /^@set (.*)$/,
|
|
104
|
-
unset: /^@unset (.*)$/,
|
|
105
|
-
inc: /^@inc (\S+)(?: (\d+))?$/,
|
|
106
|
-
dec: /^@dec (\S+)(?: (\d+))?$/,
|
|
107
|
-
js: /^(\t| {4})(.*)$/,
|
|
108
|
-
continue: /^\+\+\+(.*)$/,
|
|
109
|
-
ui: /^@ui (.*)$/,
|
|
110
|
-
};
|
|
111
|
-
async function processFileText(inputText, inputFilename, isFirst) {
|
|
112
|
-
warnAboutV5Syntax(inputText, inputFilename);
|
|
113
|
-
const inputLines = inputText.replace(/\r/g, "").split("\n");
|
|
114
|
-
let lineCount = 0;
|
|
115
|
-
let section = null;
|
|
116
|
-
let passage = null; // annotated differently to section, as a workaround for TypeScript "Property does not exist on type never"
|
|
117
|
-
let textStarted = false;
|
|
118
|
-
let inUiBlock = false;
|
|
119
|
-
let inSnippet = false;
|
|
120
|
-
const ensureThisSectionExists = () => {
|
|
121
|
-
return ensureSectionExists(section, isFirst, inputFilename, lineCount);
|
|
122
|
-
};
|
|
123
|
-
const addAutoSection = () => {
|
|
124
|
-
autoSectionCount++;
|
|
125
|
-
const autoSectionName = `_continue${autoSectionCount}`;
|
|
126
|
-
section = story.addSection(autoSectionName, inputFilename, lineCount);
|
|
127
|
-
passage = null;
|
|
128
|
-
textStarted = false;
|
|
129
|
-
return autoSectionName;
|
|
130
|
-
};
|
|
131
|
-
for (const line of inputLines) {
|
|
132
|
-
const stripLine = line.trim();
|
|
133
|
-
lineCount++;
|
|
134
|
-
const match = {};
|
|
135
|
-
for (const key of Object.keys(regexes)) {
|
|
136
|
-
const regex = regexes[key];
|
|
137
|
-
const result = key == "js" ? regex.exec(line) : regex.exec(stripLine);
|
|
138
|
-
if (result) {
|
|
139
|
-
match[key] = result;
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
if (match.snippet) {
|
|
143
|
-
inSnippet = true;
|
|
144
|
-
section = null;
|
|
145
|
-
passage = null;
|
|
146
|
-
textStarted = false;
|
|
147
|
-
}
|
|
148
|
-
else if (match.section) {
|
|
149
|
-
inSnippet = false;
|
|
150
|
-
section = story.addSection(match.section[1], inputFilename, lineCount);
|
|
151
|
-
passage = null;
|
|
152
|
-
textStarted = false;
|
|
153
|
-
}
|
|
154
|
-
else if (inSnippet) {
|
|
155
|
-
// skip all content inside snippet blocks
|
|
156
|
-
}
|
|
157
|
-
else if (match.passage) {
|
|
158
|
-
if (!section) {
|
|
159
|
-
errors.push(`ERROR: ${inputFilename} line ${lineCount}: Can't add passage "${match.passage[1]}" as no section has been created.`);
|
|
160
|
-
return false;
|
|
161
|
-
}
|
|
162
|
-
section = ensureThisSectionExists();
|
|
163
|
-
passage = section.addPassage(match.passage[1], lineCount);
|
|
164
|
-
textStarted = false;
|
|
165
|
-
}
|
|
166
|
-
else if (match.continue) {
|
|
167
|
-
section = ensureThisSectionExists();
|
|
168
|
-
const previousSection = section;
|
|
169
|
-
const autoSectionName = addAutoSection();
|
|
170
|
-
const text = match.continue[1] || "Continue...";
|
|
171
|
-
previousSection.addText(`[[${text}]](${autoSectionName})`);
|
|
172
|
-
}
|
|
173
|
-
else if (stripLine == "---") {
|
|
174
|
-
inUiBlock = false;
|
|
175
|
-
if (!section) {
|
|
176
|
-
// Just add the _default section if we haven't started yet
|
|
177
|
-
ensureThisSectionExists();
|
|
178
|
-
}
|
|
179
|
-
else {
|
|
180
|
-
addAutoSection();
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
else if (stripLine == "@ui") {
|
|
184
|
-
inUiBlock = true;
|
|
185
|
-
}
|
|
186
|
-
else if (match.ui) {
|
|
187
|
-
if (settings.externalFiles) {
|
|
188
|
-
const content = await settings.externalFiles.getContent(match.ui[1]);
|
|
189
|
-
story.addUiJs(content);
|
|
190
|
-
}
|
|
191
|
-
else {
|
|
192
|
-
warnExternalFilesUnavailable(stripLine, inputFilename, lineCount);
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
else if (stripLine == "@clear") {
|
|
196
|
-
if (!passage) {
|
|
197
|
-
section = ensureThisSectionExists();
|
|
198
|
-
section.clear = true;
|
|
199
|
-
}
|
|
200
|
-
else {
|
|
201
|
-
passage.clear = true;
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
else if (match.title) {
|
|
205
|
-
story.title = match.title[1];
|
|
206
|
-
}
|
|
207
|
-
else if (match.start) {
|
|
208
|
-
story.start = match.start[1];
|
|
209
|
-
}
|
|
210
|
-
else if (match.import && !settings.externalFiles) {
|
|
211
|
-
warnExternalFilesUnavailable(stripLine, inputFilename, lineCount);
|
|
212
|
-
}
|
|
213
|
-
else if (match.import && settings.externalFiles) {
|
|
214
|
-
const importFilenames = await settings.externalFiles.getMatchingFilenames(match.import[1]);
|
|
215
|
-
for (const importFilename of importFilenames) {
|
|
216
|
-
if (importFilename.endsWith(".squiffy")) {
|
|
217
|
-
const content = await settings.externalFiles.getContent(importFilename);
|
|
218
|
-
const success = await processFileText(content, importFilename, false);
|
|
219
|
-
if (!success)
|
|
220
|
-
return false;
|
|
221
|
-
}
|
|
222
|
-
else if (importFilename.endsWith(".js")) {
|
|
223
|
-
story.scripts.push(settings.externalFiles.getLocalFilename(importFilename));
|
|
224
|
-
}
|
|
225
|
-
else if (importFilename.endsWith(".css")) {
|
|
226
|
-
story.stylesheets.push(settings.externalFiles.getLocalFilename(importFilename));
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
else if (match.attributes) {
|
|
231
|
-
section = ensureThisSectionExists();
|
|
232
|
-
section = addAttribute(match.attributes[1], section, passage, isFirst, inputFilename, lineCount);
|
|
233
|
-
}
|
|
234
|
-
else if (match.unset) {
|
|
235
|
-
section = ensureThisSectionExists();
|
|
236
|
-
section = addAttribute("not " + match.unset[1], section, passage, isFirst, inputFilename, lineCount);
|
|
237
|
-
}
|
|
238
|
-
else if (match.inc) {
|
|
239
|
-
section = ensureThisSectionExists();
|
|
240
|
-
section = addAttribute(match.inc[1] + "+=" + (match.inc[2] === undefined ? "1" : match.inc[2]), section, passage, isFirst, inputFilename, lineCount);
|
|
241
|
-
}
|
|
242
|
-
else if (match.dec) {
|
|
243
|
-
section = ensureThisSectionExists();
|
|
244
|
-
section = addAttribute(match.dec[1] + "-=" + (match.dec[2] === undefined ? "1" : match.dec[2]), section, passage, isFirst, inputFilename, lineCount);
|
|
245
|
-
}
|
|
246
|
-
else if (!textStarted && match.js) {
|
|
247
|
-
if (inUiBlock) {
|
|
248
|
-
story.addUiJs(match.js[2]);
|
|
249
|
-
}
|
|
250
|
-
else if (!passage) {
|
|
251
|
-
section = ensureThisSectionExists();
|
|
252
|
-
section.addJS(match.js[2]);
|
|
253
|
-
}
|
|
254
|
-
else {
|
|
255
|
-
passage.addJS(match.js[2]);
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
else if (textStarted || stripLine.length > 0) {
|
|
259
|
-
if (inUiBlock) {
|
|
260
|
-
errors.push(`ERROR: ${inputFilename} line ${lineCount}: Unexpected text in @ui block.`);
|
|
261
|
-
return false;
|
|
262
|
-
}
|
|
263
|
-
else if (!passage) {
|
|
264
|
-
section = ensureThisSectionExists();
|
|
265
|
-
if (section) {
|
|
266
|
-
section.addText(line);
|
|
267
|
-
textStarted = true;
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
else {
|
|
271
|
-
passage.addText(line);
|
|
272
|
-
textStarted = true;
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
return true;
|
|
277
|
-
}
|
|
278
|
-
function warnExternalFilesUnavailable(line, inputFilename, lineCount) {
|
|
279
|
-
settings.onWarning?.(`WARNING: ${inputFilename} line ${lineCount}: "${line}" was ignored, as other files can't be loaded here`);
|
|
280
|
-
}
|
|
281
|
-
function warnAboutV5Syntax(inputText, inputFilename) {
|
|
282
|
-
if (!settings.onWarning)
|
|
283
|
-
return;
|
|
284
|
-
const truncate = (s) => s.length > 60 ? s.substring(0, 57) + "..." : s;
|
|
285
|
-
for (const match of findV5Syntax(inputText)) {
|
|
286
|
-
let warning = `WARNING: ${inputFilename} line ${match.line}: ${truncate(match.text)} looks like Squiffy 5 text processor syntax, which isn't supported in Squiffy 6.`;
|
|
287
|
-
if (match.suggestion) {
|
|
288
|
-
warning += match.suggestion.length > 60
|
|
289
|
-
? " Convert it to Handlebars syntax."
|
|
290
|
-
: ` Use ${match.suggestion} instead.`;
|
|
291
|
-
}
|
|
292
|
-
else if (match.text.startsWith("{else:")) {
|
|
293
|
-
warning += " Use {{else}} inside an {{#if}} block instead.";
|
|
294
|
-
}
|
|
13
|
+
const analysis = await analyse(settings);
|
|
14
|
+
const { legacyErrors, legacyWarnings } = analysisInternals.get(analysis);
|
|
15
|
+
if (settings.onWarning) {
|
|
16
|
+
for (const warning of legacyWarnings) {
|
|
295
17
|
settings.onWarning(warning);
|
|
296
18
|
}
|
|
297
19
|
}
|
|
298
|
-
|
|
299
|
-
if (!section && isFirst) {
|
|
300
|
-
section = story.addSection("_default", inputFilename, lineCount);
|
|
301
|
-
}
|
|
302
|
-
return section;
|
|
303
|
-
}
|
|
304
|
-
function addAttribute(attribute, section, passage, isFirst, inputFilename, lineCount) {
|
|
305
|
-
if (!passage) {
|
|
306
|
-
section = ensureSectionExists(section, isFirst, inputFilename, lineCount);
|
|
307
|
-
section.addAttribute(attribute);
|
|
308
|
-
}
|
|
309
|
-
else {
|
|
310
|
-
passage.addAttribute(attribute);
|
|
311
|
-
}
|
|
312
|
-
return section;
|
|
313
|
-
}
|
|
314
|
-
function extractLinkFunctions(link) {
|
|
315
|
-
const fragments = link.split(",");
|
|
316
|
-
return {
|
|
317
|
-
target: fragments[0].trim(),
|
|
318
|
-
setters: fragments.slice(1).join(", ")
|
|
319
|
-
};
|
|
320
|
-
}
|
|
321
|
-
function getAdditionalLinkParameters(link) {
|
|
322
|
-
const functions = extractLinkFunctions(link);
|
|
323
|
-
let additionalParameters = "";
|
|
324
|
-
if (functions.setters.length > 0) {
|
|
325
|
-
additionalParameters += ` set="${functions.setters}"`;
|
|
326
|
-
}
|
|
327
|
-
return {
|
|
328
|
-
target: functions.target,
|
|
329
|
-
additionalParameters
|
|
330
|
-
};
|
|
331
|
-
}
|
|
332
|
-
function escapeForHandlebarsString(s) {
|
|
333
|
-
return s.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
|
|
334
|
-
}
|
|
335
|
-
async function processText(input, section, passage) {
|
|
336
|
-
// Helper to get the next section name
|
|
337
|
-
const getNextSectionName = () => {
|
|
338
|
-
const sectionNames = Object.keys(story.sections);
|
|
339
|
-
const currentIndex = sectionNames.indexOf(section.name);
|
|
340
|
-
return sectionNames[currentIndex + 1] || null;
|
|
341
|
-
};
|
|
342
|
-
// Handlebars expressions can contain square brackets that aren't links, such as
|
|
343
|
-
// {{someArray.[1]}} or {{someHelper "[x]"}}, so swap each one for a placeholder while
|
|
344
|
-
// converting links. Text between block helpers like {{#if x}}...{{/if}} is still converted.
|
|
345
|
-
const mustaches = [];
|
|
346
|
-
input = input.replace(/\{\{\{[\s\S]*?\}\}\}|\{\{[\s\S]*?\}\}/g, match => {
|
|
347
|
-
mustaches.push(match);
|
|
348
|
-
return `\uE000${mustaches.length - 1}\uE001`;
|
|
349
|
-
});
|
|
350
|
-
const restore = (s) => s.replace(/\uE000(\d+)\uE001/g, (_match, i) => mustaches[Number(i)]);
|
|
351
|
-
// nextSectionLinkRegex matches [[text>]] - a link to the next section
|
|
352
|
-
const nextSectionLinkRegex = /\[\[([^\]]*?)>\]\]/g;
|
|
353
|
-
input = input.replace(nextSectionLinkRegex, (_match, text) => {
|
|
354
|
-
text = restore(text);
|
|
355
|
-
const nextSectionName = getNextSectionName();
|
|
356
|
-
if (!nextSectionName) {
|
|
357
|
-
settings.onWarning?.(`WARNING: ${section.filename} line ${section.line}: In section '${section.name}', there is a [[${text}>]] link but no following section exists`);
|
|
358
|
-
return `[[${text}]]`; // fallback
|
|
359
|
-
}
|
|
360
|
-
return `{{section "${escapeForHandlebarsString(nextSectionName)}" text="${escapeForHandlebarsString(text)}"}}`;
|
|
361
|
-
});
|
|
362
|
-
// namedNextSectionLinkRegex matches [[text]](>) or [[text]](>, setter=value)
|
|
363
|
-
// - a named link where target starts with >
|
|
364
|
-
const namedNextSectionLinkRegex = /\[\[([^\]]*?)\]\]\(>(.*?)\)/g;
|
|
365
|
-
input = input.replace(namedNextSectionLinkRegex, (_match, text, rest) => {
|
|
366
|
-
text = restore(text);
|
|
367
|
-
rest = restore(rest);
|
|
368
|
-
const nextSectionName = getNextSectionName();
|
|
369
|
-
if (!nextSectionName) {
|
|
370
|
-
settings.onWarning?.(`WARNING: ${section.filename} line ${section.line}: In section '${section.name}', there is a [[${text}]](>) link but no following section exists`);
|
|
371
|
-
return `[[${text}]]`; // fallback
|
|
372
|
-
}
|
|
373
|
-
// rest could be empty or ", setter=value"
|
|
374
|
-
const parsedName = getAdditionalLinkParameters(nextSectionName + rest);
|
|
375
|
-
return `{{section "${escapeForHandlebarsString(parsedName.target)}" text="${escapeForHandlebarsString(text)}"${parsedName.additionalParameters}}}`;
|
|
376
|
-
});
|
|
377
|
-
// namedSectionLinkRegex matches:
|
|
378
|
-
// open [[
|
|
379
|
-
// any text - the link text
|
|
380
|
-
// closing ]]
|
|
381
|
-
// open bracket
|
|
382
|
-
// any text - the name of the section
|
|
383
|
-
// closing bracket
|
|
384
|
-
const namedSectionLinkRegex = /\[\[([^\]]*?)\]\]\((.*?)\)/g;
|
|
385
|
-
let links = allMatchesForGroup(input, namedSectionLinkRegex, 2).map(restore);
|
|
386
|
-
checkSectionLinks(links, section, passage);
|
|
387
|
-
input = input.replace(namedSectionLinkRegex, (_match, text /* $1 */, name /* $2 */) => {
|
|
388
|
-
text = restore(text);
|
|
389
|
-
const parsedName = getAdditionalLinkParameters(restore(name));
|
|
390
|
-
return `{{section "${escapeForHandlebarsString(parsedName.target)}" text="${escapeForHandlebarsString(text)}"${parsedName.additionalParameters}}}`;
|
|
391
|
-
});
|
|
392
|
-
// namedPassageLinkRegex matches:
|
|
393
|
-
// not preceded by ! (to avoid matching image links like )
|
|
394
|
-
// open [
|
|
395
|
-
// any text - the link text
|
|
396
|
-
// closing ]
|
|
397
|
-
// open bracket, but not http(s):// after it
|
|
398
|
-
// any text - the name of the passage
|
|
399
|
-
// closing bracket
|
|
400
|
-
const namedPassageLinkRegex = /(?<!!)\[([^\]]*?)\]\(((?!https?:\/\/).*?)\)/g;
|
|
401
|
-
links = allMatchesForGroup(input, namedPassageLinkRegex, 2).map(restore);
|
|
402
|
-
checkPassageLinks(links, section, passage);
|
|
403
|
-
input = input.replace(namedPassageLinkRegex, (_match, text /* $1 */, name /* $2 */) => {
|
|
404
|
-
text = restore(text);
|
|
405
|
-
const parsedName = getAdditionalLinkParameters(restore(name));
|
|
406
|
-
return `{{passage "${escapeForHandlebarsString(parsedName.target)}" text="${escapeForHandlebarsString(text)}"${parsedName.additionalParameters}}}`;
|
|
407
|
-
});
|
|
408
|
-
// unnamedSectionLinkRegex matches:
|
|
409
|
-
// open [[
|
|
410
|
-
// any text - the link text
|
|
411
|
-
// closing ]]
|
|
412
|
-
const unnamedSectionLinkRegex = /\[\[(.*?)\]\]/g;
|
|
413
|
-
links = allMatchesForGroup(input, unnamedSectionLinkRegex, 1).map(restore);
|
|
414
|
-
checkSectionLinks(links, section, passage);
|
|
415
|
-
input = input.replace(unnamedSectionLinkRegex, (_match, name) => `{{section "${escapeForHandlebarsString(restore(name))}"}}`);
|
|
416
|
-
// unnamedPassageLinkRegex matches:
|
|
417
|
-
// not preceded by ! (to avoid matching image links like ![alt])
|
|
418
|
-
// open [
|
|
419
|
-
// any text - the link text
|
|
420
|
-
// closing ]
|
|
421
|
-
// no bracket after
|
|
422
|
-
const unnamedPassageLinkRegex = /(?<!!)\[(.*?)\]([^(]|$)/g;
|
|
423
|
-
links = allMatchesForGroup(input, unnamedPassageLinkRegex, 1).map(restore);
|
|
424
|
-
checkPassageLinks(links, section, passage);
|
|
425
|
-
input = input.replace(unnamedPassageLinkRegex, (_match, name, suffix) => `{{passage "${escapeForHandlebarsString(restore(name))}"}}${suffix}`);
|
|
426
|
-
input = restore(input);
|
|
427
|
-
checkEmbeds(input, section, passage);
|
|
428
|
-
return input;
|
|
429
|
-
}
|
|
430
|
-
function checkEmbeds(input, section, passage) {
|
|
431
|
-
if (!settings.onWarning)
|
|
432
|
-
return;
|
|
433
|
-
// embedRegex matches {{embed "name"}} or {{embed 'name'}} with a literal name.
|
|
434
|
-
// Like the runtime's embed helper, a name can refer to a passage in the
|
|
435
|
-
// current section or to any section.
|
|
436
|
-
const embedRegex = /\{\{~?\s*embed\s+(["'])(.*?)\1/g;
|
|
437
|
-
const embeds = allMatchesForGroup(input, embedRegex, 2);
|
|
438
|
-
const badEmbeds = embeds.filter(name => !(name in section.passages) && !(name in story.sections));
|
|
439
|
-
for (const badEmbed of badEmbeds) {
|
|
440
|
-
const location = passage
|
|
441
|
-
? `${section.filename} line ${passage.line}: In section '${section.name}', passage '${passage.name}'`
|
|
442
|
-
: `${section.filename} line ${section.line}: In section '${section.name}'`;
|
|
443
|
-
settings.onWarning(`WARNING: ${location} there is an embed of {{embed "${badEmbed}"}}, but no section or passage with that name exists`);
|
|
444
|
-
}
|
|
445
|
-
}
|
|
446
|
-
function allMatchesForGroup(input, regex, groupNumber) {
|
|
447
|
-
const result = [];
|
|
448
|
-
let match;
|
|
449
|
-
while ((match = regex.exec(input))) {
|
|
450
|
-
result.push(match[groupNumber]);
|
|
451
|
-
}
|
|
452
|
-
return result;
|
|
453
|
-
}
|
|
454
|
-
function checkSectionLinks(links, section, passage) {
|
|
455
|
-
const badLinks = links.filter(m => !linkDestinationExists(m, story.sections));
|
|
456
|
-
showBadLinksWarning(badLinks, "section", "[[", "]]", section, passage);
|
|
457
|
-
}
|
|
458
|
-
function checkPassageLinks(links, section, passage) {
|
|
459
|
-
const badLinks = links.filter(m => !linkDestinationExists(m, section.passages));
|
|
460
|
-
showBadLinksWarning(badLinks, "passage", "[", "]", section, passage);
|
|
461
|
-
}
|
|
462
|
-
function linkDestinationExists(link, keys) {
|
|
463
|
-
// Link destination data may look like:
|
|
464
|
-
// passageName
|
|
465
|
-
// passageName, my_attribute=2
|
|
466
|
-
// passage name with a comma, in the name
|
|
467
|
-
// We're only interested in checking if the named passage or section exists.
|
|
468
|
-
// First check if the full link is a valid destination (handles names containing commas).
|
|
469
|
-
const trimmedLink = link.trim();
|
|
470
|
-
if (Object.keys(keys).includes(trimmedLink)) {
|
|
471
|
-
return true;
|
|
472
|
-
}
|
|
473
|
-
// Otherwise, assume a comma separates the destination from setters.
|
|
474
|
-
const linkDestination = trimmedLink.split(",")[0].trim();
|
|
475
|
-
return Object.keys(keys).includes(linkDestination);
|
|
476
|
-
}
|
|
477
|
-
function showBadLinksWarning(badLinks, linkTo, before, after, section, passage) {
|
|
478
|
-
if (!settings.onWarning)
|
|
479
|
-
return;
|
|
480
|
-
for (const badLink of badLinks) {
|
|
481
|
-
let warning;
|
|
482
|
-
if (!passage) {
|
|
483
|
-
warning = `${section.filename} line ${section.line}: In section '${section.name}'`;
|
|
484
|
-
}
|
|
485
|
-
else {
|
|
486
|
-
warning = `${section.filename} line ${passage.line}: In section '${section.name}', passage '${passage.name}'`;
|
|
487
|
-
}
|
|
488
|
-
settings.onWarning(`WARNING: ${warning} there is a link to a ${linkTo} called ${before}${badLink}${after}, which doesn't exist`);
|
|
489
|
-
}
|
|
490
|
-
}
|
|
491
|
-
function writeJs(outputJsFile, tabCount, js) {
|
|
492
|
-
const tabs = new Array(tabCount + 1).join("\t");
|
|
493
|
-
outputJsFile.push(`${tabs}(squiffy, get, set) => {`);
|
|
494
|
-
for (const jsLine of js) {
|
|
495
|
-
outputJsFile.push(`${tabs}\t${jsLine}`);
|
|
496
|
-
}
|
|
497
|
-
outputJsFile.push(`${tabs}},`);
|
|
498
|
-
}
|
|
499
|
-
const success = await processFileText(settings.script, settings.scriptBaseFilename, true);
|
|
500
|
-
if (success) {
|
|
501
|
-
if (!Object.keys(story.sections).length) {
|
|
502
|
-
ensureSectionExists(null, true, settings.scriptBaseFilename, 0);
|
|
503
|
-
}
|
|
504
|
-
const storyData = await getStoryData();
|
|
505
|
-
return {
|
|
506
|
-
success: true,
|
|
507
|
-
output: storyData,
|
|
508
|
-
getJs: (excludeHeader) => {
|
|
509
|
-
return getJs(storyData, excludeHeader || false);
|
|
510
|
-
},
|
|
511
|
-
getUiInfo: () => {
|
|
512
|
-
return {
|
|
513
|
-
title: story.title,
|
|
514
|
-
externalScripts: story.scripts,
|
|
515
|
-
externalStylesheets: story.stylesheets,
|
|
516
|
-
};
|
|
517
|
-
}
|
|
518
|
-
};
|
|
519
|
-
}
|
|
520
|
-
else {
|
|
20
|
+
if (!analysis.success) {
|
|
521
21
|
return {
|
|
522
22
|
success: false,
|
|
523
|
-
errors:
|
|
23
|
+
errors: legacyErrors,
|
|
24
|
+
diagnostics: analysis.diagnostics,
|
|
524
25
|
};
|
|
525
26
|
}
|
|
526
|
-
}
|
|
527
|
-
class Story {
|
|
528
|
-
constructor() {
|
|
529
|
-
Object.defineProperty(this, "sections", {
|
|
530
|
-
enumerable: true,
|
|
531
|
-
configurable: true,
|
|
532
|
-
writable: true,
|
|
533
|
-
value: {}
|
|
534
|
-
});
|
|
535
|
-
Object.defineProperty(this, "title", {
|
|
536
|
-
enumerable: true,
|
|
537
|
-
configurable: true,
|
|
538
|
-
writable: true,
|
|
539
|
-
value: ""
|
|
540
|
-
});
|
|
541
|
-
Object.defineProperty(this, "scripts", {
|
|
542
|
-
enumerable: true,
|
|
543
|
-
configurable: true,
|
|
544
|
-
writable: true,
|
|
545
|
-
value: []
|
|
546
|
-
});
|
|
547
|
-
Object.defineProperty(this, "stylesheets", {
|
|
548
|
-
enumerable: true,
|
|
549
|
-
configurable: true,
|
|
550
|
-
writable: true,
|
|
551
|
-
value: []
|
|
552
|
-
});
|
|
553
|
-
Object.defineProperty(this, "start", {
|
|
554
|
-
enumerable: true,
|
|
555
|
-
configurable: true,
|
|
556
|
-
writable: true,
|
|
557
|
-
value: ""
|
|
558
|
-
});
|
|
559
|
-
Object.defineProperty(this, "id", {
|
|
560
|
-
enumerable: true,
|
|
561
|
-
configurable: true,
|
|
562
|
-
writable: true,
|
|
563
|
-
value: null
|
|
564
|
-
});
|
|
565
|
-
Object.defineProperty(this, "uiJs", {
|
|
566
|
-
enumerable: true,
|
|
567
|
-
configurable: true,
|
|
568
|
-
writable: true,
|
|
569
|
-
value: []
|
|
570
|
-
});
|
|
571
|
-
this.id = crypto.randomUUID();
|
|
572
|
-
}
|
|
573
|
-
addSection(name, filename, line) {
|
|
574
|
-
const section = new Section(name, filename, line);
|
|
575
|
-
this.sections[name] = section;
|
|
576
|
-
return section;
|
|
577
|
-
}
|
|
578
|
-
addUiJs(text) {
|
|
579
|
-
this.uiJs.push(text);
|
|
580
|
-
}
|
|
581
|
-
}
|
|
582
|
-
class Section {
|
|
583
|
-
constructor(name, filename, line) {
|
|
584
|
-
Object.defineProperty(this, "name", {
|
|
585
|
-
enumerable: true,
|
|
586
|
-
configurable: true,
|
|
587
|
-
writable: true,
|
|
588
|
-
value: void 0
|
|
589
|
-
});
|
|
590
|
-
Object.defineProperty(this, "filename", {
|
|
591
|
-
enumerable: true,
|
|
592
|
-
configurable: true,
|
|
593
|
-
writable: true,
|
|
594
|
-
value: void 0
|
|
595
|
-
});
|
|
596
|
-
Object.defineProperty(this, "line", {
|
|
597
|
-
enumerable: true,
|
|
598
|
-
configurable: true,
|
|
599
|
-
writable: true,
|
|
600
|
-
value: void 0
|
|
601
|
-
});
|
|
602
|
-
Object.defineProperty(this, "text", {
|
|
603
|
-
enumerable: true,
|
|
604
|
-
configurable: true,
|
|
605
|
-
writable: true,
|
|
606
|
-
value: []
|
|
607
|
-
});
|
|
608
|
-
Object.defineProperty(this, "passages", {
|
|
609
|
-
enumerable: true,
|
|
610
|
-
configurable: true,
|
|
611
|
-
writable: true,
|
|
612
|
-
value: {}
|
|
613
|
-
});
|
|
614
|
-
Object.defineProperty(this, "js", {
|
|
615
|
-
enumerable: true,
|
|
616
|
-
configurable: true,
|
|
617
|
-
writable: true,
|
|
618
|
-
value: []
|
|
619
|
-
});
|
|
620
|
-
Object.defineProperty(this, "clear", {
|
|
621
|
-
enumerable: true,
|
|
622
|
-
configurable: true,
|
|
623
|
-
writable: true,
|
|
624
|
-
value: false
|
|
625
|
-
});
|
|
626
|
-
Object.defineProperty(this, "attributes", {
|
|
627
|
-
enumerable: true,
|
|
628
|
-
configurable: true,
|
|
629
|
-
writable: true,
|
|
630
|
-
value: []
|
|
631
|
-
});
|
|
632
|
-
this.name = name;
|
|
633
|
-
this.filename = filename;
|
|
634
|
-
this.line = line;
|
|
635
|
-
}
|
|
636
|
-
addPassage(name, line) {
|
|
637
|
-
const passage = new Passage(name, line);
|
|
638
|
-
this.passages[name] = passage;
|
|
639
|
-
return passage;
|
|
640
|
-
}
|
|
641
|
-
addText(text) {
|
|
642
|
-
this.text.push(text);
|
|
643
|
-
}
|
|
644
|
-
addJS(text) {
|
|
645
|
-
this.js.push(text);
|
|
646
|
-
}
|
|
647
|
-
addAttribute(text) {
|
|
648
|
-
this.attributes.push(text);
|
|
649
|
-
}
|
|
650
|
-
}
|
|
651
|
-
class Passage {
|
|
652
|
-
constructor(name, line) {
|
|
653
|
-
Object.defineProperty(this, "name", {
|
|
654
|
-
enumerable: true,
|
|
655
|
-
configurable: true,
|
|
656
|
-
writable: true,
|
|
657
|
-
value: void 0
|
|
658
|
-
});
|
|
659
|
-
Object.defineProperty(this, "line", {
|
|
660
|
-
enumerable: true,
|
|
661
|
-
configurable: true,
|
|
662
|
-
writable: true,
|
|
663
|
-
value: void 0
|
|
664
|
-
});
|
|
665
|
-
Object.defineProperty(this, "text", {
|
|
666
|
-
enumerable: true,
|
|
667
|
-
configurable: true,
|
|
668
|
-
writable: true,
|
|
669
|
-
value: []
|
|
670
|
-
});
|
|
671
|
-
Object.defineProperty(this, "js", {
|
|
672
|
-
enumerable: true,
|
|
673
|
-
configurable: true,
|
|
674
|
-
writable: true,
|
|
675
|
-
value: []
|
|
676
|
-
});
|
|
677
|
-
Object.defineProperty(this, "clear", {
|
|
678
|
-
enumerable: true,
|
|
679
|
-
configurable: true,
|
|
680
|
-
writable: true,
|
|
681
|
-
value: false
|
|
682
|
-
});
|
|
683
|
-
Object.defineProperty(this, "attributes", {
|
|
684
|
-
enumerable: true,
|
|
685
|
-
configurable: true,
|
|
686
|
-
writable: true,
|
|
687
|
-
value: []
|
|
688
|
-
});
|
|
689
|
-
this.name = name;
|
|
690
|
-
this.line = line;
|
|
691
|
-
}
|
|
692
|
-
addText(text) {
|
|
693
|
-
this.text.push(text);
|
|
694
|
-
}
|
|
695
|
-
addJS(text) {
|
|
696
|
-
this.js.push(text);
|
|
697
|
-
}
|
|
698
|
-
addAttribute(text) {
|
|
699
|
-
this.attributes.push(text);
|
|
700
|
-
}
|
|
27
|
+
return emit(analysis, { globalJs: settings.globalJs });
|
|
701
28
|
}
|