Skip to content

Commit d8a8163

Browse files
authored
chapter 1 revised (#235)
1 parent 8415bbb commit d8a8163

30 files changed

+3444
-2694
lines changed

javascript/constants.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export const sourceAcademyURL = "http://source-academy.github.io";
2+
3+
// to change to localhost if required
4+
// http://localhost:8075
5+
6+
export const authors = "Harold Abelson and Gerald Jay Sussman";
7+
8+
export const authors_with = "Julie Sussman";
9+
10+
export const adapters = "Martin Henz and Tobias Wrigstad";
11+
12+
export const adapters_with = "Chan Ger Hean, He Xinyue, Liu Hang, Feng Piaopiao, Jolyn Tan and Wang Qian";
13+
14+

javascript/htmlContent.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { adapters, adapters_with, authors, authors_with
2+
} from "constants";
3+
14
import { write } from "fs-extra";
25

36
const shortTitleDefault = `SICP — JS`;
@@ -99,7 +102,7 @@ export const switchTitle = version => {
99102
</div>`;
100103
} else if (version == "scheme") {
101104
// scheme version of the web textbook has yet been developed
102-
console.log("generate sicp schceme web textook");
105+
console.log("generate sicp scheme web textook");
103106
}
104107
};
105108

@@ -246,13 +249,25 @@ export const indexPage = writeTo => {
246249
</TABLE>
247250
248251
<div class="title-text-ATTRIBUTION">
249-
<span class="title-text-AUTHOR">Martin Henz and Tobias Wrigstad<br/>with Chan Ger Hean, He Xinyue, Liu Hang, Feng Piaopiao, Jolyn Tan and Wang Qian</span> <span class="title-text-TITLE">adapters to JavaScript</span>
252+
<span class="title-text-AUTHOR">Martin Henz and Tobias Wrigstad<br/>with Chan Ger Hean, He Xinyue, Liu Hang, Feng Piaopiao, Jolyn Tan and Wang Qian</span><span class="title-text-TITLE">adapters to JavaScript</span>
250253
</div>
251254
252255
<div class="title-text-ATTRIBUTION">
253256
<span class="title-text-TITLE">original textbook by</span> <span class="title-text-AUTHOR">Harold Abelson and Gerald Jay Sussman<br/>with Julie Sussman</span>
254257
</div>
255258
259+
<div><a href="http://creativecommons.org/licenses/by-sa/4.0/" rel="nofollow"><img src="https://camo.githubusercontent.com/68525ec5f8acf359e826761fa50e9262da143eb5/68747470733a2f2f6c6963656e7365627574746f6e732e6e65742f6c2f62792d73612f342e302f38387833312e706e67" alt="CC BY-SA 4.0" data-canonical-src="https://licensebuttons.net/l/by-sa/4.0/88x31.png" style="max-width:100%;"></a>
260+
<p>
261+
This work is licensed under the <a href="http://creativecommons.org/licenses/by-sa/4.0/" rel="nofollow">Creative Commons Attribution-ShareAlike 4.0
262+
International License</a>.
263+
</div>
264+
265+
<div><a href="https://www.gnu.org/licenses/gpl-3.0.en.html" rel="nofollow"><img src="https://camo.githubusercontent.com/46d38fe6087a9b9bdf7e45458901b818765b8391/68747470733a2f2f75706c6f61642e77696b696d656469612e6f72672f77696b6970656469612f636f6d6d6f6e732f7468756d622f372f37392f4c6963656e73655f69636f6e2d67706c2e7376672f353070782d4c6963656e73655f69636f6e2d67706c2e7376672e706e67" alt="GPL 3" data-canonical-src="https://upload.wikimedia.org/wikipedia/commons/thumb/7/79/License_icon-gpl.svg/50px-License_icon-gpl.svg.png" style="max-width:100%;"></a>
266+
<p>
267+
All JavaScript programs in this work are licensed under the
268+
<a href="https://www.gnu.org/licenses/gpl-3.0.en.html" rel="nofollow">GNU General Public License Version 3</a>.
269+
</div>
270+
256271
`);
257272
};
258273

javascript/latexContent.js

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
1+
import { adapters, adapters_with, authors, authors_with
2+
} from "constants";
3+
14
const title = `\\begin{titlepage}
25
\\centering
3-
\\includegraphics[width=0.8\\textwidth]{./sicp.png}\\par
6+
\\includegraphics[width=0.6\\textwidth]{./sicp.png}\\par
47
{\\LARGE\\bfseries Structure and Interpretation of Computer\\\\
58
Programs — JavaScript Adaptation\\par}
69
\\vspace{0.5cm}
710
\\begin{flushright}
8-
{\\Large Harold Abelson and Gerald Jay Sussman \\\\}
9-
{\\large with Julie Sussman \\\\ — \\textit{authors} \\par}
10-
\\vspace{0.5cm}
11-
{\\Large Martin Henz and Tobias Wrigstad\\\\}
12-
{\\large with Liu Hang, Feng Piaopiao, Jolyn Tan and Chan Ger Hean \\\\
11+
{\\Large Martin Henz and Tobias Wrigstad \\\\}
12+
{\\large with Chan Ger Hean, He Xinyue, Liu Hang, \\\\ Feng Piaopiao, Jolyn Tan and Wang Qian \\\\
1313
— \\textit{adapters to JavaScript}\\par}
14+
\\vspace{0.5cm}
15+
{\\large \\textit{original textbook by} \\\\}
16+
{\\Large Harold Abelson and Gerald Jay Sussman \\\\}
17+
{\\large with Julie Sussman \\par}
1418
\\end{flushright}
15-
\\end{titlepage}`;
19+
20+
{This work is licensed under the \\href{https://creativecommons.org/licenses/by-sa/4.0/}{Creative Commons Attribution-ShareAlike 4.0 International License}.}
21+
22+
\\vspace{0.2cm}
23+
{All JavaScript programs in this work are licensed under the \\href{https://www.gnu.org/licenses/gpl-3.0.en.html}{GNU General Public License Version 3}.}
24+
25+
\\end{titlepage}
26+
`;
1627

1728
export const preamble = `\\documentclass[a4paper, 12pt]{report}
1829
\\usepackage[T1]{fontenc}
@@ -104,7 +115,7 @@ export const preamble = `\\documentclass[a4paper, 12pt]{report}
104115
basicstyle=\\linespread{1.0}\\ttfamily,
105116
showstringspaces=false,
106117
showspaces=false,
107-
escapeinside={/*!}{!*/}
118+
escapechar=^
108119
}
109120
110121
\\usepackage{xcolor}
@@ -122,10 +133,10 @@ export const preamble = `\\documentclass[a4paper, 12pt]{report}
122133
framerule=0.5ex,
123134
framesep=1em,
124135
rulecolor=\\color{LeftBarClickable},
125-
escapeinside={/*!}{!*/}
136+
escapechar=^
126137
}
127138
\\lstnewenvironment{JavaScript}{\\lstset{style=JavaScript}}{}
128-
\\lstnewenvironment{JavaScriptClickable}{\\lstset{style=JavaScript,frame=leftline}}{}
139+
\\lstnewenvironment{JavaScriptClickable}{\\lstset{style=JavaScript,frame=leftline,escapeinside={/*!}{!*/}}}{}
129140
\\lstdefinestyle{JavaScriptOutput}{
130141
language=JavaScript,
131142
basicstyle=\\linespread{1.0}\\slshape,
@@ -138,7 +149,7 @@ export const preamble = `\\documentclass[a4paper, 12pt]{report}
138149
framerule=0.5ex,
139150
framesep=1em,
140151
rulecolor=\\color{LeftBarClickable},
141-
escapeinside={/*!}{!*/}
152+
escapechar=^
142153
}
143154
\\lstnewenvironment{JavaScriptOutput}{\\lstset{style=JavaScriptOutput}}{}
144155

javascript/parseXmlJs.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,26 @@ const preserveTags = new Set([
4545

4646
const processTextFunctions = {
4747
SNIPPET: (node, writeTo) => {
48-
if (node.getAttribute("HIDE") == "yes") {
49-
return;
50-
} else if (node.getAttribute("LATEX") == "yes") {
48+
if (node.getAttribute("LATEX") == "yes") {
5149
return;
5250
} else if (node.getAttribute("EVAL") === "no") {
5351
return;
5452
}
5553

5654
const writeTojs = [];
5755
snippet_count += 1;
56+
const snippet_count_string = snippet_count < 10
57+
? "0" + snippet_count
58+
: snippet_count;
5859
processSnippetJs(node, writeTojs, "js");
5960

60-
const outputFile = path.join(relativeFileDirectory, `${snippet_count}.js`);
61+
const nameNode = node.getElementsByTagName("NAME")[0];
62+
63+
const fileName = (nameNode) ?
64+
snippet_count_string + "_" + nameNode.firstChild.nodeValue
65+
: snippet_count_string;
66+
67+
const outputFile = path.join(relativeFileDirectory, fileName + `.js`);
6168

6269
const stream = fs.createWriteStream(outputFile);
6370
stream.once("open", fd => {

javascript/processingFunctions/processSnippetEpub.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { sourceAcademyURL
2+
} from "../constants";
13
import lzString from "lz-string";
24
import {
35
checkLongLineWarning,
@@ -41,13 +43,8 @@ export const setupSnippetsEpub = node => {
4143
}
4244
};
4345

44-
const sourceAcademyURL = "https://sourceacademy.nus.edu.sg";
45-
// to change to localhost if required
46-
// http://localhost:8075
47-
4846
const recursiveGetRequires = (name, seen) => {
4947
if (seen.has(name)) return;
50-
seen.add(name);
5148
const snippetEntry = snippetStore[name];
5249
if (!snippetEntry) {
5350
missingRequireWarning(name);
@@ -56,6 +53,7 @@ const recursiveGetRequires = (name, seen) => {
5653
for (const requirement of snippetEntry.requireNames) {
5754
recursiveGetRequires(requirement, seen);
5855
}
56+
seen.add(name);
5957
};
6058

6159
export const processSnippetEpub = (node, writeTo) => {
@@ -102,6 +100,17 @@ export const processSnippetEpub = (node, writeTo) => {
102100
nameStr = snippetName.firstChild.nodeValue;
103101
const reqSet = new Set();
104102
recursiveGetRequires(nameStr, reqSet);
103+
const examples = node.getElementsByTagName("EXAMPLE");
104+
for (let i = 0; examples[i]; i++) {
105+
const exampleString = examples[i].firstChild.nodeValue;
106+
const exampleNode = snippetStore[exampleString];
107+
if (exampleNode) {
108+
const exampleRequires = exampleNode.requireNames;
109+
for (let j = 0; exampleRequires[j]; j++) {
110+
recursiveGetRequires(exampleRequires[j], reqSet);
111+
}
112+
}
113+
}
105114
for (const reqName of reqSet) {
106115
const snippetEntry = snippetStore[reqName];
107116
if (snippetEntry && reqName !== nameStr) {

javascript/processingFunctions/processSnippetHtml.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { sourceAcademyURL
2+
} from "../constants";
13
import lzString from "lz-string";
24
import {
35
checkLongLineWarning,
@@ -45,13 +47,8 @@ export const setupSnippetsHtml = node => {
4547
}
4648
};
4749

48-
const sourceAcademyURL = "https://sourceacademy.nus.edu.sg";
49-
// to change to localhost if required
50-
// http://localhost:8075
51-
5250
const recursiveGetRequires = (name, seen) => {
5351
if (seen.has(name)) return;
54-
seen.add(name);
5552
const snippetEntry = snippetStore[name];
5653
if (!snippetEntry) {
5754
missingRequireWarning(name);
@@ -60,6 +57,7 @@ const recursiveGetRequires = (name, seen) => {
6057
for (const requirement of snippetEntry.requireNames) {
6158
recursiveGetRequires(requirement, seen);
6259
}
60+
seen.add(name);
6361
};
6462

6563
export const processSnippetHtml = (node, writeTo, split) => {
@@ -102,11 +100,23 @@ export const processSnippetHtml = (node, writeTo, split) => {
102100
let reqStr = "";
103101
let reqArr = [];
104102
const snippetName = node.getElementsByTagName("NAME")[0];
103+
105104
let nameStr;
106105
if (snippetName) {
107106
nameStr = snippetName.firstChild.nodeValue;
108107
const reqSet = new Set();
109108
recursiveGetRequires(nameStr, reqSet);
109+
const examples = node.getElementsByTagName("EXAMPLE");
110+
for (let i = 0; examples[i]; i++) {
111+
const exampleString = examples[i].firstChild.nodeValue;
112+
const exampleNode = snippetStore[exampleString];
113+
if (exampleNode) {
114+
const exampleRequires = exampleNode.requireNames;
115+
for (let j = 0; exampleRequires[j]; j++) {
116+
recursiveGetRequires(exampleRequires[j], reqSet);
117+
}
118+
}
119+
}
110120
for (const reqName of reqSet) {
111121
const snippetEntry = snippetStore[reqName];
112122
if (snippetEntry && reqName !== nameStr) {

javascript/processingFunctions/processSnippetJs.js

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,6 @@ export const setupSnippetsJs = node => {
2828
const codeArr = [];
2929
recursiveProcessPureText(jsRunSnippet.firstChild, codeArr);
3030

31-
if (snippet.getElementsByTagName("EXPECTED")[0]) {
32-
codeArr.push(
33-
"\n// result: " +
34-
snippet.getElementsByTagName("EXPECTED")[0].firstChild.nodeValue +
35-
"\n"
36-
);
37-
}
38-
3931
const codeStr = codeArr.join("").trim();
4032

4133
const requirements = snippet.getElementsByTagName("REQUIRES");
@@ -49,13 +41,8 @@ export const setupSnippetsJs = node => {
4941
}
5042
};
5143

52-
const sourceAcademyURL = "https://sourceacademy.nus.edu.sg";
53-
// to change to localhost if required
54-
// http://localhost:8075
55-
5644
const recursiveGetRequires = (name, seen) => {
5745
if (seen.has(name)) return;
58-
seen.add(name);
5946
const snippetEntry = snippetStore[name];
6047
if (!snippetEntry) {
6148
missingRequireWarning(name);
@@ -64,13 +51,10 @@ const recursiveGetRequires = (name, seen) => {
6451
for (const requirement of snippetEntry.requireNames) {
6552
recursiveGetRequires(requirement, seen);
6653
}
54+
seen.add(name);
6755
};
6856

6957
export const processSnippetJs = (node, writeTo, fileFormat) => {
70-
if (node.getAttribute("HIDE") == "yes") {
71-
return;
72-
}
73-
7458
const jsSnippet = node.getElementsByTagName("JAVASCRIPT")[0];
7559
if (jsSnippet) {
7660
// JavaScript source for running. Overrides JAVASCRIPT if present.
@@ -95,6 +79,17 @@ export const processSnippetJs = (node, writeTo, fileFormat) => {
9579
nameStr = snippetName.firstChild.nodeValue;
9680
const reqSet = new Set();
9781
recursiveGetRequires(nameStr, reqSet);
82+
const examples = node.getElementsByTagName("EXAMPLE");
83+
for (let i = 0; examples[i]; i++) {
84+
const exampleString = examples[i].firstChild.nodeValue;
85+
const exampleNode = snippetStore[exampleString];
86+
if (exampleNode) {
87+
const exampleRequires = exampleNode.requireNames;
88+
for (let j = 0; exampleRequires[j]; j++) {
89+
recursiveGetRequires(exampleRequires[j], reqSet);
90+
}
91+
}
92+
}
9893
for (const reqName of reqSet) {
9994
const snippetEntry = snippetStore[reqName];
10095
if (snippetEntry && reqName !== nameStr) {
@@ -136,6 +131,13 @@ export const processSnippetJs = (node, writeTo, fileFormat) => {
136131
writeTo.push(reqStr);
137132
writeTo.push(codeStr_run);
138133
writeTo.push(exampleStr);
134+
if (node.getElementsByTagName("EXPECTED")[0]) {
135+
writeTo.push(
136+
"\n// expected: " +
137+
node.getElementsByTagName("EXPECTED")[0].firstChild.nodeValue +
138+
"\n"
139+
);
140+
}
139141
return;
140142
}
141143
}

javascript/processingFunctions/processSnippetPdf.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { sourceAcademyURL
2+
} from "../constants";
13
import lzString from "lz-string";
24
import {
35
checkLongLineWarning,
@@ -41,13 +43,8 @@ export const setupSnippetsPdf = node => {
4143
}
4244
};
4345

44-
const sourceAcademyURL = "https://sourceacademy.nus.edu.sg";
45-
// to change to localhost if required
46-
// http://localhost:8075
47-
4846
const recursiveGetRequires = (name, seen) => {
4947
if (seen.has(name)) return;
50-
seen.add(name);
5148
const snippetEntry = snippetStore[name];
5249
if (!snippetEntry) {
5350
missingRequireWarning(name);
@@ -56,6 +53,7 @@ const recursiveGetRequires = (name, seen) => {
5653
for (const requirement of snippetEntry.requireNames) {
5754
recursiveGetRequires(requirement, seen);
5855
}
56+
seen.add(name);
5957
};
6058

6159
export const processSnippetPdf = (node, writeTo) => {
@@ -99,6 +97,17 @@ export const processSnippetPdf = (node, writeTo) => {
9997
nameStr = snippetName.firstChild.nodeValue;
10098
const reqSet = new Set();
10199
recursiveGetRequires(nameStr, reqSet);
100+
const examples = node.getElementsByTagName("EXAMPLE");
101+
for (let i = 0; examples[i]; i++) {
102+
const exampleString = examples[i].firstChild.nodeValue;
103+
const exampleNode = snippetStore[exampleString];
104+
if (exampleNode) {
105+
const exampleRequires = exampleNode.requireNames;
106+
for (let j = 0; exampleRequires[j]; j++) {
107+
recursiveGetRequires(exampleRequires[j], reqSet);
108+
}
109+
}
110+
}
102111
for (const reqName of reqSet) {
103112
const snippetEntry = snippetStore[reqName];
104113
if (snippetEntry && reqName !== nameStr) {

scripts/do.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,12 @@ staging() {
8787
echo "check that everything works: https://sicp.comp.nus.edu.sg/staging"
8888
}
8989

90-
# install all files in docs_out to henz@suna, and copy from there to staging folder
90+
# install all files in docs_out to henz@suna, and copy from there to
91+
# the official website
9192
via() {
9293
cd ${DOCS}; scp -p -r * henz@suna.comp.nus.edu.sg:sicp; \
9394
echo "next: ssh henz@suna.comp.nus.edu.sg"; \
94-
echo "finally: cd sicp; scp -p -r * sicp@web1.comp.nus.edu.sg:public_html/staging"
95+
echo "finally: cd sicp; scp -p -r * sicp@web1.comp.nus.edu.sg:public_html"
9596
}
9697

9798
main $1

0 commit comments

Comments
 (0)