-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapcsa_simple_edx_build.js
412 lines (278 loc) · 10.8 KB
/
apcsa_simple_edx_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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/** APCSA AUTOBUILD SCRIPT
*
* run with `node test.js`
*/
// Default Node modules
fs = require('fs');
path = require('path');
exec = require('child_process').execSync;
cheerio = require('cheerio');
mkdirp = require('mkdirp');
llab = require('./lib/llab');
css = require('./code/css');
util = require('./code/util');
edx_util = require('./code/edx_util');
//////////////////////////////////
var TOPICS_TO_PROCESS = [];
TOPICS_TO_PROCESS.push(['c1/L1_objects_classes.topic', 'L1_']);
TOPICS_TO_PROCESS.push(['c1/L2_first_programming.topic', 'L2_']);
TOPICS_TO_PROCESS.push(['c1/L3_programming_2.topic', 'L3_']);
TOPICS_TO_PROCESS.push(['c1/L4_conditionals_1.topic', 'L4_']);
TOPICS_TO_PROCESS.push(['c1/L5_conditionals_2.topic', 'L5_']);
//TOPICS_TO_PROCESS.push(['c1/L6_virtual_pets.topic', 'L6_']);
//////////////////////////
var DEBUGMODE = true;
function DEBUG(str) {
if (DEBUGMODE) {
console.log(str);
}
}
var PROCESS_FUNCTIONS = {
file: processFile,
quiz: processQuiz,
markdown: processMarkdown,
external: processExternal
};
BASEURL = '/apcsa/r'; // MATCH LLAB.ROOTURL IN CURR REPO
//GLOBAL -- FIXME
var relPath;
var count;
var dir;
var PETER = false;
/////////
///////// COURSE level
// This is where a llab course CONTENT lives
// This should be a checked out state
// TODO: Config param this shit.
curFolder = '../apcsa-r/main/';
// This is where the edX XML folder will be.
//output = './apcsa_edx_out/OUT/';
// if you want to make a new folder (i.e., not populate in existing)
output = './apcsa_edx_out/cur_' + new Date().toISOString() + "/";
output = output.replace(/:/g, "_");
output = output.replace("-", "_");
//DEBUG("starting course: " + output);
edx_util.startCourse(output);
//////////////
////////////// CSS BS
//var cssRelPath = path.relative(curFolder, 'curriculum/edc/llab/css/default.css');
//var CSSOptions = {
// paths: [
// // TODO: Use newer llab stuff?
// // TODO: Exclude Bootstrap?
// 'curriculum/edc/llab/css/3.3.0/bootstrap-compiled.min.css',
// 'curriculum/edc/llab/css/default.css',
// 'curriculum/edc/css/bjc.css'
// ],
// rules: [
// {
// name: 'transform-urls',
// // Params: baseURL, filePath
// options: ['/bjc-r', cssRelPath]
// },
// {
// name: 'rename-selectors',
// options: ['.full', '.llab-full']
// },
// {
// name: 'prefix-selectors',
// options: '.llab-full'
// // , exclude: /bootstrap/
// }
// ]
//};
//
//
//fs.writeFileSync('./tmp/' + 'bjc-edx.css', css(CSSOptions));
// NEED TO MANUALLY IMPORT CSS
function css_get_reference(cssfile) {
var path = util.edXPath(cssfile);
return '<link rel="stylesheet" href="' + path + '">\n\n';
}
var cssString = '';
//cssString += css_get_reference('bootstrap.min.css');
//cssString += css_get_reference('bootstrap-theme.min.css');
cssString += css_get_reference('brainstorm.css');
cssString += css_get_reference('matchsequence.css');
//cssString += css_get_reference('default.css');
cssString += css_get_reference('ucb_apcsa_edx.css');
/////////
///////// TOPICS/CHAPTER
TOPICS_TO_PROCESS.forEach(function(topicinfo) {
var topicpath = topicinfo[0];
var prepender = topicinfo[1];
DEBUG("TOPIC sez do me: " + topicpath + " , " + prepender);
topic_do_me(topicinfo[0], topicinfo[1]);
});
edx_util.endCourse();
//var topic;
//var topicdata;
function topic_do_me(topicpath, prepender) {
topic = fs.readFileSync(util.topicPath(curFolder, topicpath));
topic = topic.toString();
topicdata = llab.parse(topic);
topictitle = topicdata.title.trim();
topictitle = topictitle || "TOPIC_TITLE";
edx_util.startTopic(topicdata, topictitle, topicpath, prepender);
topicdata.topics.forEach(parseTopic);
edx_util.endTopic();
}
function parseTopic (topic, args) {
topic.contents.forEach(parseSection, args);
}
/////////////////////// SEQUENTIAL / SECTION (topic heading)
function loadFile (path) {
// ?
}
function shouldParse (title) {
return true;
}
function parseSection (sectiondata, skip) {
var title = sectiondata.title.trim();
if (!shouldParse(title)) {
console.log('skipping section:', title);
return;
}
DEBUG("SECTION sez do me : " + title);
edx_util.startSection(sectiondata, title);
sectiondata.contents.forEach(function (page) {
processPage(page);
});
edx_util.endSection();
}
/////////////////////// VERTICAL / PAGE
// TODO figure out how to do edx-specific components (e.g., stuff that doesn't
// show up in llab-format -- autograding, etc.
// TODO bring back the split stuff -- apcsa will use it eventually
// This needs renamed...
function processPage (pagedata) {
var item = pagedata;
if (!item.url) {
console.log("skipping file: no url")
return;
} else if (item.url.indexOf(BASEURL) != 0) {
return;
}
var file = item.url.replace(BASEURL, curFolder);
var relPath = path.relative(curFolder, file);
var basename_base = path.basename(file, ".html");
var html = fs.readFileSync(file);
var $ = cheerio.load(html);
var title = $('title').html();
var text = $('body').html();
//DEBUG('Do Page (file : ' + file + ')(title: ' + title +')');
DEBUG('Do Page (file : ' + file + ')');
edx_util.startVertical(relPath, title);
//////// do parts
var partnum = 1;
var get_part_basename = function () {
return basename_base + "_p" + partnum++;
}
// APCSA doesn't need to split files right now
// parse quizes separately.
var quizzes = $('div.assessment-data');
if (quizzes.length > 0) {
// quiz
var xml = assessmentData2EdxProblem($, quizzes[0]); // TODO only one in apcsa right now
edx_util.addQuizEdxFormatComponent(xml, get_part_basename());
} else {
// html
var proc_html = processHTML(html, true);
edx_util.addHTMLComponenet(proc_html, title, get_part_basename());
}
edx_util.endVertical();
}
//TODO use cheerio rather than python craziness
function assessmentData2EdxProblem($, assessData) {
var qzHTML = $.html(assessData); // like a call to outerHTML()
//console.log("ASSDAT2EDX : qzhtml-> " + qzHTML);
var command;
//var exec = "python3";
var cmd = "/Anaconda3/python.exe";
// pass in qzHTML as string
// TODO replace doublequote with singlequote in qzHTML, just in case.
// or escape single quotes.
// command = cmd + ' code/mc_parser.py \'' + qzHTML + '\'';
// write ass-data to file, rather than pass in as string. Overwrite whatever's there.
var filename = "./apcsa_edx_out/CUR_ASSESSMENT_DATA.html";
fs.writeFileSync(filename, qzHTML);
command = cmd + ' code/mc_parser.py ' + filename;
var xml = exec(command).toString(); // returns a buffer
return xml;
}
///////////////////////////// PARTS
function processItem (item, options) {
return PROCESS_FUNCTIONS[item.type].call(null, item.content, options);
}
function processQuiz (quiz) {
return quiz;
}
function processMarkdown (file) {
return file;
}
function processFile (file, options) {
// FIXME -- this is a simplification for now.
return processHTML(file, options);
}
function processExternal (item, options) {
return item;
}
/** Does the work to modify a bunch of things to prep for edX
*
* @param {Cherrio-Object} The contents of the html file
*
*/
function processHTML (html) {
$ = cheerio.load(html);
// OLD BJC STUFF
// // Fix some of the EDC image elements with .button
// // These conflict with edX.
// $('.button').removeClass('button');
/////////// FROM apcsa_process_html.js, curriculum.js
var WRAPCLASS = "apcsa_full";
var WRAPCLASS_SEL = "." + WRAPCLASS;
// make the wrap class div
$.root().prepend('<div class="' + WRAPCLASS +'"></div>');
// move contents into the wrapclass div
var bodycontents = $("body").html();
$(WRAPCLASS_SEL).html(bodycontents);
// title -- apcsa is a title tag in head
var title = $("title").text();
$(WRAPCLASS_SEL).prepend('<div class="header">' + title + '</div>');
// remove the (empty?) html/head/body -- do them all in case some are missing
$("head").remove();
$("body").remove();
$("html").remove();
// add scripttag script
$(WRAPCLASS_SEL).prepend('<script type="text/javascript" src="/static/apcsa_process_html.js"></script>');
// add css tagcss
$(WRAPCLASS_SEL).prepend(cssString);
// fix up contents
// Fix image URLs
var prepend_str_find = '/apcsa/r/static/';
var prepend_str_replace = '/static/';
var imgregex_find = /\//g; // finds every /
var imgregex_replace = "_";
$('img').each(function (index, elm) {
var url = $(elm).attr('src');
if (url.indexOf(prepend_str_find) == 0) {
url = url.slice(prepend_str_find.length); // lose that
url = url.replace(imgregex_find, imgregex_replace);
url = prepend_str_replace + url;
} else {
DEBUG("img src didn't have prepend str I expected, but rather: " + url);
}
//$(elm).attr('src', util.transformURL(BASEURL + "/static", relPath, url));
$(elm).attr('src', url);
});
// HACKOLA - put in stub
//return getTestHTMLPage();
return ($.root().html());
}
function getTestHTMLPage() {
var str = '<div class="apcsa_full"> <link rel="stylesheet" type="text/css" media="screen" href="/static/css_ucb_apcsa.css" /> <div class="header" audio="audio_header">STUB PAGE RIGHT NOW, TESTING</div> <div id="marginCol"><div class="vocab"><a href="http://veritas.eecs.berkeley.edu/apcsa-ret/page/glossary.php?term=uniform distribution" target="_vocab">uniform distribution</a></div></div><div id="mainCol"> <p audio="2">The way to look at many, many rolls is with a chart. The <tt>lesson10/Dice2</tt> project contains a bar chart class that can show the roll of a single die. A good random number generator will make a <span class="vocab" term="uniform distribution">uniform distribution</span>, or a set of numbers in which any particular number is as likely as any other. </p> <div class="openbluejproj" project="c2/random/Dice2"> <p audio="1">Open the current project in BlueJ, and use the <tt>DieRollGenerator</tt> and <tt>BarChart6</tt> classes to look at many, many rolls of your dice generator. (<b>random/Dice2</b>). </p> </div> <br><div class="imageCenter"> <img src="/static/art_scenes_random_dice.png" alt="Twitterton in trouble"></div> </div></div>';
return str;
}
//////////////////////////// done
module.exports = "non requireable, you spud";
console.log('This conversion is done!');