-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGiftParser.js
More file actions
170 lines (145 loc) · 5.52 KB
/
GiftParser.js
File metadata and controls
170 lines (145 loc) · 5.52 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import * as fs from "fs";
export function readGiftFile(file) {
const content = fs.readFileSync(file, 'utf8');
const rawBlocks = content.split(/\n::/);
const questions = rawBlocks.map((block, index) => {
if (index === 0 && !block.startsWith("::")) {
return null;
}
const fullBlock = block.startsWith("::") ? block :"::" + block;
return fullBlock.trim();
}).filter(Boolean);
return questions;
}
export function parseGiftQuestion(block) {
const raw = block;
const titleMatch = block.match(/^::([^:]+)::/);
const title = titleMatch ? titleMatch[1].trim() : "Untitled";
let rest = block.replace(/^::[^:]+::/, "").trim();
const rawAnswerBlocks = [...rest.matchAll(/\{([^}]*)\}/g)].map(m => m[1].trim());
let correctAnswers = [];
rawAnswerBlocks.forEach(block => {
const parts = block.split(/(?=[~=])/g);
parts.forEach(p => {
p = p.trim();
if (p.startsWith("=")) {
let answer = p.replace(/^=+/, "").trim();
answer = answer.replace(/^%?\d*%?/, "").trim();
correctAnswers.push(answer);
}
});
});
let text = rest.replace(/\{[^}]*\}/g, "(___)");
text = text.replace(/\[(html|plain|markdown|moodle)\]/gi, "");
let type = "unknown";
if (rawAnswerBlocks.length === 0) {
type = "text";
} else if (rawAnswerBlocks.some(a => a.includes("->"))) {
type = "matching";
} else if (rawAnswerBlocks.some(a => a.startsWith("T") || a.startsWith("F"))) {
type = "truefalse";
} else if (rawAnswerBlocks.some(a => a.includes("SA:") || /^\d+:/.test(a))) {
type = "cloze";
} else if (rawAnswerBlocks.some(a => a.includes("~"))) {
// Si distracteurs (~) présents, c'est un QCM
type = "multiplechoice";
} else if (rawAnswerBlocks.some(a => a.includes("="))) {
// Si seulement = (sans ~), c'est une réponse ouverte à taper
type = "shortanswer";
}
function parseChoiceParts(b) {
const out = [];
if (!b) return out;
let base = b.replace(/^\d+:[A-Za-z]+:/, '').trim();
const parts = base.split(/(?=[~=])/g);
for (let p of parts) {
p = p.trim();
if (!p) continue;
const correct = p.startsWith('=');
let txt = p.replace(/^=+/, '').replace(/^~+/, '').replace(/^%?\d+%?/, '').trim();
if (txt) {
out.push({ text: txt, correct });
}
}
return out;
}
let choices = null;
let matchingPairs = null;
if (type === 'multiplechoice') {
choices = [];
rawAnswerBlocks.forEach(b => {
const c = parseChoiceParts(b);
choices.push(...c);
});
} else if (type === 'cloze') {
choices = rawAnswerBlocks.map((b) => parseChoiceParts(b));
} else if (type === 'truefalse') {
choices = [];
rawAnswerBlocks.forEach(b => {
const opt = b.trim().toUpperCase();
if (/^T|TRUE/.test(opt)) choices.push({ text: 'True', correct: true });
else if (/^F|FALSE/.test(opt)) choices.push({ text: 'False', correct: false });
else {
if (opt.includes('T')) choices.push({ text: 'True', correct: true });
if (opt.includes('F')) choices.push({ text: 'False', correct: true });
}
});
} else if (type === 'matching') {
matchingPairs = [];
rawAnswerBlocks.forEach(b => {
const pairs = b.split(/(?==)/g).map(s=>s.trim()).filter(Boolean);
pairs.forEach(p => {
const match = p.match(/^=([^->]+)->(.+)$/);
if (match) matchingPairs.push({ left: match[1].trim(), right: match[2].trim() });
});
});
}
function buildDisplay() {
let out = '';
out += `${title} [${type}]\n`;
out += `${text}\n`;
if (type === 'multiplechoice' && Array.isArray(choices)) {
const labels = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
out += '\nOptions:\n';
choices.forEach((c, i) => {
const label = labels[i] || String(i+1);
const mark = c.correct ? ' (✓)' : '';
out += `${label}) ${c.text}${mark}\n`;
});
} else if (type === 'cloze' && Array.isArray(choices)) {
const labels = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
choices.forEach((blockChoices, bi) => {
out += `\nBlank ${bi+1}:\n`;
blockChoices.forEach((c, i) => {
const label = labels[i] || String(i+1);
const mark = c.correct ? ' (✓)' : '';
out += `${label}) ${c.text}${mark}\n`;
});
});
} else if (type === 'truefalse' && Array.isArray(choices)) {
out += '\nTrue/False:\n';
choices.forEach((c) => {
const mark = c.correct ? ' (✓)' : '';
out += `${c.text}${mark}\n`;
});
} else if (type === 'matching' && Array.isArray(matchingPairs)) {
out += '\nMatching pairs:\n';
matchingPairs.forEach((p, i) => {
out += `${i+1}) ${p.left} -> ${p.right}\n`;
});
}
return out.trim();
}
const display = buildDisplay();
return {
title,
text: text.trim(),
answers: rawAnswerBlocks,
correctAnswers,
type,
raw,
choices,
matchingPairs,
display
};
}