-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtxt2xml.awk
136 lines (129 loc) · 3.37 KB
/
txt2xml.awk
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
# ord.awk --- do ord and chr
# Global identifiers:
# _ord_: numerical values indexed by characters
# _ord_init: function to initialize _ord_
BEGIN { _ord_init(); num=0; }
function _ord_init( low, high, i, t)
{
low = sprintf("%c", 7) # BEL is ascii 7
if (low == "\a") { # regular ascii
low = 0
high = 127
} else if (sprintf("%c", 128 + 7) == "\a") {
# ascii, mark parity
low = 128
high = 255
} else { # ebcdic(!)
low = 0
high = 255
}
for (i = low; i <= high; i++) {
t = sprintf("%c", i)
_ord_[t] = i
}
}
function ord(str, c)
{
# only first character is of interest
c = substr(str, 1, 1)
return _ord_[c]
}
function outputmcfile() {
outputfile=sprintf("Problem%s_%03d.xml",FILENAME,num);
print "<problem display_name=\"Multiple Choice\" max_attempts=\"1\">" > outputfile;
printf(" <p>%s</p>\n",header) >> outputfile;
print " <multiplechoiceresponse>" >> outputfile;
gsub(/['"]/,"",header);
printf(" <choicegroup label=\"%s\" type=\"MultipleChoice\">\n",header) >> outputfile;
for (i=0;i<nchoices;i++) {
if (i==truechoice) {
printf(" <choice correct=\"true\">%s</choice>\n",choice[i]) >> outputfile;
} else {
printf(" <choice correct=\"false\">%s</choice>\n",choice[i]) >> outputfile;
}
}
print " </choicegroup>" >> outputfile;
print " </multiplechoiceresponse>" >> outputfile;
print "</problem>" >> outputfile;
close(outputfile);
}
function outputfbfile() {
if (tfmode) {
outputfile=sprintf("Problem%s_%03d.xml",FILENAME,num);
print "<problem display_name=\"Dropdown\" max_attempts=\"1\">" > outputfile;
printf(" <p>%s</p>\n",header) >> outputfile;
print " <optionresponse>" >> outputfile;
printf(" <optioninput options=\"('TRUE','FALSE')\" correct=\"%s\"/>\n",$0) >> outputfile;
print " </optionresponse>" >> outputfile;
print "</problem>" >> outputfile;
close(outputfile);
} else {
outputfile=sprintf("Problem%s_%03d.xml",FILENAME,num);
print "<problem display_name=\"Text Input\" max_attempts=\"1\">" > outputfile;
printf(" <p>%s</p>\n",header) >> outputfile;
printf(" <stringresponse answer=\"%s\" type=\"ci\">\n",$0) >> outputfile
gsub(/['"]/,"",header);
printf(" <textline label=\"%s\" size=\"40\"/>\n",header) >> outputfile
print " </stringresponse>" >> outputfile;
print "</problem>" >> outputfile;
close(outputfile);
}
}
/True\/False/ {
num=num-1;
startfbreading=1;
startmcreading=0;
tfmode=1;
}
/Multiple-Choice/ {
startfbreading=0;
startmcreading=1;
}
/Fill-in-the-Blank/ {
num=num-1;
startmcreading=0;
startfbreading=1;
tfmode=0;
}
/Short Answer/ {
startfbreading=0;
}
(startmcreading) {
if (/^[0-9]+\)/) {
num=num+1;
sub(/^[0-9]+\)/,"");
gsub(/^ /,"");
gsub(/ +/," ");
gsub(/ $/,"");
header="";
nchoices=0;
}
if ($0 ~ /^[A-Z]\)/) {
sub(/^.+\)/,"");
choice[nchoices]=$0;
nchoices++;
}
if (nchoices==0) {
header=sprintf("%s %s",header,$0);
}
if ($0 ~ /Answer/) {
truechoice=ord($2)-ord("A");
outputmcfile();
}
}
(startfbreading) {
if ($1*1!=0) {
num=num+1;
sub(/^[0-9]+\)/,"");
gsub(/^ /,"");
gsub(/ +/," ");
gsub(/ $/,"");
header="";
}
if ($0 ~ /Answer/) {
gsub(/Answer: +/,"");
outputfbfile();
} else {
header=sprintf("%s %s",header,$0);
}
}