forked from source-academy/sicp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessSnippetHtml.js
executable file
·236 lines (211 loc) · 7.43 KB
/
processSnippetHtml.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
import { sourceAcademyURL } from "../constants";
import lzString from "lz-string";
import {
missingRequireWarning,
missingExampleWarning,
repeatedNameWarning
} from "./warnings.js";
import { chapterIndex } from "../parseXmlHtml";
import recursiveProcessPureText from "./recursiveProcessPureText";
import { processRuneModule } from "./processModuleImports.js";
const snippetStore = {};
export const setupSnippetsHtml = 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++) {
// console.log("in setupSnippetsHtml: name: " + nameStr);
// console.log("in setupSnippetsHtml: " + requirements[i].firstChild.nodeValue);
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);
};
export const processSnippetHtml = (node, writeTo, split) => {
if (node.getAttribute("HIDE") == "yes") {
return;
}
const jsPromptSnippet = node.getElementsByTagName("JAVASCRIPT_PROMPT")[0];
if (jsPromptSnippet) {
writeTo.push("<pre class='prettyprintoutput'>");
writeTo.push(jsPromptSnippet.firstChild.nodeValue.trimRight());
writeTo.push("</pre>");
}
const jsLonelySnippet = node.getElementsByTagName("JAVASCRIPT_LONELY")[0];
if (jsLonelySnippet) {
writeTo.push("<pre class='prettyprintoutput'>");
writeTo.push(jsLonelySnippet.firstChild.nodeValue.trimRight());
writeTo.push("</pre>");
}
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 = [];
if (jsSnippet) recursiveProcessPureText(jsSnippet.firstChild, codeArr);
const codeStr = codeArr.join("").trim();
const codeArr_run = [];
if (jsRunSnippet)
recursiveProcessPureText(jsRunSnippet.firstChild, codeArr_run);
const codeStr_run = codeArr_run.join("").trim();
// Do warning for very long lines if no latex
if (node.getAttribute("LATEX") !== "yes") {
//checkLongLineWarning(codeStr);
}
if (node.getAttribute("EVAL") === "no") {
writeTo.push("<pre class='prettyprint no-eval'>\n");
writeTo.push(codeStr);
writeTo.push("</pre>");
} else {
writeTo.push(
"<pre class='prettyprint eval' title='Evaluate Javascript program'"
);
let reqStr = "";
let reqArr = [];
const snippetName = node.getElementsByTagName("NAME")[0];
let nameStr;
if (snippetName) {
nameStr = snippetName.firstChild.nodeValue;
// console.log(nameStr);
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);
}
// make url for source academy link
const compressed = lzString.compressToEncodedURIComponent(
"// SICP JS " +
chapterIndex +
importStatement +
reqStr +
"\n\n" +
codeStr_run +
exampleStr
);
const url =
sourceAcademyURL +
"/playground#chap=" +
chap +
variant +
"&prgrm=" +
compressed;
const chunks = (codeStr + "\n").match(
/^((?:.*?[\r\n]+){1,6})((?:.|\n|\r)*)$/
);
// 6 lines plus rest
writeTo.push(`onclick="window.open('${url}')">`);
writeTo.push(chunks[1]);
if (chunks[2]) {
writeTo.push(chunks[2]);
}
writeTo.push("</pre>");
}
}
if (jsOutputSnippet) {
writeTo.push("<pre class='prettyprintoutput'>");
writeTo.push(jsOutputSnippet.firstChild.nodeValue.trimRight());
writeTo.push("</pre>");
}
};
export default processSnippetHtml;