Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/cvtSiyavula.py
blob: 6a6db141e9a113abb9c998545df8744c1b387c6e (plain)
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
#!/usr/bin/python
#create milestone of basic activities from Siyavula folder
#source folder has text files a01..a99.txt representing individual activities after conversion from 
#doc format. Folder also has all of the images for the milestone.
#
#for each folder in SOURCE create a milestone folder in TARGET
#for each a*.txt file in SOURCE folder create an activity folder in TARGET / milestone
#also add an entry in activities.js
#copy the a*.txt file to the activity folder as source.txt
#for each image tag in a*.txt, replace the tag with a comment <!--In_imgn.png height xxx width xxx left-->
#copy the source image to the activity folder renaming it imgn (where n is the number of the image in this activity)
from path import path
from PIL import Image
import subprocess
import sys
from optparse import OptionParser

SOURCE = path('../')
TARGET = path('../trial/')

def getField(element, tag):
    pos1 = element.find(tag)
    if pos1 < 0:
        return ''
    pos2 = element[pos1:].find(' ')
    if pos2 < 0:
        pos2 = len(element)
    field = element[pos1+len(tag):pos1+pos2].replace('"','')
    if tag == 'src=':
        pos1 = field.find('html_')
        field = '*' + field[pos1+len('html_'):]
    return field

def parseElement(count, element, folder, activityName):
    # <img src="Math_Gr1_m1_NUMBER_FUN_html_m3e0e7122.png" border="1" alt="" hspace="12" width="100" height="115" align="LEFT" />
    #need to build comment
    tgtimg = 'img' + str(count) + '.png'
    tgtpth = TARGET / folder /activityName / tgtimg
    searchImg = getField(element, 'src=')
    h = getField(element,'height=')
    w = getField(element,'width=')
    if len(h) < 1 or len(w) < 1:
        print element, 'h:',h,'w:',w
    align = getField(element,'align=')
    comment = '<!--I' + str(count) +'_' + tgtimg + ' height ' + h + ' width ' + w + ' ' + align.lower() + '-->'
    srcpth = SOURCE / folder
    files = srcpth.files(searchImg)
    srcimg = files[0].name
    try:
        im = Image.open(srcpth / srcimg)
        try:
            size = (int(w), int(h))
            imt = im.resize(size,Image.ANTIALIAS)
            imt.save(tgtpth)
        except:
            im.save(tgtpth,'png')
    except IOError:
        print 'cannot create thumbnail for', tgtpth, srcpth/srcimg
    return comment

def processImages(txt, folder, activityName):
    tag = '<img '
    endTag = '>'
    count = 0
    while tag in txt:
        count += 1
        start = txt.find(tag)
        end = start + txt[start:].find(endTag)
        element = txt[start+len(tag):end]
        comment = parseElement(count, element, folder, activityName)
        txt = txt[:start]+comment+txt[end+len(endTag):]
    return txt

#get command line options
parser = OptionParser(usage="Usage: %prog [options] file")
(options, args) = parser.parse_args()
if not args:
    print 'Specify a course (e.g. zs4) as argument.'
    parser.print_help()
    sys.exit(1)

SUBJECT = path(args[0])
COURSE = path(args[1])
if SUBJECT == 'siyavula_science':
    sbj = 'sci'
elif SUBJECT == 'siyavula_technology':
    sbj = 'tek'
elif SUBJECT == 'siyavula_mathematics':
    sbj = 'ma'
elif SUBJECT == 'siyavula_english':
    sbj = 'en'
else:
    print 'subject not known'
    sys.exit()

srcpth = SOURCE / SUBJECT / COURSE
tgtpth = TARGET / SUBJECT / COURSE
print 'pths', srcpth, tgtpth
#create clean output folder
subprocess.call('rm -rf ' + tgtpth, shell=True)
subprocess.call('mkdir -p ' + tgtpth, shell=True) 
milestones = srcpth.dirs()
milestones.sort()
ms_entries = []
mcount = 0
for milestone in milestones:
    subprocess.call('mkdir ' + tgtpth / milestone.namebase, shell=True)
    fin = open(srcpth / milestone.namebase / 'source.txt', 'r') 
    txt = fin.read()
    fin.close()
    entry = ['0',str(mcount),sbj,'milestone',milestone.namebase,'cyan']
    ms_entries.append(entry)
    mcount += 1
    acount = 0
    entries = []
    tag = '<hr />'
    done = False
    txtout = ''
    while not done:
        acount += 1
        acnt = str(acount)
        if len(acnt) < 2:
            acnt = '0' + acnt
        activityName = milestone.namebase + 'a' + acnt
        entry = [str(acount),str(acount),'Technology','basic',activityName,'blue']
        entries.append(entry)
        tgt = tgtpth / milestone.namebase / activityName
        print 'tgt', tgt
        subprocess.call('mkdir ' + tgt, shell=True) 
        pos = txt.find(tag)
        txtin = txt[:pos]
        txt = txt[pos+len(tag):]   
        folder = SUBJECT / COURSE / milestone.namebase
        txtout = processImages(txtin, folder, activityName)
        fout = open(tgt / 'source.txt','w')
        fout.write(txtout)
        fout.close()
        txtout = ''
        if not tag in txt:
            done = True
    #write out activities.js
    txtout = 'var activities = [\n'
    for entry in entries:
        txtout += str(entry) + ',\n'
    txtout += ']\n\n'
    fout=open(TARGET / folder / 'activities.js','w')
    fout.write(txtout)
    fout.close()
#also write out milestones.js
txtout = 'var activities = [\n'
for entry in ms_entries:
    txtout += str(entry) + ',\n'
txtout += ']\n\n'
fout = open(TARGET / SUBJECT / COURSE /  'milestones.js','w')
fout.write(txtout)
fout.close()