-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.js
84 lines (73 loc) · 2.45 KB
/
convert.js
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
const fs = require("fs/promises");
async function main() {
const file = "./data/was.gift";
const contents = await fs.readFile(file, { encoding: "utf-8" });
const json = parseGift(contents);
fs.writeFile("./data/was.json", JSON.stringify(json, null, 2));
}
/** This function is mostly AI generated */
function parseGift(giftString) {
// Split the string into separate questions
let questions = giftString.split("// question:");
// Initialize an array to hold the parsed questions
let parsedQuestions = [];
// Process each question
for (let i = 1; i < questions.length; i++) {
let question = questions[i];
// Extract the instruction
let instructionStart = question.indexOf("::Question::") + 12;
let instructionEnd = question.indexOf("{");
let instruction = question
.substring(instructionStart, instructionEnd)
.trim()
.replace("[html]", "");
// Extract the options and source
let optionsText = question.split("{")[1].split("}")[0].trim().split("\n");
let options = [];
let source = "";
let multiple = false;
for (let j = 0; j < optionsText.length; j++) {
if (optionsText[j] !== "") {
if (optionsText[j].trim().startsWith("####")) {
source += optionsText[j].substring(5).trim() + " ";
} else {
let text = optionsText[j].trim();
let isCorrect = text[0] === "=";
if (isCorrect) {
text = text.substring(1).trim();
} else if (text[0] === "~") {
text = text.substring(1).trim();
if (text.startsWith("%")) {
multiple = true;
isCorrect = true;
text = text.substring(text.indexOf("%", 1) + 1).trim();
}
}
if (text.includes("#")) {
let hashIndex = text.indexOf("#");
source += text.substring(hashIndex + 1).trim() + " ";
text = text.substring(0, hashIndex).trim();
}
options.push({
id: j,
text: text.replace(/\\/gm, ""),
isCorrect: isCorrect,
});
}
}
}
// Only add the parsed question to the array if it has at least one option
if (options.length > 0) {
parsedQuestions.push({
source: source.trim().replace(/\\/gm, ""),
question: {
instruction: instruction,
options: options,
multiple: multiple,
},
});
}
}
return parsedQuestions;
}
main();