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
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
import sys, subprocess
from path import path
MENUS = path('/home/tony/Desktop/git/generate/menus')
#
def getTag(txt, tag):
start = txt.find(tag)+len(tag)+1
end = txt[start:].find(' ')+start
if end < start:
end = len(txt)
return txt[start:end]
def getImageData(image):
#parse image:format: 1_yyyy.png height xxx width yyy left
#get image number
end = image.find('_')
imgno = image[:end]
#get image name
start = image.find('_')
end = image.find(' ')
name = image[start+1:end]
#get height
height = getTag(image, 'height')
width = getTag(image, 'width')
if 'left' in image:
ifloat = 'left'
elif 'right'in image:
ifloat = 'right'
else:
ifloat = None
return (imgno, name, height, width, ifloat)
def makeQuiz(screen):
#scan for quiz comments
tag = '<!--Q'
endTag = '-->'
quiz = []
if tag+endTag in screen:
return True, quiz
while tag in screen:
#comment = <!--Q:'question' A:'answer'-->
start = screen.find(tag)
end = screen.find(endTag)
question = screen[start+4:end]
if question:
quiz.append(question)
screen = screen[end+len(endTag):]
return False, quiz
def findImages(screen):
#process imagelist
imageList = []
tag = '<!--I'
endTag = '-->'
while tag in screen:
start = screen.find(tag)
end = screen.find(endTag)
comment = screen[start+len(tag):end]
if len(comment) > 0:
imageList.append(comment)
screen = screen[end+len(endTag):]
return imageList
def findAudio(screen):
#process audiolist
audioList = []
tag = '<!--'
tags = "ARSXH"
endTag = '-->'
while tag in screen:
start = screen.find(tag)
end = screen.find(endTag)
comment = screen[start+len(tag):end]
if len(comment) > 0 and comment[0] in tags:
audioList.append(comment)
screen = screen[end+len(endTag):]
return audioList
def generate_image(imageList):
#format: nn_img1.png height:xxx width:yyy left
txtout = ''
for line in imageList:
(imgno, name, height, width, ifloat) = getImageData(line)
real = path(name).namebase
txtout = txtout + " $('#I" + imgno + "')\n"
if ifloat:
txtout = txtout + " .addClass('image_"+ifloat+"')\n"
#txtout = txtout + " .append(karma.createImg('" + real + "'))\n"
txtout += " $('<img>',{\n"
txtout += " src:host+pth+'/"+name+"'\n })\n .appendTo('#I"+imgno+"')\n"
return txtout
def generate_load(pth):
lesson = ''
lesson = lesson + " $('<div id="
lesson = lesson + '"txtMain"/>'
lesson = lesson + "')\n"
lesson = lesson + " .appendTo('#content')\n"
lesson = lesson + " .load(host+'cgi-bin/getFile.py',\n"
if screen == 0:
lesson = lesson + " {'filename':" + pth + "/a.txt'},\n"
else:
lesson = lesson + " {'filename':" + pth + "/a" + str(screen) + ".txt'},\n"
lesson = lesson + " function(){\n"
return lesson
def generate_audio(audiolist):
#format A1_clip.ogg or R1_clip.ogg or X1_clip.ogg or S1_clip.txt or H1_clip.ogg
txtout = ''
for line in audiolist:
typ = line[0]
pos = line.find('_')
clip = line[pos+1:]
if typ == 'A':
txtout += " playAudio(pth+'"+clip+"')\n"
elif typ == 'X':
txtout += " playXoAudio(pth+'"+clip+"')\n"
elif typ == 'H':
txtout += " playHelp(pth+'"+clip+"')\n"
elif typ == 'S' and line[1] != 'G':
txtout += " sayText(pth+'"+clip+"')\n"
elif typ == 'R':
txtout += " recordAudio(pth+'"+clip+"')\n"
return txtout
def generateLessonCSS(imageList):
txtout = ''
for image in imageList:
(imgno, name, height, width, ifloat) = getImageData(image)
insert = "#I"+imgno+"{height:"+height+"px; width:"+width+"px; }\n\n"
txtout += insert
return txtout
def generateLessonKarma(imageList):
txtout = 'function lesson_karma(){\n return Karma({\n image:[\n'
for image in imageList:
(imgno, name, height, width, float) = getImageData(image)
txtout += "{name:'"+path(name).namebase+"', file:'"+name+"'},\n"
txtout += ' ],\n audio:[\n ]})};\n'
return txtout
def generateQuiz(activity, quizList):
for i in range(len(quizList)):
quiz = quizList[i]
if len(quizList) == 1:
txtout = 'var quiz = {\n'
else:
txtout = 'var quiz'+str(i+1)+' = {\n'
tflist = []
mclist = []
salist = []
#we need to make a list of the questions by type: multilist, tf, fill
#note: we should have cloze and sa should accept variant answers (e.g a/b where a, b are regular expressions
for question in quiz:
if "S:" in question:
mclist.append(question)
elif "T:" in question or 'F:' in question:
tflist.append(question)
else:
salist.append(question)
if mclist:
txtout += ' multiList:[\n'
for q in mclist:
#process question and add to txtout
apos = q.find('A:')
spos = q.find('S:')
ans = 'ans: ' + q[apos+2:spos] + ', '
sel = 'ansSel: [' + q[spos+2:] + ']'
ques = '{ques: ' + q[:apos] + ', '
txtout += ques + ans + sel + '},\n'
txtout += '],\n'
if tflist:
txtout += ' tf:[\n'
for q in tflist:
#process questionstring and add to quiztxt
apos1 = q.find('T:')
apos2 = q.find('F:')
ans = 'ans:' + q[apos1+2:apos2] + ', '
sel = 'ansSel:' + q[apos2+2:]
ques = '{ques: ' + q[:apos1] + ', '
txtout += ques + ans + sel + '},\n'
txtout += '],\n'
if salist:
txtout += ' fill:[\n'
for q in salist:
#process question string and add to quiztxt
apos = q.find('A:')
ans = 'ans: ' + q[apos + 2:]
ques = '{ques: ' + q[2:apos] + ','
txtout += ques + ans + '},\n'
txtout += ' ]\n'
txtout += '};\n\n'
txtout += 'var options = {\n'
txtout += " title: '" + activity.namebase + "',\n"
txtout += ' random: false,\n'
txtout += ' allrandom: false,\n'
txtout += '};\n'
return txtout
def generateLesson(srcpth, pth, screens, imageList, audioList, quiz):
#generate lesson.js
tag = '<!--SG-->'
txtout = ''
lessonpth = srcpth / 'lesson.js'
if lessonpth.exists():
fin = open(lessonpth, 'r')
txtout += fin.read()
txtout += '\n'
fin.close()
if len(screens) == 1:
#single screen
txtout += 'function initialize(karma) {\n'
txtout += " host = 'http://localhost:8008/'\n"
txtout += " pth='"+pth+"/'\n"
txtout += " if(mode=='Faculty'){\n $('#ĺinkEdit').addClass('linkEdit');\n"
txtout += " }else{\n"
txtout += " $('#linkApply')\n .addClass('linkApply')\n .attr('score','70');\n }\n"
txtout += " $('<div id="
txtout += '"txtMain"/>'
txtout += "')\n"
txtout += " .appendTo('#content')\n"
txtout += " .load(host+'cgi-bin/getFile.py',\n"
txtout += " {'filename':pth+'a.txt'},\n"
txtout += " function(){\n"
if imageList and imageList[0]:
txtout += generate_image(imageList[0])
if audioList and audioList[0]:
txtout += generate_audio(audioList[0])
txtout += ' });\n'
if tag in screens[0]:
txtout += " $('#linkStart')\n"
txtout += " .addClass('linkStart')\n"
txtout += " .click(function(){startGame(karma);\n"
txtout += " });\n"
txtout += '};\n\n'
if not 'startGame' in txtout:
txtout += 'function startGame(karma) {\n'
if quiz:
txtout += " $('#linkStart').addClass('linkStart');\n"
txtout += " $('<div id=" + '"quizArea"/>' + "')\n"
txtout += ' .appendTo("#content")\n'
txtout += ' $("#quizArea")\n'
txtout += ' .jQuizMe(quiz, options)\n'
txtout += '};\n\n'
txtout += 'setUpLesson(initialize, startGame);\n'
else:
#multiple screen
txtout += 'var currentScreen;\n\n'
for i in range(len(screens)):
screen = i+1
txtout += 'function generateScreen'+str(screen)+'(karma) {\n'
txtout += ' currentScreen = ' + str(i+1) + '\n'
txtout += " host = 'http://localhost:8008/'\n"
txtout += " pth='"+pth+"/'\n"
txtout += " if(mode=='Faculty'){\n $('#ĺinkEdit').addClass('linkEdit');\n"
txtout += " }else{\n"
txtout += " $('#linkApply')\n .addClass('linkApply')\n .attr('score','70');\n }\n"
txtout += " $('<div id="
txtout += '"txtMain"/>'
txtout += "')\n"
txtout += " .appendTo('#content')\n"
txtout += " .load(host+'cgi-bin/getFile.py',\n"
txtout += " {'filename':pth+'a" + str(screen) + ".txt'},\n"
txtout += " function(){\n"
if imageList[i]:
txtout += generate_image(imageList[i])
txtout += ' });\n'
if audioList[i]:
txtout += generate_audio(audioList[i])
if tag in screens[i]:
txtout += " $('#linkStart')\n"
txtout += " .addClass('linkStart')\n"
txtout += " .click(function(){startGame(karma);});\n"
txtout += '};\n'
txtout += '\n'
txtout += 'function initialize() {};\n\n'
if not 'startGame' in txtout:
txtout += 'function startGame(karma){\n'
if quiz:
if not srcpth / 'quiz.js':
txtout += " url = window.location+'';\n"
txtout += " tmp = url.split('/');\n"
txtout += " tmp.pop();\n"
txtout += " tmp.shift();\n"
txtout += " tmp.shift();\n"
txtout += " tmp.shift();\n"
txtout += " pth = tmp.join('/');\n"
txtout += " $('div id = " + '"workArea"'+"'/>)\n"
txtout += " .load(host+'cgi/writeQuiz.py',\n"
txtout += " {'activity':pth,'text':quiztxt});\n"
txtout += " $('#linkStart').addClass('linkStart');\n"
txtout += " $('<div id=" + '"quizArea"/>' + "')\n"
txtout += ' .appendTo("#content")\n'
txtout += ' $("#quizArea")\n'
txtout += ' .jQuizMe(quiz[currentScreen], options)\n'
txtout += '};\n\n'
txtout += '\nsetUpMultiScreenLesson([\n'
for i in range(len(screens)):
txtout += ' generateScreen' + str(i+1) + ',\n'
txtout += ']);\n'
return txtout
def makeScreens(txt):
screens = []
tag = '<hr />'
while tag in txt:
pos = txt.find(tag)
screen = txt[:pos]
txt = txt[pos+len(tag):]
screens.append(screen)
if len(txt)>0:
screens.append(txt)
return screens
def addSpans(screen):
tag = '<!--I'
endTag = '-->'
start = 0
while screen[start:].find(tag)>-1:
pos1 = screen[start:].find(tag)
pos2 = screen[start:].find(endTag)
end = start + pos2
comment = screen[start+pos1+len(tag):end]
if len(comment) > 0:
imgno, name, height, width, ifloat = getImageData(comment)
span = "<span id = 'I" + imgno + "'></span>"
screen = screen[:end+len(endTag)] + span + screen[end+len(endTag):]
end = end + len(span)
start = end + len(endTag)
return screen
def generateBasicLesson(actpth, srcpth, dstpth):
#read source.txt
txtpth = srcpth / 'source.txt'
fin = open(txtpth,'r')
txt = fin.read()
fin.close()
#copy index.html to dst
subprocess.call('cp ' + MENUS / 'index.html ' + dstpth, shell=True)
#copy source.txt to dst
subprocess.call('cp ' + srcpth / 'source.txt ' + dstpth, shell=True)
#copy assets to dst
images = path(srcpth).files('*.png')
for image in images:
subprocess.call('cp ' + image + ' ' + dstpth, shell=True)
images = path(srcpth).files('*.ogg')
for image in images:
subprocess.call('cp ' + image + ' ' + dstpth, shell=True)
#we don't copy gif because Karma doesn't handle them - they need to be converted to png
#subprocess.call('cp ' + srcpth / '*.gif ' + dstpth, shell = True)
#scan source text returning list of screens (a*.txt)
screens = makeScreens(txt)
quiz = []
imageList = []
audioList = []
quizFlag = False #True when activity contains generated quiz
for i in range(len(screens)):
screen = screens[i]
flag, quizText = makeQuiz(screen)
if flag:
quizFlag = True
if quizText:
quiz.append(quizText)
imageList.append(findImages(screen))
audioList.append(findAudio(screen))
#write screen
#but first add spans for images
screen = addSpans(screen)
if len(screens) == 1:
fout = open(dstpth / 'a.txt','w')
else:
fout = open(dstpth / 'a' + str(i+1) + '.txt','w')
fout.write(screen)
fout.close()
#create lesson.js
txtout = generateLesson(srcpth, actpth, screens, imageList, audioList, quiz)
fout = open(dstpth / 'lesson.js','w')
fout.write(txtout)
fout.close()
if imageList:
totalList = []
for list in imageList:
for image in list:
totalList.append(image)
#create lesson-karma.js
txtout = generateLessonKarma(totalList)
fout = open(dstpth / 'lesson-karma.js','w')
fout.write(txtout)
fout.close()
#create lesson.css
txtout = generateLessonCSS(totalList)
fout = open(dstpth / 'lesson.css','w')
fout.write(txtout)
fout.close()
if quiz and not quizFlag:
#create quiz.js
txtout = generateQuiz(dstpth, quiz)
fout = open(dstpth / 'quiz.js','w')
fout.write(txtout)
fout.close()
|