-
Notifications
You must be signed in to change notification settings - Fork 121
/
processSnippetJson.js
executable file
·265 lines (233 loc) · 8.04 KB
/
processSnippetJson.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import lzString from "lz-string";
import {
missingRequireWarning,
missingExampleWarning,
repeatedNameWarning
} from "./warnings.js";
import { chapterIndex } from "../parseXmlJson";
import recursiveProcessPureText from "./recursiveProcessPureText";
import { processRuneModule } from "./processModuleImports.js";
const snippetStore = {};
export const setupSnippetsJson = node => {
const snippets = node.getElementsByTagName("SNIPPET");
for (let i = 0; snippets[i]; i++) {
const snippet = snippets[i];
const jsSnippet = snippet.getElementsByTagName("JAVASCRIPT")[0];
let jsRunSnippet = snippet.getElementsByTagName("JAVASCRIPT_RUN")[0];
if (!jsRunSnippet) {
jsRunSnippet = jsSnippet;
}
const snippetName = snippet.getElementsByTagName("NAME")[0];
if (snippetName && jsSnippet) {
const nameStr = snippetName.firstChild.nodeValue;
if (snippetStore[nameStr]) {
repeatedNameWarning(nameStr);
return;
}
const codeArr = [];
recursiveProcessPureText(jsRunSnippet.firstChild, codeArr);
const codeStr = codeArr.join("").trim();
const requirements = snippet.getElementsByTagName("REQUIRES");
const requireNames = [];
for (let i = 0; requirements[i]; i++) {
requireNames.push(requirements[i].firstChild.nodeValue);
}
snippetStore[nameStr] = { codeStr, requireNames };
}
}
};
const recursiveGetRequires = (name, seen) => {
// console.log("in recursiveGetRequires: " + name);
if (seen.has(name)) return;
const snippetEntry = snippetStore[name];
if (!snippetEntry) {
missingRequireWarning(name);
return;
}
for (const requirement of snippetEntry.requireNames) {
recursiveGetRequires(requirement, seen);
}
seen.add(name);
};
const addToSnippet = (tag, value, snippet) => {
snippet[tag] = value;
};
export const recursivelyProcessTextSnippetJson = (node, writeTo) => {
if (!node) return;
const name = node.nodeName;
if (name === "#text") {
if (!node.nodeValue.match(/&(\w|\.|\d)+;/)) {
let s = node.nodeValue;
writeTo.push(s);
}
} else if (name === "META") {
writeTo.push("$");
let s = node.firstChild.nodeValue;
s = s.replace(/-/g, "$-$").replace(/ /g, "\\ ");
writeTo.push(s);
writeTo.push("$");
} else if (name === "METAPHRASE") {
writeTo.push("$\\langle{}$");
recursivelyProcessTextSnippetJson(node.firstChild, writeTo);
writeTo.push("$\\rangle$");
} else if (name === "JAVASCRIPTINLINE") {
recursivelyProcessTextSnippetJson(node.firstChild, writeTo);
} else if (name === "#comment" || name === "ALLOW_BREAK") {
return;
} else if (name === "SHORT_SPACE" || name === "SHORT_SPACE_AND_ALLOW_BREAK") {
writeTo.push("\n");
} else {
console.log(`processSnippetJson: UNRECOGNISED TAG ${name}\n\n`);
}
return recursivelyProcessTextSnippetJson(node.nextSibling, writeTo);
};
export const processSnippetJson = (node, snippet) => {
if (node.getAttribute("HIDE") == "yes") {
return;
}
addToSnippet("eval", false, snippet);
const jsPromptSnippet = node.getElementsByTagName("JAVASCRIPT_PROMPT")[0];
if (jsPromptSnippet) {
addToSnippet("body", jsPromptSnippet.firstChild.nodeValue.trim(), snippet);
}
const jsSnippet = node.getElementsByTagName("JAVASCRIPT")[0];
const jsOutputSnippet = node.getElementsByTagName("JAVASCRIPT_OUTPUT")[0];
if (jsSnippet) {
// JavaScript source for running. Overrides JAVASCRIPT if present.
let jsRunSnippet = node.getElementsByTagName("JAVASCRIPT_RUN")[0];
if (!jsRunSnippet) {
jsRunSnippet = jsSnippet;
}
const codeArr = [];
recursiveProcessPureText(jsSnippet.firstChild, codeArr);
let codeStr = codeArr.join("").trim();
// Remove newline from beginning and end
codeStr = codeStr.replace(/^[\r\n]+/g, "");
codeStr = codeStr.replace(/[\r\n\s]+$/g, "");
const codeArr_run = [];
if (jsRunSnippet) {
recursiveProcessPureText(jsRunSnippet.firstChild, codeArr_run);
}
const codeStr_run = codeArr_run.join("").trim();
if (node.getAttribute("EVAL") === "no") {
addToSnippet("body", codeStr, snippet);
} else {
addToSnippet("eval", true, snippet);
let reqStr = "";
let reqArr = [];
const snippetName = node.getElementsByTagName("NAME")[0];
let nameStr;
if (snippetName) {
nameStr = snippetName.firstChild.nodeValue;
const reqSet = new Set();
recursiveGetRequires(nameStr, reqSet);
const examples = node.getElementsByTagName("EXAMPLE");
for (let i = 0; examples[i]; i++) {
const exampleString = examples[i].firstChild.nodeValue;
const exampleNode = snippetStore[exampleString];
if (exampleNode) {
const exampleRequires = exampleNode.requireNames;
for (let j = 0; exampleRequires[j]; j++) {
recursiveGetRequires(exampleRequires[j], reqSet);
}
}
}
for (const reqName of reqSet) {
const snippetEntry = snippetStore[reqName];
if (snippetEntry && reqName !== nameStr) {
if (snippetEntry.codeStr) {
reqArr.push("\n\n");
reqArr.push(snippetEntry.codeStr);
reqStr = reqArr.join("");
}
}
}
reqStr = reqArr.join("");
} else {
const requirements = node.getElementsByTagName("REQUIRES");
for (let i = 0; requirements[i]; i++) {
const required = requirements[i].firstChild.nodeValue;
if (snippetStore[required]) {
if (snippetStore[required].codeStr) {
reqArr.push("\n\n");
reqArr.push(snippetStore[required].codeStr);
reqStr = reqArr.join("");
}
} else {
missingRequireWarning(required);
}
}
reqStr = reqArr.join("");
}
const examples = node.getElementsByTagName("EXAMPLE");
const exampleArr = [];
for (let i = 0; examples[i]; i++) {
const example = examples[i].firstChild.nodeValue;
if (snippetStore[example]) {
if (snippetStore[example].codeStr) {
exampleArr.push("\n\n");
exampleArr.push(snippetStore[example].codeStr);
reqStr = reqArr.join("");
}
} else {
missingExampleWarning(example);
}
}
const exampleStr = exampleArr.join("");
const current_chap = chapterIndex.substring(0, 1);
const explicit_chap = node.getAttribute("CHAP");
const implicit_chap = explicit_chap ? explicit_chap : current_chap;
const chap = implicit_chap === "5" ? "4" : implicit_chap;
let variant = node.getAttribute("VARIANT");
let ext = node.getAttribute("EXT");
let importStatement = "";
if (variant) {
variant = "&variant=" + variant;
} else {
variant = "";
}
if (ext === "RUNES") {
importStatement =
"\n\n" + processRuneModule(reqStr + " " + codeStr_run + exampleStr);
}
const compressed = lzString.compressToEncodedURIComponent(
"// SICP JS " +
chapterIndex +
importStatement +
reqStr +
"\n\n" +
codeStr_run +
exampleStr
);
const makeHash = program =>
"chap=" + chap + variant + "&prgrm=" + program;
if (reqStr) {
let prependLength = reqStr.split("\n").length;
if (importStatement) {
prependLength += 2;
}
addToSnippet("prependLength", prependLength, snippet);
} else {
addToSnippet("prependLength", 0, snippet);
}
addToSnippet("program", makeHash(compressed), snippet);
const chunks = (codeStr + "\n").match(
/^((?:.*?[\r\n]+){1,6})((?:.|\n|\r)*)$/
);
let body = "";
body += chunks[1];
if (chunks[2]) {
body += chunks[2];
}
addToSnippet("body", body.trim() + " ", snippet);
}
}
if (jsOutputSnippet) {
addToSnippet(
"output",
jsOutputSnippet.firstChild.nodeValue.trim(),
snippet
);
}
};
export default processSnippetJson;