squiffy-compiler 7.0.0-beta.3 → 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.d.ts +2 -0
- package/dist/analyse.js +43 -1
- package/dist/compiler.d.ts +7 -0
- package/dist/compiler.js +3 -0
- package/dist/emit.d.ts +1 -0
- package/dist/emit.js +6 -0
- package/dist/language-service.js +39 -3
- package/dist/node.d.ts +2 -0
- package/dist/node.js +6 -2
- package/dist/parse.js +13 -4
- package/dist/project.d.ts +2 -0
- package/dist/project.js +14 -2
- package/dist/setters.d.ts +40 -0
- package/dist/setters.js +411 -0
- package/dist/story-graph.d.ts +2 -1
- package/dist/story-graph.js +10 -3
- package/dist/syntax.d.ts +9 -1
- package/dist/text.d.ts +4 -1
- package/dist/text.js +56 -32
- package/dist/twine/convert.d.ts +75 -0
- package/dist/twine/convert.js +626 -0
- package/dist/twine/expression.d.ts +62 -0
- package/dist/twine/expression.js +201 -0
- package/dist/twine/harlowe.d.ts +17 -0
- package/dist/twine/harlowe.js +1856 -0
- package/dist/twine/story.d.ts +24 -0
- package/dist/twine/story.js +231 -0
- package/dist/twine/sugarcube.d.ts +33 -0
- package/dist/twine/sugarcube.js +1584 -0
- package/package.json +2 -2
- package/syntaxes/squiffy.tmLanguage.json +33 -7
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export interface TwinePassage {
|
|
2
|
+
name: string;
|
|
3
|
+
tags: string[];
|
|
4
|
+
text: string;
|
|
5
|
+
position?: [number, number];
|
|
6
|
+
size?: [number, number];
|
|
7
|
+
}
|
|
8
|
+
export type TwineFormat = "harlowe" | "sugarcube" | "other";
|
|
9
|
+
export interface TwineStory {
|
|
10
|
+
title: string;
|
|
11
|
+
formatName: string;
|
|
12
|
+
formatVersion: string;
|
|
13
|
+
format: TwineFormat;
|
|
14
|
+
start?: string;
|
|
15
|
+
passages: TwinePassage[];
|
|
16
|
+
stylesheet: string;
|
|
17
|
+
script: string;
|
|
18
|
+
}
|
|
19
|
+
export declare class TwineParseError extends Error {
|
|
20
|
+
}
|
|
21
|
+
export declare function formatOf(name: string): TwineFormat;
|
|
22
|
+
export declare function isTwineSource(text: string): boolean;
|
|
23
|
+
export declare function parseTwine(text: string): TwineStory;
|
|
24
|
+
export declare function decodeEntities(s: string): string;
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
// Reads a Twine story from Twee (Twee 3, and the older Twee 1/2 that Twine 1 and Tweego read)
|
|
2
|
+
// or from Twine's HTML (Twine 2's published or archive HTML, and Twine 1's), into one shape for
|
|
3
|
+
// the converters.
|
|
4
|
+
export class TwineParseError extends Error {
|
|
5
|
+
}
|
|
6
|
+
export function formatOf(name) {
|
|
7
|
+
const lower = name.toLowerCase();
|
|
8
|
+
if (lower.startsWith("harlowe"))
|
|
9
|
+
return "harlowe";
|
|
10
|
+
// Twine 1's formats (Sugarcane, Jonah) have <<macros>> like SugarCube's, which grew out of them
|
|
11
|
+
if (lower.startsWith("sugarcube") || lower == "sugarcane" || lower == "jonah" || lower == "")
|
|
12
|
+
return "sugarcube";
|
|
13
|
+
return "other";
|
|
14
|
+
}
|
|
15
|
+
// Whether some text looks like a Twine story, rather than any other HTML or text file
|
|
16
|
+
export function isTwineSource(text) {
|
|
17
|
+
return /<tw-storydata[\s>]/i.test(text) || /<div[^>]*\bid=["']storeArea["']/i.test(text) || /^::\s*\S/m.test(text);
|
|
18
|
+
}
|
|
19
|
+
export function parseTwine(text) {
|
|
20
|
+
if (/<tw-storydata[\s>]/i.test(text))
|
|
21
|
+
return parseTwine2Html(text);
|
|
22
|
+
if (/<div[^>]*\bid=["']storeArea["']/i.test(text))
|
|
23
|
+
return parseTwine1Html(text);
|
|
24
|
+
if (/^::/m.test(text))
|
|
25
|
+
return parseTwee(text);
|
|
26
|
+
throw new TwineParseError("This doesn't look like a Twine story: there's no story data in it.");
|
|
27
|
+
}
|
|
28
|
+
const entities = { amp: "&", lt: "<", gt: ">", quot: "\"", apos: "'", nbsp: " " };
|
|
29
|
+
export function decodeEntities(s) {
|
|
30
|
+
return s.replace(/&(#x[0-9a-f]+|#\d+|\w+);/gi, (match, entity) => {
|
|
31
|
+
if (entity[0] == "#") {
|
|
32
|
+
const code = entity[1] == "x" || entity[1] == "X" ? parseInt(entity.substring(2), 16) : parseInt(entity.substring(1), 10);
|
|
33
|
+
return Number.isFinite(code) ? String.fromCodePoint(code) : match;
|
|
34
|
+
}
|
|
35
|
+
return entities[entity.toLowerCase()] ?? match;
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
function attributes(tag) {
|
|
39
|
+
const result = {};
|
|
40
|
+
const regex = /([\w-]+)\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+))/g;
|
|
41
|
+
let match;
|
|
42
|
+
while ((match = regex.exec(tag))) {
|
|
43
|
+
result[match[1].toLowerCase()] = decodeEntities(match[2] ?? match[3] ?? match[4] ?? "");
|
|
44
|
+
}
|
|
45
|
+
return result;
|
|
46
|
+
}
|
|
47
|
+
// "x,y" or "width,height", as Twine writes them
|
|
48
|
+
function pair(value) {
|
|
49
|
+
if (typeof value != "string")
|
|
50
|
+
return undefined;
|
|
51
|
+
const parts = value.split(",").map(n => Number(n.trim()));
|
|
52
|
+
return parts.length == 2 && parts.every(n => Number.isFinite(n)) ? [parts[0], parts[1]] : undefined;
|
|
53
|
+
}
|
|
54
|
+
// The passage's place on Twine's map, if it has one
|
|
55
|
+
function place(position, size) {
|
|
56
|
+
const p = pair(position);
|
|
57
|
+
if (!p)
|
|
58
|
+
return {};
|
|
59
|
+
const s = pair(size);
|
|
60
|
+
return s && s[0] > 0 && s[1] > 0 ? { position: p, size: s } : { position: p };
|
|
61
|
+
}
|
|
62
|
+
function splitTags(tags) {
|
|
63
|
+
return (tags ?? "").split(/\s+/).filter(t => t.length > 0);
|
|
64
|
+
}
|
|
65
|
+
// Separates the passages that hold the story's CSS and JavaScript, as Twine 1 and Twee do with
|
|
66
|
+
// tags, from the passages of the story itself.
|
|
67
|
+
function splitSpecial(passages, stylesheet, script) {
|
|
68
|
+
return passages.filter(p => {
|
|
69
|
+
if (p.tags.includes("stylesheet")) {
|
|
70
|
+
stylesheet.push(p.text);
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
if (p.tags.includes("script")) {
|
|
74
|
+
script.push(p.text);
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
return !p.tags.includes("Twine.private") && !p.tags.includes("annotation");
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
function parseTwine2Html(html) {
|
|
81
|
+
const storyTag = /<tw-storydata\b[^>]*>/i.exec(html);
|
|
82
|
+
const story = attributes(storyTag[0]);
|
|
83
|
+
const body = html.substring(storyTag.index + storyTag[0].length);
|
|
84
|
+
const end = body.search(/<\/tw-storydata>/i);
|
|
85
|
+
const data = end == -1 ? body : body.substring(0, end);
|
|
86
|
+
const stylesheet = [];
|
|
87
|
+
const script = [];
|
|
88
|
+
for (const match of data.matchAll(/<style\b([^>]*)>([\s\S]*?)<\/style>/gi)) {
|
|
89
|
+
if (/twine-user-stylesheet|text\/twine-css/i.test(match[1]))
|
|
90
|
+
stylesheet.push(decodeEntities(match[2]));
|
|
91
|
+
}
|
|
92
|
+
for (const match of data.matchAll(/<script\b([^>]*)>([\s\S]*?)<\/script>/gi)) {
|
|
93
|
+
if (/twine-user-script|text\/twine-javascript/i.test(match[1]))
|
|
94
|
+
script.push(decodeEntities(match[2]));
|
|
95
|
+
}
|
|
96
|
+
let start;
|
|
97
|
+
const passages = [];
|
|
98
|
+
for (const match of data.matchAll(/<tw-passagedata\b([^>]*)>([\s\S]*?)<\/tw-passagedata>/gi)) {
|
|
99
|
+
const a = attributes(match[1]);
|
|
100
|
+
const passage = { name: a.name ?? "", tags: splitTags(a.tags), text: decodeEntities(match[2]), ...place(a.position, a.size) };
|
|
101
|
+
if (a.pid !== undefined && a.pid == story.startnode)
|
|
102
|
+
start = passage.name;
|
|
103
|
+
passages.push(passage);
|
|
104
|
+
}
|
|
105
|
+
const formatName = story.format ?? "";
|
|
106
|
+
return {
|
|
107
|
+
title: story.name ?? "",
|
|
108
|
+
formatName,
|
|
109
|
+
formatVersion: story["format-version"] ?? "",
|
|
110
|
+
format: formatOf(formatName),
|
|
111
|
+
start,
|
|
112
|
+
passages: splitSpecial(passages, stylesheet, script),
|
|
113
|
+
stylesheet: stylesheet.join("\n\n").trim(),
|
|
114
|
+
script: script.join("\n\n").trim(),
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
function parseTwine1Html(html) {
|
|
118
|
+
const start = html.search(/<div[^>]*\bid=["']storeArea["']/i);
|
|
119
|
+
const data = html.substring(start);
|
|
120
|
+
const passages = [];
|
|
121
|
+
for (const match of data.matchAll(/<div\b([^>]*\btiddler=[^>]*)>([^<]*)<\/div>/gi)) {
|
|
122
|
+
const a = attributes(match[1]);
|
|
123
|
+
// Twine 1 escapes newlines and backslashes in passage text
|
|
124
|
+
const text = decodeEntities(match[2]).replace(/\\(.)/g, (_m, c) => c == "n" ? "\n" : c == "t" ? "\t" : c == "s" ? " " : c);
|
|
125
|
+
passages.push({ name: a.tiddler ?? "", tags: splitTags(a.tags), text, ...place(a["twine-position"], undefined) });
|
|
126
|
+
}
|
|
127
|
+
return fromTwee1Passages(passages);
|
|
128
|
+
}
|
|
129
|
+
// Twine 1 and Twee 1/2 keep the title and settings in passages
|
|
130
|
+
function fromTwee1Passages(all) {
|
|
131
|
+
const stylesheet = [];
|
|
132
|
+
const script = [];
|
|
133
|
+
const special = new Map();
|
|
134
|
+
const passages = splitSpecial(all, stylesheet, script).filter(p => {
|
|
135
|
+
if (["StoryTitle", "StorySettings", "StoryIncludes", "StoryData"].includes(p.name)) {
|
|
136
|
+
special.set(p.name, p.text);
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
return true;
|
|
140
|
+
});
|
|
141
|
+
let formatName = "";
|
|
142
|
+
let formatVersion = "";
|
|
143
|
+
let start;
|
|
144
|
+
const storyData = special.get("StoryData");
|
|
145
|
+
if (storyData) {
|
|
146
|
+
try {
|
|
147
|
+
const data = JSON.parse(storyData);
|
|
148
|
+
formatName = data.format ?? "";
|
|
149
|
+
formatVersion = data["format-version"] ?? "";
|
|
150
|
+
start = data.start;
|
|
151
|
+
}
|
|
152
|
+
catch {
|
|
153
|
+
// Treated as if there were no StoryData
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
if (!start && passages.some(p => p.name == "Start"))
|
|
157
|
+
start = "Start";
|
|
158
|
+
return {
|
|
159
|
+
title: (special.get("StoryTitle") ?? "").trim(),
|
|
160
|
+
formatName,
|
|
161
|
+
formatVersion,
|
|
162
|
+
format: formatOf(formatName),
|
|
163
|
+
start,
|
|
164
|
+
passages,
|
|
165
|
+
stylesheet: stylesheet.join("\n\n").trim(),
|
|
166
|
+
script: script.join("\n\n").trim(),
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
// Twee: each passage starts with a line ":: Name [tags] {metadata}". In names, \ escapes
|
|
170
|
+
// [ ] { } and \ itself. A line in a passage that would start with :: is written \::.
|
|
171
|
+
function parseTwee(text) {
|
|
172
|
+
const lines = text.replace(/\r\n?/g, "\n").split("\n");
|
|
173
|
+
const passages = [];
|
|
174
|
+
let current = null;
|
|
175
|
+
const finish = () => {
|
|
176
|
+
if (!current)
|
|
177
|
+
return;
|
|
178
|
+
// Twee separates passages with blank lines, which aren't part of the passage
|
|
179
|
+
while (current.lines.length && current.lines[current.lines.length - 1].trim() == "")
|
|
180
|
+
current.lines.pop();
|
|
181
|
+
const { lines, ...header } = current;
|
|
182
|
+
passages.push({ ...header, text: lines.join("\n") });
|
|
183
|
+
};
|
|
184
|
+
for (const line of lines) {
|
|
185
|
+
if (line.startsWith("::")) {
|
|
186
|
+
finish();
|
|
187
|
+
const header = parseTweeHeader(line.substring(2));
|
|
188
|
+
current = { ...header, lines: [] };
|
|
189
|
+
}
|
|
190
|
+
else if (current) {
|
|
191
|
+
current.lines.push(line.startsWith("\\::") ? line.substring(1) : line);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
finish();
|
|
195
|
+
return fromTwee1Passages(passages);
|
|
196
|
+
}
|
|
197
|
+
// Twee 3 puts the passage's place on the map in its metadata, e.g. {"position":"600,400","size":"100,200"}
|
|
198
|
+
function parseTweeHeader(header) {
|
|
199
|
+
let name = "";
|
|
200
|
+
let i = 0;
|
|
201
|
+
const s = header.trimStart();
|
|
202
|
+
for (; i < s.length; i++) {
|
|
203
|
+
const c = s[i];
|
|
204
|
+
if (c == "\\" && i + 1 < s.length) {
|
|
205
|
+
name += s[++i];
|
|
206
|
+
}
|
|
207
|
+
else if (c == "[" || c == "{") {
|
|
208
|
+
break;
|
|
209
|
+
}
|
|
210
|
+
else {
|
|
211
|
+
name += c;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
let tags = [];
|
|
215
|
+
const rest = s.substring(i);
|
|
216
|
+
const tagMatch = /^\[((?:\\.|[^\]])*)\]/.exec(rest);
|
|
217
|
+
if (tagMatch)
|
|
218
|
+
tags = splitTags(tagMatch[1].replace(/\\(.)/g, "$1"));
|
|
219
|
+
const metadata = rest.substring(tagMatch?.[0].length ?? 0).trim();
|
|
220
|
+
let placed = {};
|
|
221
|
+
if (metadata.startsWith("{")) {
|
|
222
|
+
try {
|
|
223
|
+
const data = JSON.parse(metadata);
|
|
224
|
+
placed = place(data?.position, data?.size);
|
|
225
|
+
}
|
|
226
|
+
catch {
|
|
227
|
+
// Metadata that isn't JSON is left out
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
return { name: name.trim(), tags, ...placed };
|
|
231
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { Expr } from "./expression.js";
|
|
2
|
+
import type { ConvertedPassage, PassageContext } from "./convert.js";
|
|
3
|
+
export declare class SugarCubeExpressions {
|
|
4
|
+
readonly ctx: PassageContext;
|
|
5
|
+
constructor(ctx: PassageContext);
|
|
6
|
+
attribute(name: string, temp: boolean): string;
|
|
7
|
+
parse(source: string): Expr;
|
|
8
|
+
statements(source: string): Statement[];
|
|
9
|
+
passageName(source: string): {
|
|
10
|
+
name: string;
|
|
11
|
+
} | {
|
|
12
|
+
expr: Expr;
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
export type Statement = {
|
|
16
|
+
kind: "set" | "inc" | "dec";
|
|
17
|
+
name: string;
|
|
18
|
+
value: Expr;
|
|
19
|
+
op?: "*=" | "/=";
|
|
20
|
+
} | {
|
|
21
|
+
kind: "append" | "remove";
|
|
22
|
+
name: string;
|
|
23
|
+
value: Expr;
|
|
24
|
+
} | {
|
|
25
|
+
kind: "unset";
|
|
26
|
+
name: string;
|
|
27
|
+
};
|
|
28
|
+
export declare function sugarCubeWidgets(story: PassageContext["story"]): Map<string, string>;
|
|
29
|
+
export declare function convertSugarCubeWidgets(text: string, ctx: PassageContext): {
|
|
30
|
+
section: string;
|
|
31
|
+
converted: ConvertedPassage;
|
|
32
|
+
}[];
|
|
33
|
+
export declare function convertSugarCubePassage(text: string, ctx: PassageContext): ConvertedPassage;
|