-
Notifications
You must be signed in to change notification settings - Fork 1
/
apcsa_test.js
318 lines (259 loc) · 7.8 KB
/
apcsa_test.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
/** LLAB AUTOBUILD WIP SCRIPT
*
* TODO: This script is NOT FINISHED
* 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');
// 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.
// TODO: CONFIG THIS SHIT.
output = './apcsa_edx_out/';
BASEURL = '/bjc-r'; // MATCH LLAB.ROOTURL IN CURR REPO
if (true) {
output += 'U1/';
topic = 'nyc_bjc/1-intro-loops.topic';
} else {
output += 'U2/';
topic = 'nyc_bjc/2-conditionals-abstraction.topic';
}
var PROCESS_FUNCTIONS = {
file: processFile,
quiz: processQuiz,
markdown: processMarkdown,
external: processExternal
};
topic = fs.readFileSync(util.topicPath(curFolder, topic));
topic = topic.toString();
data = llab.parse(topic);
// GLOBAL -- FIXME
var relPath;
var count;
var dir;
var PETER = false;
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));
cssPath = util.edXPath('bjc-edx.css');
cssString = '<link rel="stylesheet" href="' + cssPath + '">\n\n';
function loadFile (path) {
}
data.topics.forEach(parseTopic);
function parseTopic (topic, args) {
topic.contents.forEach(parseSection, args);
}
function shouldParse (title) {
return title.indexOf('Programming Lab') == 0 ||
title.indexOf('Investigation') == 0;
}
function parseSection (section, skip) {
var title = section.title.trim();
if (!shouldParse(title)) {
console.log('skipping:', title);
return;
}
dir = output;
if (!PETER) {
dir += title;
}
// Make if it doesn't exist.
mkdirp.sync(dir);
count = 0;
results = [];
section.contents.forEach(function (item) {
// This also writes files...hmmm.
results.push(processCurriculumItem(item));
});
return results;
}
// This needs renamed...
function processCurriculumItem (item) {
if (!item.url) {
return;
} else if (item.url.indexOf(BASEURL) != 0) {
return;
}
count += 1;
file = item.url.replace(BASEURL, curFolder);
relPath = path.relative(curFolder, file);
console.log('FILE: ', file);
html = fs.readFileSync(file);
parts = splitFile(html, count, dir);
parts.forEach(function(part, index) {
var css = index == 0;
var data = processItem(part, css);
// part.path is a file name
console.log(dir);
var folder = dir + '/' + part.directory
console.log('WRITING CONTENT', folder + part.path);
mkdirp.sync(folder);
fs.writeFileSync(folder + part.path, data);
});
return 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, includeCSS) {
var outerHTML, wrap;
$ = cheerio.load(html);
// Fix some of the EDC image elements with .button
// These conflict with edX.
$('.button').removeClass('button');
// Fix image URLs
$('img').each(function (index, elm) {
var url = $(elm).attr('src');
$(elm).attr('src', util.transformURL(BASEURL, relPath, url));
});
// Fix Snap! run links.
console.log('Found ', $('a').length, ' ALL urls.');
console.log('Transforming ', $('a.run').length, ' STARTER FILE urls.');
$('a.run').each(function (index, elm) {
var url = $(elm).attr('href');
$(elm).attr('href', util.transformURL(BASEURL, relPath, url));
});
// Remove EDC's inline HTML comments. (Why is it there.....)
[
'.comment',
'.todo',
'.commentBig'
].forEach(function (sel) { $(sel).remove(); });
// wrap content in div.llab-full
wrap = '<div class="llab-full">CONTENT</div>';
outerHTML = wrap.replace(/CONTENT/, $.html());
if (includeCSS != false) {
outerHTML = cssString + outerHTML;
}
return outerHTML;
}
/** Split a single curriculum page into components to be in a vertical.
*
* @param {string} the raw HTML file to be processed
*/
function splitFile (html, page, dir) {
var $, output, title, quizzes, qzHTML, text;
output = [];
$ = cheerio.load(html);
// EDC Puts an <h2> at the beginning of every page.
title = $('h2').first().text();
text = $('body').html()
// parse quizes separately.
quizzes = $('div.assessment-data');
console.log('Found ', quizzes.length, ' quizzes.');
quizzes.each(function(index, elm) {
qzHTML = $.html(elm); // like a call to outerHTML()
command = 'python3 code/mc_parser.py \'' + qzHTML + '\'';
xml = exec(command).toString();
var idx = text.indexOf(qzHTML);
var before = text.slice(0, idx).trim();
if (before.length) {
num = output.length + 1;
file = page + '-' + num + '-' + title + '.html';
file = util.edXFileName(file);
output.push({
type: 'file',
title: num + '-' + title,
content: before,
directory: 'html/',
path: file
}); // part before quiz
}
num = output.length + 1;
file = page + '-' + num + '-' + title + '.xml';
file = util.edXFileName(file);
output.push({
type: 'quiz',
title: num + '-' + 'Quiz-'+ title,
content: xml,
directory: 'problem/',
path: file
}); // push quiz
text = text.slice(idx + qzHTML.length);
});
if (quizzes.length == 0) {
file = page + '-' + title + '.html';
file = util.edXFileName(file);
output.push({
type: 'file',
title: title,
content: text,
directory: 'html/',
path: file
});
}
return output;
}
module.exports = function(path, sectionName, directory) {
// Globals
PETER = true;
// util.topicPath(curFolder, path) == assuming we have some folder.
topic = fs.readFileSync(path).toString();
data = llab.parse(topic);
output = directory;
var topic, data, result;
data.topics.forEach(function (topic) {
topic.contents.some(function (section) {
var title = section.title.trim();
found = title.indexOf(sectionName) != -1;
if (found) {
tmp = parseSection(section);
result = tmp;
return true;
}
});
});
return result;
}
console.log('This conversion is done!');