-
Notifications
You must be signed in to change notification settings - Fork 1
/
quiz_questions.py
executable file
·114 lines (88 loc) · 2.47 KB
/
quiz_questions.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
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
#!/usr/bin/env python
import re
import sys
import json
#
# find all questions and metadata
#
q_rx = re.compile(r"""
# number of the question
(?P<number>\d+)\)
# Question being asked
(?P<prompt>.+\?)
# Possible answers to the question
(?P<answers>(?:\s+[-\*]\s.+)+)
# image to include in response
\s+(?P<image>\(image\).+)?
# credits to the image included in response
\s+(?P<image_credit>\(image_credit\).+)?
# Extra text to include in response
\s+(?P<text>(?![-\*]|^\d+\)|\s+\(image\)).+)?
""", re.X | re.M)
#
# question answer regex
#
answ_rx = re.compile(r"""
# indicator if correct or not
#
# (-) : incorrect
# (*) : correct
#
\s+(?P<correct>[-\*])
# actual text of answer
\s+(?P<answer>.+)
""", re.X | re.M)
def toJson(filename):
with open(filename, 'r') as quiz_file:
quiz_text = quiz_file.read()
# find the parent url before comments are removed
url = re.search(r'^url:(.+)', quiz_text, re.M)
if url:
url = url.group(1).strip()
# find title for quiz
title = re.search(r'^#(.+)', quiz_text, re.M)
if title:
title = title.group(1).strip()
# remove comments from quiz file
# quiz_text = re.sub(r'//(.+)', "", quiz_text)
# find all questions
questions = [m.groupdict() for m in q_rx.finditer(quiz_text)]
results = []
for q in questions:
out = {}
out['prompt'] = q['prompt'].strip()
out['number'] = int(q['number'])
if q['image']:
out['image'] = q['image'].replace('(image)', "").strip()
if q['image_credit']:
out['image_credit'] = q['image_credit'].replace('(image_credit)', "").strip()
# correct answer info
out['correct'] = {}
if q['text']:
out['correct']['text'] = q['text'].strip()
answers = [m.groupdict() for m in answ_rx.finditer(q['answers'])]
out['answers'] = []
for i, a in enumerate(answers):
out['answers'].append(a['answer'].strip())
if a['correct'] == "*":
out['correct']['index'] = i
results.append(out)
with open(filename.split('.')[0] + ".json", 'w') as outfile:
json.dump(
{
"questions" : sorted(results, key=lambda x: x['number']),
"title" : title,
"url" : url
},
outfile,
sort_keys=True,
indent=2,
separators=(',', ': ')
)
if __name__ == '__main__':
if len(sys.argv) == 1:
print("no quiz file given to script...")
else:
quiz_file = sys.argv[1]
print("generating {}.json".format(quiz_file.split('.')[0]))
toJson(quiz_file)