squiffy-compiler 6.0.0-beta.4 → 6.0.0
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/compiler.d.ts +2 -1
- package/dist/compiler.js +43 -8
- package/dist/v5.d.ts +17 -0
- package/dist/v5.js +466 -0
- package/package.json +2 -2
package/dist/compiler.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
export { findV5Syntax, convertV5Syntax } from "./v5.js";
|
|
2
|
+
export type { V5SyntaxMatch, V5ConversionProblem, V5ConversionResult } from "./v5.js";
|
|
1
3
|
export interface Output {
|
|
2
4
|
story: OutputStory;
|
|
3
5
|
js: string[][];
|
|
@@ -50,4 +52,3 @@ export interface CompileError {
|
|
|
50
52
|
errors: string[];
|
|
51
53
|
}
|
|
52
54
|
export declare function compile(settings: CompilerSettings): Promise<CompileSuccess | CompileError>;
|
|
53
|
-
export {};
|
package/dist/compiler.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import pkg from "../package.json" with { type: "json" };
|
|
2
|
+
import { findV5Syntax } from "./v5.js";
|
|
2
3
|
const version = pkg.version;
|
|
4
|
+
export { findV5Syntax, convertV5Syntax } from "./v5.js";
|
|
3
5
|
export async function compile(settings) {
|
|
4
6
|
const story = new Story();
|
|
5
7
|
const errors = [];
|
|
@@ -107,6 +109,7 @@ export async function compile(settings) {
|
|
|
107
109
|
ui: /^@ui (.*)$/,
|
|
108
110
|
};
|
|
109
111
|
async function processFileText(inputText, inputFilename, isFirst) {
|
|
112
|
+
warnAboutV5Syntax(inputText, inputFilename);
|
|
110
113
|
const inputLines = inputText.replace(/\r/g, "").split("\n");
|
|
111
114
|
let lineCount = 0;
|
|
112
115
|
let section = null;
|
|
@@ -264,6 +267,23 @@ export async function compile(settings) {
|
|
|
264
267
|
}
|
|
265
268
|
return true;
|
|
266
269
|
}
|
|
270
|
+
function warnAboutV5Syntax(inputText, inputFilename) {
|
|
271
|
+
if (!settings.onWarning)
|
|
272
|
+
return;
|
|
273
|
+
const truncate = (s) => s.length > 60 ? s.substring(0, 57) + "..." : s;
|
|
274
|
+
for (const match of findV5Syntax(inputText)) {
|
|
275
|
+
let warning = `WARNING: ${inputFilename} line ${match.line}: ${truncate(match.text)} looks like Squiffy 5 text processor syntax, which isn't supported in Squiffy 6.`;
|
|
276
|
+
if (match.suggestion) {
|
|
277
|
+
warning += match.suggestion.length > 60
|
|
278
|
+
? " Convert it to Handlebars syntax."
|
|
279
|
+
: ` Use ${match.suggestion} instead.`;
|
|
280
|
+
}
|
|
281
|
+
else if (match.text.startsWith("{else:")) {
|
|
282
|
+
warning += " Use {{else}} inside an {{#if}} block instead.";
|
|
283
|
+
}
|
|
284
|
+
settings.onWarning(warning);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
267
287
|
function ensureSectionExists(section, isFirst, inputFilename, lineCount) {
|
|
268
288
|
if (!section && isFirst) {
|
|
269
289
|
section = story.addSection("_default", inputFilename, lineCount);
|
|
@@ -308,9 +328,19 @@ export async function compile(settings) {
|
|
|
308
328
|
const currentIndex = sectionNames.indexOf(section.name);
|
|
309
329
|
return sectionNames[currentIndex + 1] || null;
|
|
310
330
|
};
|
|
331
|
+
// Handlebars expressions can contain square brackets that aren't links, such as
|
|
332
|
+
// {{someArray.[1]}} or {{someHelper "[x]"}}, so swap each one for a placeholder while
|
|
333
|
+
// converting links. Text between block helpers like {{#if x}}...{{/if}} is still converted.
|
|
334
|
+
const mustaches = [];
|
|
335
|
+
input = input.replace(/\{\{\{[\s\S]*?\}\}\}|\{\{[\s\S]*?\}\}/g, match => {
|
|
336
|
+
mustaches.push(match);
|
|
337
|
+
return `\uE000${mustaches.length - 1}\uE001`;
|
|
338
|
+
});
|
|
339
|
+
const restore = (s) => s.replace(/\uE000(\d+)\uE001/g, (_match, i) => mustaches[Number(i)]);
|
|
311
340
|
// nextSectionLinkRegex matches [[text>]] - a link to the next section
|
|
312
341
|
const nextSectionLinkRegex = /\[\[([^\]]*?)>\]\]/g;
|
|
313
342
|
input = input.replace(nextSectionLinkRegex, (_match, text) => {
|
|
343
|
+
text = restore(text);
|
|
314
344
|
const nextSectionName = getNextSectionName();
|
|
315
345
|
if (!nextSectionName) {
|
|
316
346
|
settings.onWarning?.(`WARNING: ${section.filename} line ${section.line}: In section '${section.name}', there is a [[${text}>]] link but no following section exists`);
|
|
@@ -322,6 +352,8 @@ export async function compile(settings) {
|
|
|
322
352
|
// - a named link where target starts with >
|
|
323
353
|
const namedNextSectionLinkRegex = /\[\[([^\]]*?)\]\]\(>(.*?)\)/g;
|
|
324
354
|
input = input.replace(namedNextSectionLinkRegex, (_match, text, rest) => {
|
|
355
|
+
text = restore(text);
|
|
356
|
+
rest = restore(rest);
|
|
325
357
|
const nextSectionName = getNextSectionName();
|
|
326
358
|
if (!nextSectionName) {
|
|
327
359
|
settings.onWarning?.(`WARNING: ${section.filename} line ${section.line}: In section '${section.name}', there is a [[${text}]](>) link but no following section exists`);
|
|
@@ -339,10 +371,11 @@ export async function compile(settings) {
|
|
|
339
371
|
// any text - the name of the section
|
|
340
372
|
// closing bracket
|
|
341
373
|
const namedSectionLinkRegex = /\[\[([^\]]*?)\]\]\((.*?)\)/g;
|
|
342
|
-
let links = allMatchesForGroup(input, namedSectionLinkRegex, 2);
|
|
374
|
+
let links = allMatchesForGroup(input, namedSectionLinkRegex, 2).map(restore);
|
|
343
375
|
checkSectionLinks(links, section, passage);
|
|
344
376
|
input = input.replace(namedSectionLinkRegex, (_match, text /* $1 */, name /* $2 */) => {
|
|
345
|
-
|
|
377
|
+
text = restore(text);
|
|
378
|
+
const parsedName = getAdditionalLinkParameters(restore(name));
|
|
346
379
|
return `{{section "${escapeForHandlebarsString(parsedName.target)}" text="${escapeForHandlebarsString(text)}"${parsedName.additionalParameters}}}`;
|
|
347
380
|
});
|
|
348
381
|
// namedPassageLinkRegex matches:
|
|
@@ -354,10 +387,11 @@ export async function compile(settings) {
|
|
|
354
387
|
// any text - the name of the passage
|
|
355
388
|
// closing bracket
|
|
356
389
|
const namedPassageLinkRegex = /(?<!!)\[([^\]]*?)\]\(((?!https?:\/\/).*?)\)/g;
|
|
357
|
-
links = allMatchesForGroup(input, namedPassageLinkRegex, 2);
|
|
390
|
+
links = allMatchesForGroup(input, namedPassageLinkRegex, 2).map(restore);
|
|
358
391
|
checkPassageLinks(links, section, passage);
|
|
359
392
|
input = input.replace(namedPassageLinkRegex, (_match, text /* $1 */, name /* $2 */) => {
|
|
360
|
-
|
|
393
|
+
text = restore(text);
|
|
394
|
+
const parsedName = getAdditionalLinkParameters(restore(name));
|
|
361
395
|
return `{{passage "${escapeForHandlebarsString(parsedName.target)}" text="${escapeForHandlebarsString(text)}"${parsedName.additionalParameters}}}`;
|
|
362
396
|
});
|
|
363
397
|
// unnamedSectionLinkRegex matches:
|
|
@@ -365,9 +399,9 @@ export async function compile(settings) {
|
|
|
365
399
|
// any text - the link text
|
|
366
400
|
// closing ]]
|
|
367
401
|
const unnamedSectionLinkRegex = /\[\[(.*?)\]\]/g;
|
|
368
|
-
links = allMatchesForGroup(input, unnamedSectionLinkRegex, 1);
|
|
402
|
+
links = allMatchesForGroup(input, unnamedSectionLinkRegex, 1).map(restore);
|
|
369
403
|
checkSectionLinks(links, section, passage);
|
|
370
|
-
input = input.replace(unnamedSectionLinkRegex, (_match, name) => `{{section "${escapeForHandlebarsString(name)}"}}`);
|
|
404
|
+
input = input.replace(unnamedSectionLinkRegex, (_match, name) => `{{section "${escapeForHandlebarsString(restore(name))}"}}`);
|
|
371
405
|
// unnamedPassageLinkRegex matches:
|
|
372
406
|
// not preceded by ! (to avoid matching image links like ![alt])
|
|
373
407
|
// open [
|
|
@@ -375,9 +409,10 @@ export async function compile(settings) {
|
|
|
375
409
|
// closing ]
|
|
376
410
|
// no bracket after
|
|
377
411
|
const unnamedPassageLinkRegex = /(?<!!)\[(.*?)\]([^(]|$)/g;
|
|
378
|
-
links = allMatchesForGroup(input, unnamedPassageLinkRegex, 1);
|
|
412
|
+
links = allMatchesForGroup(input, unnamedPassageLinkRegex, 1).map(restore);
|
|
379
413
|
checkPassageLinks(links, section, passage);
|
|
380
|
-
input = input.replace(unnamedPassageLinkRegex, (_match, name, suffix) => `{{passage "${escapeForHandlebarsString(name)}"}}${suffix}`);
|
|
414
|
+
input = input.replace(unnamedPassageLinkRegex, (_match, name, suffix) => `{{passage "${escapeForHandlebarsString(restore(name))}"}}${suffix}`);
|
|
415
|
+
input = restore(input);
|
|
381
416
|
checkEmbeds(input, section, passage);
|
|
382
417
|
return input;
|
|
383
418
|
}
|
package/dist/v5.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export interface V5SyntaxMatch {
|
|
2
|
+
line: number;
|
|
3
|
+
text: string;
|
|
4
|
+
suggestion?: string;
|
|
5
|
+
}
|
|
6
|
+
export interface V5ConversionProblem {
|
|
7
|
+
line: number;
|
|
8
|
+
text: string;
|
|
9
|
+
reason: string;
|
|
10
|
+
}
|
|
11
|
+
export interface V5ConversionResult {
|
|
12
|
+
script: string;
|
|
13
|
+
converted: number;
|
|
14
|
+
problems: V5ConversionProblem[];
|
|
15
|
+
}
|
|
16
|
+
export declare function findV5Syntax(script: string): V5SyntaxMatch[];
|
|
17
|
+
export declare function convertV5Syntax(script: string): V5ConversionResult;
|
package/dist/v5.js
ADDED
|
@@ -0,0 +1,466 @@
|
|
|
1
|
+
// Detects Squiffy 5 text processor syntax (e.g. {if x:yes}{else:no}, {x}, {@x+=1}), which
|
|
2
|
+
// Squiffy 6 replaced with Handlebars, and converts it to the equivalent Handlebars syntax.
|
|
3
|
+
//
|
|
4
|
+
// Only story text is looked at: JavaScript blocks, @ui blocks, snippets, directives like @set,
|
|
5
|
+
// Handlebars expressions ({{...}}) and Markdown code spans are all skipped.
|
|
6
|
+
class ConversionError extends Error {
|
|
7
|
+
}
|
|
8
|
+
const sectionRegex = /^\[\[(.*)\]\]:$/;
|
|
9
|
+
const snippetRegex = /^\[snippet:\s*(.+)\]:$/;
|
|
10
|
+
const passageRegex = /^\[(.*)\]:$/;
|
|
11
|
+
const directiveRegex = /^@(title|import|start|set|unset|ui) |^@(inc|dec) \S+(?: \d+)?$|^@(clear|ui)$/;
|
|
12
|
+
const jsRegex = /^(\t| {4})/;
|
|
13
|
+
const identifierRegex = /^[A-Za-z_]\w*$/;
|
|
14
|
+
const numberRegex = /^-?\d+(\.\d+)?$/;
|
|
15
|
+
const reservedWords = new Set(["if", "else", "this", "true", "false", "null", "undefined", "and", "or", "not"]);
|
|
16
|
+
// Finds the parts of the script that the compiler treats as story text. This mirrors the
|
|
17
|
+
// line handling in compile(), in compiler.ts.
|
|
18
|
+
function findTextBlocks(script) {
|
|
19
|
+
const blocks = [];
|
|
20
|
+
const names = { sections: new Set(), passages: new Map() };
|
|
21
|
+
let section = null;
|
|
22
|
+
let textStarted = false;
|
|
23
|
+
let inUiBlock = false;
|
|
24
|
+
let inSnippet = false;
|
|
25
|
+
let block = null;
|
|
26
|
+
let offset = 0;
|
|
27
|
+
let autoSectionCount = 0;
|
|
28
|
+
const endBlock = () => {
|
|
29
|
+
if (block) {
|
|
30
|
+
blocks.push(block);
|
|
31
|
+
block = null;
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
for (const line of script.split("\n")) {
|
|
35
|
+
const lineStart = offset;
|
|
36
|
+
offset += line.length + 1;
|
|
37
|
+
const stripLine = line.trim();
|
|
38
|
+
let isText = false;
|
|
39
|
+
let match;
|
|
40
|
+
if (snippetRegex.test(stripLine)) {
|
|
41
|
+
inSnippet = true;
|
|
42
|
+
section = null;
|
|
43
|
+
textStarted = false;
|
|
44
|
+
}
|
|
45
|
+
else if ((match = sectionRegex.exec(stripLine))) {
|
|
46
|
+
inSnippet = false;
|
|
47
|
+
section = match[1];
|
|
48
|
+
names.sections.add(section);
|
|
49
|
+
textStarted = false;
|
|
50
|
+
}
|
|
51
|
+
else if (inSnippet) {
|
|
52
|
+
// snippets aren't compiled
|
|
53
|
+
}
|
|
54
|
+
else if ((match = passageRegex.exec(stripLine))) {
|
|
55
|
+
if (section !== null) {
|
|
56
|
+
if (!names.passages.has(section))
|
|
57
|
+
names.passages.set(section, new Set());
|
|
58
|
+
names.passages.get(section).add(match[1]);
|
|
59
|
+
}
|
|
60
|
+
textStarted = false;
|
|
61
|
+
}
|
|
62
|
+
else if (stripLine.startsWith("+++") || stripLine == "---") {
|
|
63
|
+
// These start a new automatically-named section
|
|
64
|
+
if (stripLine == "---")
|
|
65
|
+
inUiBlock = false;
|
|
66
|
+
autoSectionCount++;
|
|
67
|
+
section = `\uE000${autoSectionCount}`;
|
|
68
|
+
textStarted = false;
|
|
69
|
+
}
|
|
70
|
+
else if (directiveRegex.test(stripLine)) {
|
|
71
|
+
if (stripLine == "@ui")
|
|
72
|
+
inUiBlock = true;
|
|
73
|
+
}
|
|
74
|
+
else if (!textStarted && jsRegex.test(line)) {
|
|
75
|
+
// JavaScript
|
|
76
|
+
}
|
|
77
|
+
else if ((textStarted || stripLine.length > 0) && !inUiBlock) {
|
|
78
|
+
textStarted = true;
|
|
79
|
+
isText = true;
|
|
80
|
+
}
|
|
81
|
+
if (isText) {
|
|
82
|
+
if (block) {
|
|
83
|
+
block.end = lineStart + line.length;
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
block = { start: lineStart, end: lineStart + line.length, section };
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
endBlock();
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
endBlock();
|
|
94
|
+
return { blocks, names };
|
|
95
|
+
}
|
|
96
|
+
function findClose(text, open) {
|
|
97
|
+
let depth = 0;
|
|
98
|
+
for (let i = open; i < text.length; i++) {
|
|
99
|
+
if (text[i] == "{")
|
|
100
|
+
depth++;
|
|
101
|
+
else if (text[i] == "}") {
|
|
102
|
+
depth--;
|
|
103
|
+
if (depth == 0)
|
|
104
|
+
return i;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return -1;
|
|
108
|
+
}
|
|
109
|
+
function classify(inner, section, names) {
|
|
110
|
+
if (/^if\s/.test(inner) && inner.includes(":"))
|
|
111
|
+
return "if";
|
|
112
|
+
if (inner.startsWith("else:"))
|
|
113
|
+
return "else";
|
|
114
|
+
if (inner.startsWith("label:"))
|
|
115
|
+
return "label";
|
|
116
|
+
if (/^(rotate|sequence)[: ]/.test(inner))
|
|
117
|
+
return "rotate";
|
|
118
|
+
if (/^@\S/.test(inner))
|
|
119
|
+
return "set";
|
|
120
|
+
if (/^seen\s+\S/.test(inner))
|
|
121
|
+
return "seen";
|
|
122
|
+
if ((section !== null && names.passages.get(section)?.has(inner)) || names.sections.has(inner))
|
|
123
|
+
return "embed";
|
|
124
|
+
if (identifierRegex.test(inner) && !reservedWords.has(inner))
|
|
125
|
+
return "attribute";
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
// Finds the top-level v5 commands in some text. Handlebars expressions and Markdown code
|
|
129
|
+
// spans are skipped.
|
|
130
|
+
function findCommands(text, section, names) {
|
|
131
|
+
const commands = [];
|
|
132
|
+
let i = 0;
|
|
133
|
+
while (i < text.length) {
|
|
134
|
+
if (text.startsWith("{{", i)) {
|
|
135
|
+
const closeToken = text.startsWith("{{{", i) ? "}}}" : "}}";
|
|
136
|
+
const close = text.indexOf(closeToken, i + 2);
|
|
137
|
+
i = close == -1 ? i + 2 : close + closeToken.length;
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
if (text[i] == "`") {
|
|
141
|
+
const fence = /^`+/.exec(text.slice(i))[0];
|
|
142
|
+
const close = text.indexOf(fence, i + fence.length);
|
|
143
|
+
i = close == -1 ? i + fence.length : close + fence.length;
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
if (text[i] == "{") {
|
|
147
|
+
const close = findClose(text, i);
|
|
148
|
+
if (close != -1) {
|
|
149
|
+
const inner = text.slice(i + 1, close);
|
|
150
|
+
const kind = classify(inner, section, names);
|
|
151
|
+
if (kind) {
|
|
152
|
+
commands.push({ start: i, end: close + 1, inner, kind });
|
|
153
|
+
i = close + 1;
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
i++;
|
|
159
|
+
}
|
|
160
|
+
return commands;
|
|
161
|
+
}
|
|
162
|
+
function quote(s) {
|
|
163
|
+
return `"${s.replace(/\\/g, "\\\\").replace(/"/g, "\\\"")}"`;
|
|
164
|
+
}
|
|
165
|
+
function value(s) {
|
|
166
|
+
if (numberRegex.test(s))
|
|
167
|
+
return s;
|
|
168
|
+
return quote(s);
|
|
169
|
+
}
|
|
170
|
+
function attributeName(s, what) {
|
|
171
|
+
s = s.trim();
|
|
172
|
+
if (!identifierRegex.test(s) || reservedWords.has(s)) {
|
|
173
|
+
throw new ConversionError(`'${s}' isn't a valid ${what}`);
|
|
174
|
+
}
|
|
175
|
+
return s;
|
|
176
|
+
}
|
|
177
|
+
function convertCondition(condition) {
|
|
178
|
+
const comparison = /^([\w ]*)(=|<=|>=|<>|<|>)(.*)$/.exec(condition);
|
|
179
|
+
if (comparison) {
|
|
180
|
+
const lhs = attributeName(comparison[1], "attribute name");
|
|
181
|
+
const op = comparison[2] == "<>" ? "!=" : comparison[2];
|
|
182
|
+
const rhs = comparison[3].trim();
|
|
183
|
+
const rhsValue = rhs.startsWith("@") ? attributeName(rhs.substring(1), "attribute name") : value(rhs);
|
|
184
|
+
return `${lhs} ${op} ${rhsValue}`;
|
|
185
|
+
}
|
|
186
|
+
let prefix = "";
|
|
187
|
+
if (condition.startsWith("not ")) {
|
|
188
|
+
prefix = "not ";
|
|
189
|
+
condition = condition.substring(4);
|
|
190
|
+
}
|
|
191
|
+
if (condition.startsWith("seen ")) {
|
|
192
|
+
return `${prefix}(seen ${quote(condition.substring(5).trim())})`;
|
|
193
|
+
}
|
|
194
|
+
return prefix + attributeName(condition, "condition");
|
|
195
|
+
}
|
|
196
|
+
const replaceMessage = "@replace can't be converted automatically. In Squiffy 6, use {{#replace \"label\"}}...{{/replace}}";
|
|
197
|
+
function convertSetter(setter) {
|
|
198
|
+
if (setter.startsWith("@replace ")) {
|
|
199
|
+
throw new ConversionError(replaceMessage);
|
|
200
|
+
}
|
|
201
|
+
if (setter.includes(":=")) {
|
|
202
|
+
throw new ConversionError("':=' can't be converted automatically");
|
|
203
|
+
}
|
|
204
|
+
const set = /^(\w*)\s*=\s*(.*)$/.exec(setter);
|
|
205
|
+
if (set) {
|
|
206
|
+
const name = attributeName(set[1], "attribute name");
|
|
207
|
+
const rhs = set[2].trim();
|
|
208
|
+
const rhsValue = rhs.startsWith("@") ? attributeName(rhs.substring(1), "attribute name") : value(rhs);
|
|
209
|
+
return `{{set ${quote(name)} ${rhsValue}}}`;
|
|
210
|
+
}
|
|
211
|
+
const incDec = /^(\w*)\s*([+\-*/])=\s*(.*)$/.exec(setter);
|
|
212
|
+
if (incDec) {
|
|
213
|
+
const name = attributeName(incDec[1], "attribute name");
|
|
214
|
+
const op = incDec[2];
|
|
215
|
+
if (op == "*" || op == "/") {
|
|
216
|
+
throw new ConversionError(`'${op}=' has no equivalent helper in Squiffy 6`);
|
|
217
|
+
}
|
|
218
|
+
const rhs = incDec[3].trim();
|
|
219
|
+
let amount;
|
|
220
|
+
if (rhs.startsWith("@")) {
|
|
221
|
+
amount = attributeName(rhs.substring(1), "attribute name");
|
|
222
|
+
}
|
|
223
|
+
else if (numberRegex.test(rhs)) {
|
|
224
|
+
amount = rhs;
|
|
225
|
+
}
|
|
226
|
+
else {
|
|
227
|
+
throw new ConversionError(`'${rhs}' isn't a number`);
|
|
228
|
+
}
|
|
229
|
+
const helper = op == "+" ? "inc" : "dec";
|
|
230
|
+
return amount == "1" ? `{{${helper} ${quote(name)}}}` : `{{${helper} ${quote(name)} ${amount}}}`;
|
|
231
|
+
}
|
|
232
|
+
if (setter.startsWith("not ")) {
|
|
233
|
+
return `{{unset ${quote(attributeName(setter.substring(4), "attribute name"))}}}`;
|
|
234
|
+
}
|
|
235
|
+
return `{{set ${quote(attributeName(setter, "attribute name"))} true}}`;
|
|
236
|
+
}
|
|
237
|
+
function convertRotate(inner) {
|
|
238
|
+
const type = inner.startsWith("rotate") ? "rotate" : "sequence";
|
|
239
|
+
let options;
|
|
240
|
+
let attribute = "";
|
|
241
|
+
if (inner[type.length] == " ") {
|
|
242
|
+
const colon = inner.indexOf(":");
|
|
243
|
+
if (colon == -1) {
|
|
244
|
+
throw new ConversionError("there's no ':' before the options");
|
|
245
|
+
}
|
|
246
|
+
attribute = attributeName(inner.substring(type.length + 1, colon), "attribute name");
|
|
247
|
+
options = inner.substring(colon + 1);
|
|
248
|
+
}
|
|
249
|
+
else {
|
|
250
|
+
options = inner.substring(type.length + 1);
|
|
251
|
+
}
|
|
252
|
+
if (options.includes("{")) {
|
|
253
|
+
throw new ConversionError("the options contain text processor commands");
|
|
254
|
+
}
|
|
255
|
+
const items = options.split(":").map(quote).join(" ");
|
|
256
|
+
return `{{${type} (array ${items})${attribute ? ` set=${quote(attribute)}` : ""}}}`;
|
|
257
|
+
}
|
|
258
|
+
class Converter {
|
|
259
|
+
constructor(names, section) {
|
|
260
|
+
Object.defineProperty(this, "names", {
|
|
261
|
+
enumerable: true,
|
|
262
|
+
configurable: true,
|
|
263
|
+
writable: true,
|
|
264
|
+
value: names
|
|
265
|
+
});
|
|
266
|
+
Object.defineProperty(this, "section", {
|
|
267
|
+
enumerable: true,
|
|
268
|
+
configurable: true,
|
|
269
|
+
writable: true,
|
|
270
|
+
value: section
|
|
271
|
+
});
|
|
272
|
+
Object.defineProperty(this, "problems", {
|
|
273
|
+
enumerable: true,
|
|
274
|
+
configurable: true,
|
|
275
|
+
writable: true,
|
|
276
|
+
value: []
|
|
277
|
+
});
|
|
278
|
+
Object.defineProperty(this, "converted", {
|
|
279
|
+
enumerable: true,
|
|
280
|
+
configurable: true,
|
|
281
|
+
writable: true,
|
|
282
|
+
value: 0
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
// Converts the v5 commands in some text. base is the text's offset in the script,
|
|
286
|
+
// so that problems can be reported with the right line number.
|
|
287
|
+
convertText(text, base) {
|
|
288
|
+
const commands = findCommands(text, this.section, this.names);
|
|
289
|
+
let output = "";
|
|
290
|
+
let pos = 0;
|
|
291
|
+
for (let i = 0; i < commands.length; i++) {
|
|
292
|
+
const command = commands[i];
|
|
293
|
+
output += text.slice(pos, command.start);
|
|
294
|
+
pos = command.end;
|
|
295
|
+
const original = text.slice(command.start, command.end);
|
|
296
|
+
const report = (reason) => {
|
|
297
|
+
this.problems.push({ offset: base + command.start, text: original, reason });
|
|
298
|
+
output += original;
|
|
299
|
+
};
|
|
300
|
+
if (command.kind == "else") {
|
|
301
|
+
report("this {else:...} doesn't directly follow an {if ...:...}, so it's not clear which {if} it belongs to");
|
|
302
|
+
continue;
|
|
303
|
+
}
|
|
304
|
+
if (command.kind == "if") {
|
|
305
|
+
const next = commands[i + 1];
|
|
306
|
+
const between = next ? text.slice(command.end, next.start) : "";
|
|
307
|
+
const hasElse = next?.kind == "else" && /^\s*$/.test(between);
|
|
308
|
+
const colon = command.inner.indexOf(":");
|
|
309
|
+
const ifText = command.inner.substring(colon + 1);
|
|
310
|
+
if (hasElse && findCommands(ifText, this.section, this.names).some(c => c.kind == "if")) {
|
|
311
|
+
// In v5, an {else} pairs with the last {if} that ran, which could be this one or
|
|
312
|
+
// one nested inside it.
|
|
313
|
+
report("this {if} contains another {if} and is followed by an {else}, so it's not clear which {if} the {else} belongs to");
|
|
314
|
+
continue;
|
|
315
|
+
}
|
|
316
|
+
let condition;
|
|
317
|
+
try {
|
|
318
|
+
condition = convertCondition(command.inner.substring(3, colon).trim());
|
|
319
|
+
}
|
|
320
|
+
catch (e) {
|
|
321
|
+
if (!(e instanceof ConversionError))
|
|
322
|
+
throw e;
|
|
323
|
+
report(`the condition can't be converted: ${e.message}`);
|
|
324
|
+
continue;
|
|
325
|
+
}
|
|
326
|
+
const ifBase = base + command.start + 1 + colon + 1;
|
|
327
|
+
output += `{{#if ${condition}}}${this.convertText(ifText, ifBase)}`;
|
|
328
|
+
this.converted++;
|
|
329
|
+
if (hasElse) {
|
|
330
|
+
const elseText = next.inner.substring("else:".length);
|
|
331
|
+
const elseBase = base + next.start + 1 + "else:".length;
|
|
332
|
+
output += `${between}{{else}}${this.convertText(elseText, elseBase)}`;
|
|
333
|
+
this.converted++;
|
|
334
|
+
pos = next.end;
|
|
335
|
+
i++;
|
|
336
|
+
}
|
|
337
|
+
output += "{{/if}}";
|
|
338
|
+
continue;
|
|
339
|
+
}
|
|
340
|
+
if (command.kind == "label") {
|
|
341
|
+
const eq = command.inner.indexOf("=");
|
|
342
|
+
if (eq == -1) {
|
|
343
|
+
report("there's no '=' after the label name");
|
|
344
|
+
continue;
|
|
345
|
+
}
|
|
346
|
+
const name = command.inner.substring("label:".length, eq).trim();
|
|
347
|
+
if (!/^\w+$/.test(name)) {
|
|
348
|
+
report(`'${name}' isn't a valid label name`);
|
|
349
|
+
continue;
|
|
350
|
+
}
|
|
351
|
+
const labelText = command.inner.substring(eq + 1);
|
|
352
|
+
const labelBase = base + command.start + 1 + eq + 1;
|
|
353
|
+
output += `{{#label ${quote(name)}}}${this.convertText(labelText, labelBase)}{{/label}}`;
|
|
354
|
+
this.converted++;
|
|
355
|
+
continue;
|
|
356
|
+
}
|
|
357
|
+
if (command.kind == "seen") {
|
|
358
|
+
report("{seen ...} only works as an {if} condition. In Squiffy 6, use {{#if (seen \"name\")}}...{{/if}}");
|
|
359
|
+
continue;
|
|
360
|
+
}
|
|
361
|
+
let result;
|
|
362
|
+
try {
|
|
363
|
+
switch (command.kind) {
|
|
364
|
+
case "rotate":
|
|
365
|
+
result = convertRotate(command.inner);
|
|
366
|
+
break;
|
|
367
|
+
case "set":
|
|
368
|
+
if (command.inner.startsWith("@replace ")) {
|
|
369
|
+
throw new ConversionError(replaceMessage);
|
|
370
|
+
}
|
|
371
|
+
result = command.inner.substring(1).split(",").map(s => convertSetter(s.trim())).join("");
|
|
372
|
+
break;
|
|
373
|
+
case "embed":
|
|
374
|
+
result = `{{embed ${quote(command.inner)}}}`;
|
|
375
|
+
break;
|
|
376
|
+
case "attribute":
|
|
377
|
+
result = `{{${command.inner}}}`;
|
|
378
|
+
break;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
catch (e) {
|
|
382
|
+
if (!(e instanceof ConversionError))
|
|
383
|
+
throw e;
|
|
384
|
+
report(e.message);
|
|
385
|
+
continue;
|
|
386
|
+
}
|
|
387
|
+
output += result;
|
|
388
|
+
this.converted++;
|
|
389
|
+
}
|
|
390
|
+
output += text.slice(pos);
|
|
391
|
+
return output;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
// Returns a function that converts an offset in the script to a 1-based line number
|
|
395
|
+
function lineFinder(script) {
|
|
396
|
+
const lineStarts = [0];
|
|
397
|
+
for (let i = 0; i < script.length; i++) {
|
|
398
|
+
if (script[i] == "\n")
|
|
399
|
+
lineStarts.push(i + 1);
|
|
400
|
+
}
|
|
401
|
+
return (offset) => {
|
|
402
|
+
let low = 0;
|
|
403
|
+
let high = lineStarts.length - 1;
|
|
404
|
+
while (low < high) {
|
|
405
|
+
const mid = Math.ceil((low + high) / 2);
|
|
406
|
+
if (lineStarts[mid] <= offset)
|
|
407
|
+
low = mid;
|
|
408
|
+
else
|
|
409
|
+
high = mid - 1;
|
|
410
|
+
}
|
|
411
|
+
return low + 1;
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
function normaliseLineEndings(script) {
|
|
415
|
+
return script.replace(/\r/g, "");
|
|
416
|
+
}
|
|
417
|
+
// Finds Squiffy 5 text processor commands in a script. Nested commands aren't listed
|
|
418
|
+
// separately from the command that contains them.
|
|
419
|
+
export function findV5Syntax(script) {
|
|
420
|
+
script = normaliseLineEndings(script);
|
|
421
|
+
const { blocks, names } = findTextBlocks(script);
|
|
422
|
+
const lineAt = lineFinder(script);
|
|
423
|
+
const result = [];
|
|
424
|
+
for (const block of blocks) {
|
|
425
|
+
const text = script.slice(block.start, block.end);
|
|
426
|
+
for (const command of findCommands(text, block.section, names)) {
|
|
427
|
+
const original = text.slice(command.start, command.end);
|
|
428
|
+
const match = { line: lineAt(block.start + command.start), text: original };
|
|
429
|
+
if (command.kind != "else") {
|
|
430
|
+
const converter = new Converter(names, block.section);
|
|
431
|
+
const converted = converter.convertText(original, 0);
|
|
432
|
+
if (converter.problems.length == 0) {
|
|
433
|
+
match.suggestion = converted;
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
result.push(match);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
return result;
|
|
440
|
+
}
|
|
441
|
+
// Converts Squiffy 5 text processor commands in a script to Squiffy 6 Handlebars syntax.
|
|
442
|
+
// Anything that can't be converted reliably is left as-is and listed in the problems.
|
|
443
|
+
export function convertV5Syntax(script) {
|
|
444
|
+
const crlf = script.includes("\r\n");
|
|
445
|
+
script = normaliseLineEndings(script);
|
|
446
|
+
const { blocks, names } = findTextBlocks(script);
|
|
447
|
+
const lineAt = lineFinder(script);
|
|
448
|
+
let output = "";
|
|
449
|
+
let pos = 0;
|
|
450
|
+
let converted = 0;
|
|
451
|
+
const problems = [];
|
|
452
|
+
for (const block of blocks) {
|
|
453
|
+
const converter = new Converter(names, block.section);
|
|
454
|
+
output += script.slice(pos, block.start);
|
|
455
|
+
output += converter.convertText(script.slice(block.start, block.end), block.start);
|
|
456
|
+
pos = block.end;
|
|
457
|
+
converted += converter.converted;
|
|
458
|
+
problems.push(...converter.problems.map(p => ({ line: lineAt(p.offset), text: p.text, reason: p.reason })));
|
|
459
|
+
}
|
|
460
|
+
output += script.slice(pos);
|
|
461
|
+
return {
|
|
462
|
+
script: crlf ? output.replace(/\n/g, "\r\n") : output,
|
|
463
|
+
converted,
|
|
464
|
+
problems,
|
|
465
|
+
};
|
|
466
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "squiffy-compiler",
|
|
3
|
-
"version": "6.0.0
|
|
3
|
+
"version": "6.0.0",
|
|
4
4
|
"description": "A tool for creating multiple-choice interactive stories",
|
|
5
5
|
"author": "Alex Warren",
|
|
6
6
|
"contributors": [
|
|
@@ -32,5 +32,5 @@
|
|
|
32
32
|
"dist"
|
|
33
33
|
],
|
|
34
34
|
"type": "module",
|
|
35
|
-
"gitHead": "
|
|
35
|
+
"gitHead": "d8ff25924d83c5afca7483776d106799feadeace"
|
|
36
36
|
}
|