-
Notifications
You must be signed in to change notification settings - Fork 26
/
build.js
207 lines (165 loc) · 5.89 KB
/
build.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
var wrench = require('wrench');
var files = [
'01a_overview.md',
'02a_handson_charts.md',
'03a_advanced_drawing.md',
'04a_animation.md',
//'05a_gaming1.md',
'05b_gaming2.md',
'05c_gaming3.md',
'12a_intro_audio.md',
'12b_deep_audio.md',
'12c_game_audio.md',
'13a_gameinput.md',
];
// ================
var ometajs = require('ometa-js');
var fs = require('fs');
var Mustache = require('mustache');
var chapter_template = fs.readFileSync('templates/chapter.template').toString();
var MD = require('./markdown2.ometajs').Markdown;
MD.customProcessors['jangle'] = function(append, params) {
console.log("jangle params",params);
console.log("code block = ", MD.codeblocks[params.id]);
params.code = MD.codeblocks[params.id];
var htmlcode = params.code;
var rawcode = params.code;
htmlcode = htmlcode.replace(/(var\d)/g,'<i class="$1"></i>');
var dvals = [];
var paramNames = [];
for(var name in params.defaultValues) {
var val = params.defaultValues[name];
console.log("name = ",name,val);
htmlcode = htmlcode.replace('value'+name,val);
dvals.push(val);
paramNames.push(name);
}
params.htmlcode = htmlcode;
params.rawcode = rawcode;
params.drawCall = 'drawIt(ctx,'+dvals.join(',')+');';
params.paramNames = '"'+paramNames.join('","')+'"';
params.paramStruct = JSON.stringify(params.defaultValues);
var template = fs.readFileSync('templates/jangle.template').toString();
append(Mustache.render(template,params));
}
var SCREENSHOT_TEMPLATE = fs.readFileSync('templates/screenshot.template').toString();
var INTERACTIVE_TEMPLATE = fs.readFileSync('templates/interactive.template').toString();
function findTitle(obj) {
for(var id in obj) {
if(obj[id].level == 1) {
return {
id: id,
text: obj[id].text,
}
}
}
}
function findSections(obj) {
var sections = [];
for(var id in obj) {
if(obj[id].level == 2) {
sections.push({
id: id,
text: obj[id].text
});
}
}
return sections;
}
processBook({
files: files,
basepath: 'chapters/',
outdir: 'buildbook',
imagepath:'chapters/',
demopath:'chapters',
});
processBook({
files: ['lecture1.md','handson1.md','lecture2.md','handson2.md','lecture3.md'],
basepath: '2014/',
outdir: 'build2014',
imagepath: '2014/',
demopath:'2014/'
});
function copyFile(image_in, image_out) {
try {
fs.createReadStream(image_in)
.on('error', function() {
console.log("error reading file",image_in);
})
.pipe(fs.createWriteStream(image_out))
.on('error', function() {
console.log("got an error writing to ",image_out);
});
} catch (e) {
console.log("error copying", image_in);
}
}
function processBook(args) {
if(!args.files) throw new Error();
if(!args.basepath) throw new Error();
if(!args.outdir) throw new Error();
if(!args.imagepath) throw new Error();
if(!args.demopath) throw new Error();
wrench.rmdirSyncRecursive(args.outdir, true);
var image_out_path = args.outdir+'/images';
wrench.mkdirSyncRecursive(image_out_path);
var demo_out_path = args.outdir+'/demos';
wrench.mkdirSyncRecursive(demo_out_path);
wrench.copyDirSyncRecursive('node_modules/bootstrap/dist/css',args.outdir+'/css');
wrench.copyDirSyncRecursive('node_modules/bootstrap/dist/js',args.outdir+'/js');
wrench.copyDirSyncRecursive('node_modules/bootstrap/dist/fonts',args.outdir+'/fonts');
copyFile('templates/main.css',args.outdir+'/css/main.css');
MD.classProcessors['screenshot'] = function(append, text, href) {
var image_in = args.imagepath+href;
var ext = href.substring(href.lastIndexOf('.'));
var name = "img"+Math.round(Math.random()*10000)+ext;
var image_out = image_out_path+'/'+name;
console.log("copying",image_in,"to",image_out);
copyFile(image_in,image_out);
var data = {
text:text,
url:'images/'+name,
}
append(Mustache.render(SCREENSHOT_TEMPLATE, data));
}
MD.customProcessors['interactive'] = function(append, params) {
var image_in = args.imagepath+params.image;
var image_name = "img"+Math.round(Math.random()*10000)+".png";
var image_out = image_out_path+'/'+image_name;
copyFile(image_in,image_out);
console.log("copying",image_in,"to",image_out);
var html_in = args.demopath+params.href;
var html_name = "demo"+Math.round(Math.random()*10000)+".html";
var html_out = demo_out_path+'/'+html_name;
copyFile(html_in,html_out);
console.log("copying",html_in,"to",html_out);
var data = {
href:'demos/'+html_name,
url:'images/'+image_name,
}
append(Mustache.render(INTERACTIVE_TEMPLATE, data));
}
var results = [];
var count = 1;
if(!fs.existsSync(args.outdir)) fs.mkdirSync(args.outdir);
args.files.forEach(function(filename) {
var text = fs.readFileSync(args.basepath+filename);
MD.reset();
var rendered = MD.matchAll(text.toString(),'start');
rendered.number = count;
var text = Mustache.render(chapter_template,rendered);
fs.writeFileSync(args.outdir+'/chapter'+count+'.html',text);
results.push({
title: findTitle(rendered.sections),
sections: findSections(rendered.sections),
filename: 'chapter'+count+'.html',
});
count++;
});
var toc_templ = fs.readFileSync('templates/toc.template').toString();
var toc = {
bookname: 'HTML Canvas Deep Dive',
sections: results,
}
fs.writeFileSync(args.outdir+'/toc.html',Mustache.render(toc_templ,toc));
}