forked from marijnh/Eloquent-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchapter_info.js
More file actions
211 lines (194 loc) · 8.01 KB
/
chapter_info.js
File metadata and controls
211 lines (194 loc) · 8.01 KB
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
// Fragile, hacky script that finds exercises in chapters, extracts
// their starting code, and collects it into a big JSON object
// together with the solution code.
const PJSON = require("./pseudo_json")
let fs = require("fs");
let output = [], failed = false;
let allSolutions = fs.readdirSync("code/solutions/").filter(file => !/^2[012]/.test(file));
for (let file of fs.readdirSync(".").sort()) {
let match = /^((\d+).*).md$/.exec(file), chapNum = match && match[2];
if (!match || chapNum == 22) continue;
let text = fs.readFileSync(file, "utf8");
let meta = (/{{meta (.*)}}/.exec(text) || {1: "{}"})[1]
let includes = /\bload_files: (\[.*?\])/.exec(meta)
if (includes) includes = JSON.parse(includes[1]);
let chapter = {number: +chapNum,
id: match[1],
title: text.match(/(?:^|\n)# (.*?)\n/)[1],
start_code: getStartCode(text, includes),
exercises: [],
include: includes};
let zip = chapterZipFile(text, chapter);
let extraLinks = meta.match(/\bcode_links: (\[.*?\])/);
if (extraLinks) extraLinks = JSON.parse(extraLinks[1]);
if (extraLinks || zip)
chapter.links = (zip ? [zip] : []).concat(extraLinks || []);
let exerciseSection = text.indexOf("\n## Exercises\n");
let exerciseBlock = exerciseSection >= 0 ? text.slice(exerciseSection) : "";
let header = /\n### (.*?)\n/g, nextHeader = /\n##+ \w/g;
let num = 1;
while (match = header.exec(exerciseBlock)) {
nextHeader.lastIndex = header.lastIndex
let foundNext = nextHeader.exec(exerciseBlock)
let nextsection = foundNext ? foundNext.index : -1
for (let pos = header.lastIndex;;) {
let ifdef = exerciseBlock.indexOf("{{if interactive", pos);
if (ifdef == -1 || nextsection > 0 && nextsection < ifdef) break;
let indef = exerciseBlock.slice(pos = ifdef + 15, exerciseBlock.indexOf("if}}", ifdef));
let sourceBlock = indef.match(/```(.*)\n([^]+?)\n```/);
if (!sourceBlock || sourceBlock[1].indexOf("null") > -1) continue;
let type = sourceBlock[1].indexOf("html") > -1 ? "html" : "js";
let file = chapNum + "_" + num + "_" + match[1].toLowerCase().replace(/[^\-\s\w]/g, "").replace(/\s/g, "_") + "." + type;
let solution, extra
try {
solution = fs.readFileSync("code/solutions/" + file, "utf8");
extra = /^\s*<!doctype html>\s*(<base .*\n(<script src=.*\n)*)?/.exec(solution);
if (extra) solution = solution.slice(extra[0].length);
allSolutions.splice(allSolutions.indexOf(file), 1);
} catch(e) {
console.error("File ", file, " does not exist.", e);
failed = true;
}
if (sourceBlock) {
chapter.exercises.push({
name: match[1],
file: "code/solutions/" + file,
number: num,
type: type,
code: type == "html" ? prepareHTML(sourceBlock[2], includes) : sourceBlock[2],
solution: type == "html" ? prepareHTML(solution.trim(), includes) : solution.trim()
});
break;
}
}
++num;
}
let nodeInfo = "// Node exercises can not be ran in the browser,\n// but you can look at their solution here.\n";
if (chapter.number == 20) chapter.exercises = [
{name: "Search tool",
file: "code/solutions/20_1_search_tool.js",
number: 1,
type: "js",
code: nodeInfo,
solution: fs.readFileSync("code/solutions/20_1_search_tool.js", "utf8")
},
{name: "Directory creation",
file: "code/solutions/20_2_directory_creation.js",
number: 2,
type: "js",
code: nodeInfo,
solution: fs.readFileSync("code/solutions/20_2_directory_creation.js", "utf8")
},
{name: "A public space on the web",
file: "code/solutions/20_3_a_public_space_on_the_web.zip",
number: 3,
type: "js",
code: nodeInfo,
solution: "// This solutions consists of multiple files. Download it\n// though the link below.\n"
}
];
if (chapter.number == 21) chapter.exercises = [
{name: "Disk persistence",
file: "code/solutions/21_1_disk_persistence.js",
number: 1,
type: "js",
code: nodeInfo,
solution: fs.readFileSync("code/solutions/21_1_disk_persistence.js", "utf8")
},
{name: "Comment field resets",
file: "code/solutions/21_2_comment_field_resets.js",
number: 2,
type: "js",
code: nodeInfo,
solution: fs.readFileSync("code/solutions/21_2_comment_field_resets.js", "utf8")
}
];
output.push(chapter);
}
output.push({
title: "JavaScript and Performance",
number: 22,
start_code: "<!-- This chapter exists in the paper book, not in the online version -->\n\n<script>\n runLayout(forceDirected_simple, treeGraph(4, 4));\n</script>\n",
include: ["code/draw_layout.js", "code/chapter/22_fast.js"],
exercises: [
{name: "Pathfinding",
file: "code/solutions/22_1_pathfinding.js",
number: 1,
type: "js",
code: "function findPath(a, b) {\n // Your code here...\n}\n\nlet graph = treeGraph(4, 4);\nlet root = graph[0], leaf = graph[graph.length - 1];\nconsole.log(findPath(root, leaf).length);\n// → 4\n\nleaf.connect(root);\nconsole.log(findPath(root, leaf).length);\n// → 2\n",
solution: fs.readFileSync("code/solutions/22_1_pathfinding.js", "utf8")
},
{name: "Timing",
file: "code/solutions/22_2_timing.js",
number: 2,
type: "js",
code: "",
solution: fs.readFileSync("code/solutions/22_2_timing.js", "utf8")
},
{name: "Optimizing",
file: "code/solutions/22_3_optimizing.js",
number: 3,
type: "js",
code: "",
solution: fs.readFileSync("code/solutions/22_3_optimizing.js", "utf8")
}
]
});
if (allSolutions.length) {
console.error("Solution files " + allSolutions + " were not used.");
failed = true;
}
if (!failed)
console.log("var chapterData = " + JSON.stringify(output, null, 2) + ";");
else
process.exit(1);
function prepareHTML(code, include) {
return "<!doctype html>\n" + (include || []).map(s => "<script src=\"" + s + "\"></script>\n").join("") + "\n" + code;
}
function guessType(code) {
return /^[\s\w\n:]*</.test(code) ? "html" : "js";
}
function getStartCode(text, includes) {
let found = /\n```(.*?\bstartCode:.*)\n([^]*?\n)```/.exec(text);
if (!found) return ""
let snippet = found[2].replace(/(\n|^)\s*\/\/ →.*\n/g, "$1");
let directive = String(PJSON.parse(found[1]).startCode), m;
if (m = directive.match(/top_lines:\s*(\d+)/))
snippet = snippet.split("\n").slice(0, Number(m[1])).join("\n") + "\n";
if (m = directive.match(/bottom_lines:\s*(\d+)/)) {
let lines = snippet.trimRight().split("\n");
snippet = lines.slice(lines.length - Number(m[1])).join("\n") + "\n";
}
if (guessType(snippet) == "html")
return prepareHTML(snippet, includes);
else
return snippet;
}
function chapterZipFile(text, chapter) {
let spec = text.match(/\n:zip: (\S+)(?: include=(.*))?/);
if (!spec) return null;
if (!chapter.start_code) throw new Error("zip but no start code");
let name = "code/chapter/" + chapter.id + ".zip";
let files = (chapter.include || []).concat(spec[2] ? JSON.parse(spec[2]) : []);
let exists = fs.existsSync(name) && fs.statSync(name).mtime;
if (exists && files.every(file => fs.statSync("html/" + file).mtime < exists))
return name;
let zip = new (require("jszip"));
for (let file of files) {
zip.file(chapter.id + "/" + file, fs.readFileSync("html/" + file));
}
if (spec[1].indexOf("html") != -1) {
let html = chapter.start_code;
if (guessType(html) != "html")
html = prepareHTML("<body><script>\n" + html.trim() + "\n</script></body>", chapter.include);
zip.file(chapter.id + "/index.html", html);
}
if (spec[1].indexOf("node") != -1) {
zip.file(chapter.id + "/code/load.js", fs.readFileSync("code/load.js", "utf8"));
let js = chapter.start_code;
if (chapter.include) js = "// load dependencies\nrequire(\"./code/load\")(" + chapter.include.map(JSON.stringify).join(", ") + ");\n\n" + js;
zip.file(chapter.id + "/run_with_node.js", js);
}
fs.writeFileSync(name, zip.generate({type: "nodebuffer"}));
return name;
}