Skip to content

Commit bfb4b1e

Browse files
committed
Automatically extract background code from chapters
Rather than duplicating it.
1 parent 79516da commit bfb4b1e

11 files changed

Lines changed: 65 additions & 214 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/tex/*
22
/html/*.html
3+
/html/js/0*.js
34
/node_modules
45
.tern-port

00_intro.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
:next_link: 01_values
2-
:load_files: ["js/code/00_intro.js"]
2+
:load_files: ["js/00_intro.js"]
33

44
= Introduction =
55

@@ -421,4 +421,3 @@ console.log(fac(8));
421421
-----
422422

423423
Good luck!
424-

04_data.txt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
:chap_num: 4
22
:prev_link: 03_functions
33
:next_link: 05_higher_order
4-
:load_files: ["js/code/jaques_journal.js", "js/code/04_data.js"]
4+
:load_files: ["js/code/jaques_journal.js", "js/04_data.js"]
55

66
= Data Structures: Object and Array =
77

@@ -363,6 +363,8 @@ JavaScript, but it is possible to write it yourself.
363363
So Jaques starts up his JavaScript interpreter, and sets up the
364364
environment he needs to keep his journal.
365365

366+
// include_code
367+
366368
[source,javascript]
367369
----
368370
var journal = [];
@@ -465,6 +467,8 @@ event (say, pizza) is true, but Jaques did not turn into a squirrel.
465467
(((phi coefficient)))This is the function that computes a phi
466468
coefficient from such an array:
467469

470+
// include_code strip_log
471+
468472
[source,javascript]
469473
----
470474
function phi(table) {
@@ -496,6 +500,8 @@ To extract a two-by-two table for a specific event from this journal,
496500
we must loop over all the entries and tally the frequencies with which
497501
the event occurs in relation to squirrel transformations.
498502

503+
// include_code strip_log
504+
499505
[source,javascript]
500506
----
501507
function hasEvent(event, entry) {
@@ -599,6 +605,8 @@ run across a type that we didn't see before—that isn't in the
599605
`phis` object yet—we compute its correlation and add it to the
600606
object.
601607

608+
// include_code strip_log
609+
602610
[source,javascript]
603611
----
604612
function gatherCorrelations(journal) {
@@ -1065,6 +1073,8 @@ array). A list is a set of objects, with the first object holding a
10651073
reference (in a property) to the second, the second to the third, and
10661074
so on.
10671075

1076+
// include_code
1077+
10681078
[source,javascript]
10691079
----
10701080
var list = {

05_higher_order.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
:chap_num: 5
22
:prev_link: 04_data
33
:next_link: 06_object
4-
:load_files: ["js/code/ancestry.js", "js/code/05_higher_order.js"]
4+
:load_files: ["js/code/ancestry.js", "js/05_higher_order.js"]
55

66
= Higher-Order Functions =
77

@@ -402,6 +402,8 @@ the website!!tex (`eloquentjavascript.net/code`)!!,
402402
contains the content of my JSON file as a string. Let us decode it and
403403
see how many people it contains:
404404

405+
// include_code strip_log
406+
405407
[source,javascript]
406408
----
407409
var ancestry = JSON.parse(ANCESTRY_FILE);
@@ -634,6 +636,8 @@ if so, how much DNA I theoretically share with him.
634636
First, I build up an object that makes it possible to easily find
635637
people by name.
636638

639+
// include_code strip_log
640+
637641
[source,javascript]
638642
----
639643
var byName = {};
@@ -662,6 +666,8 @@ Given a person, a function to combine values from the two parents of a
662666
given person, and a zero value that is to be used for unknown persons,
663667
the `reduceAncestors` function computes a value from a family tree.
664668

669+
// include_code
670+
665671
[source,javascript]
666672
----
667673
function reduceAncestors(person, f, zero) {
@@ -816,6 +822,9 @@ present in the array. The `byName` object, which makes it easy to find
816822
a person's object from their name, might be useful here.
817823

818824
ifdef::html_target[]
825+
826+
// include_code
827+
819828
[source,javascript]
820829
----
821830
function average(array) {

06_object.txt

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
:chap_num: 6
22
:prev_link: 05_higher_order
33
:next_link: 07_elife
4-
:load_files: ["js/code/mountains.js", "js/code/06_object.js"]
4+
:load_files: ["js/code/mountains.js", "js/06_object.js"]
55

66
= The Secret Life of Objects =
77

@@ -78,6 +78,8 @@ property and immediately called, as in ++object.method()++—the special
7878
variable `this` in its body will point to the object that it was
7979
called on.
8080

81+
// include_code top_lines:6
82+
8183
[source,javascript]
8284
----
8385
function speak(line) {
@@ -213,6 +215,8 @@ Here is a simple constructor for rabbits. It is convention to
213215
capitalize the names of constructors, so that they are easily
214216
distinguished from other functions.
215217

218+
// include_code top_lines:6
219+
216220
[source,javascript]
217221
----
218222
function Rabbit(type) {
@@ -231,6 +235,8 @@ New objects created with this constructor will get this object as
231235
their prototype. So to add a `speak` method to rabbits created with
232236
the `Rabbit` constructor, we can simply do this:
233237

238+
// include_code top_lines:4
239+
234240
[source,javascript]
235241
----
236242
Rabbit.prototype.speak = function(line) {
@@ -318,6 +324,8 @@ associate values with names by creating properties for the names and
318324
giving them the corresponding value as value. Here's a similar example
319325
from chapter 4:
320326

327+
// include_code
328+
321329
[source,javascript]
322330
----
323331
var ages = {};
@@ -503,6 +511,8 @@ example, since it lends itself well to that approach.
503511
The first part of the program computes arrays of minimum column widths
504512
and row heights from a collection of cells.
505513

514+
// include_code
515+
506516
[source,javascript]
507517
----
508518
function rowHeights(rows) {
@@ -542,6 +552,8 @@ human readers) that this argument is not going to be used.
542552

543553
Drawing a table now looks like this:
544554

555+
// include_code
556+
545557
[source,javascript]
546558
----
547559
function drawTable(rows) {
@@ -574,6 +586,8 @@ Let us write a constructor for cells that contain text. The
574586
constructor splits a string into an array of lines, and the `minWidth`
575587
method finds the maximum line width in this array.
576588

589+
// include_code
590+
577591
[source,javascript]
578592
----
579593
function repeat(string, times) {
@@ -643,6 +657,8 @@ We will want to emphasize the top row, which contains the column
643657
names, by underlining the cells with a series of dash characters. No
644658
problem, we simply write a cell type that handles underlining.
645659

660+
// include_code
661+
646662
[source,javascript]
647663
----
648664
function UnderlinedCell(inner) {
@@ -772,6 +788,8 @@ We could simply write a whole new constructor, with all three methods
772788
in its prototype. But prototypes may themselves have prototypes, and
773789
this allows us to do something clever:
774790

791+
// include_code
792+
775793
[source,javascript]
776794
----
777795
function RTextCell(text) {
@@ -808,6 +826,8 @@ Now if we slightly adjust the `dataTable` function to use
808826
`RTextCell`'s for cells whose value is a number, we get the table we
809827
were aiming for:
810828

829+
// include_code strip_log
830+
811831
[source,javascript]
812832
----
813833
function dataTable(data) {

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ html: $(foreach CHAP,$(CHAPTERS),html/$(CHAP).html)
66

77
html/%.html: %.txt asciidoc_html.conf
88
PATH=node_modules/codemirror/bin:$(PATH) asciidoc -f asciidoc_html.conf --backend=html5 -o $@ $<
9+
node bin/build_code.js $<
910

1011
tex: $(foreach CHAP,$(CHAPTERS),tex/$(CHAP).tex)
1112

bin/build_code.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var fs = require("fs");
2+
3+
var file = process.argv[2];
4+
var input = fs.readFileSync(file, "utf8");
5+
6+
var included = /\/\/ include_code(.*)\n(?:\/\/.*\n)*\s*\[source,javascript\]\n----\n([\s\S]*?\n)----/g, m;
7+
var code = [];
8+
while (m = included.exec(input)) {
9+
var snippet = m[2], directive = m[1];
10+
if (directive.indexOf("strip_log") > -1)
11+
snippet = snippet.replace(/((?:\n|^)\s*)console\.log\(.*\);(\n|$)/g, "$1;$2");
12+
if (m = directive.match(/top_lines:\s*(\d+)/))
13+
snippet = snippet.split("\n").slice(0, Number(m[1])).join("\n") + "\n";
14+
code.push(snippet);
15+
}
16+
17+
if (code.length) {
18+
var out = "html/js/" + file.replace(".txt", ".js");
19+
fs.writeFileSync(out, code.join("\n"), "utf8");
20+
}

code/04_data.js

Lines changed: 0 additions & 57 deletions
This file was deleted.

code/05_higher_order.js

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)