Created
January 17, 2021 16:13
-
-
Save mcohen01/35a2fd6532b41da3bee2a25037ed2602 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def cai2php(lines): | |
import re | |
frames = [] | |
current_frame = None | |
for line in lines: | |
line = str(line, 'utf-8') | |
# first line of the frame text | |
if re.match("^[0-9]+\.", line): | |
current_frame = re.sub('^[0-9]+\.\s+', '', line.strip()) | |
# end of the frame text | |
elif re.match("^\.blank", line) and current_frame is not None: | |
frames[-1][0] = current_frame | |
current_frame = None | |
# tries | |
elif re.match("^@tries", line): | |
frames.append([None, None, line.replace('@tries ', '')]) | |
# correct answer | |
elif re.match("^@right", line): | |
frames[-1][1] = re.search('\*.*\*', line).group().replace('*', '') | |
# follow on lines of the frame text | |
elif current_frame is not None: | |
current_frame += ' ' + line.strip() | |
txt = '' | |
for f in frames: | |
txt += '@begin' + '\n' | |
txt += f[0] + '\n' | |
txt += '@end' + '\n' | |
txt += '@answer {}'.format(f[1].replace(',', ' ')) + '\n' | |
txt += '@tries {}'.format(f[2]) | |
txt += '@graphic none' + '\n' | |
txt += '@video none' + '\n' | |
txt += '\n' | |
return txt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment