-
Notifications
You must be signed in to change notification settings - Fork 0
/
text_to_html.py
executable file
·74 lines (63 loc) · 2.11 KB
/
text_to_html.py
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
#!/usr/bin/env python3
# wykys
import argparse
import codecs
import re
import sys
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='A program for analyzing a text file and generating an HTML file with questions.'
)
parser.add_argument(
'-f',
'--file',
dest='path',
action='store',
default='txt/test1.txt',
help='destination text with test',
)
with open('templates/top.html', 'r') as fr:
html_top = fr.read()
with open('templates/bot.html', 'r') as fr:
html_bot = fr.read()
try:
with open(parser.parse_args().path, 'r') as fr:
test = fr.readlines()
except UnicodeDecodeError:
try:
with codecs.open(parser.parse_args().path, 'r', 'utf-16') as fr:
test = fr.readlines()
except UnicodeDecodeError:
print('UnicodeDecodeError', file=sys.stderr)
html = html_top
start = False
cnt = 0
quest_cnt = 0
for line in test:
line = line.strip()
if len(line) > 0 and line[0] == '1':
start = True
cnt = 0
if len(line) == 0:
continue
if start:
cnt += 1
if cnt % 2 == 1:
line = re.sub(r'[ .]{2,}', ' ... ', re.sub(r'^\d+.?\s*', '', line))
if '...' not in line:
line += ' ... '
if not line.endswith('.'):
line = line.strip() + ' .'
question = line
else:
choice = re.sub('[A-D]. ', '\n', line).strip().split('\n')
quest = 4 * ' ' + 'quest(\"' + question + '\",\n ' + 4 * ' '
for s in choice:
quest += '[\"' + s.strip() + '\", \"\"],'
quest = quest[:-1] + '\n' + 4 * ' ' + ');\n'
quest_cnt += 1
html += '\n' + 4 * ' ' + '// QUEST {:3d}\n'.format(quest_cnt)
html += quest
html += html_bot
with open(parser.parse_args().path.split('.')[0] + '.html', 'w') as fw:
fw.write(html)