-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestion-data-convert.html
134 lines (114 loc) · 4.17 KB
/
question-data-convert.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>PDF Question Converter</title>
<style>
body {
margin: 20px;
font-family: sans-serif;
}
textarea {
width: 100%;
height: 200px;
}
pre {
background: #f0f0f0;
padding: 10px;
white-space: pre-wrap;
}
button {
margin: 10px 0;
padding: 8px 16px;
cursor: pointer;
}
</style>
</head>
<body>
<h2>Paste your question text here:</h2>
<textarea id="inputText"></textarea>
<br />
<button onclick="convertQuestions()">Convert to questionPool</button>
<h3>Output (JS data):</h3>
<pre id="output"></pre>
<script>
function convertQuestions() {
const input = document.getElementById('inputText').value;
const lines = input.split('\n').map(line => line.trim()).filter(Boolean);
const questionPool = [];
let currentQuestion = null;
// Helper: finalize a question object (detect single vs multiple)
function finalizeQuestion() {
if (!currentQuestion) return;
// Determine question type
if (currentQuestion.correctAnswers.length === 1) {
currentQuestion.type = "single";
} else {
currentQuestion.type = "multiple";
}
questionPool.push(currentQuestion);
}
// We'll try to detect lines that match this pattern: "1. Some text"
// That indicates the start of a new question
const questionRegex = /^\d+\.\s+(.*)$/; // e.g. "1. Question text"
// Parse lines
lines.forEach(line => {
const questionMatch = line.match(questionRegex);
if (questionMatch) {
// Found a new question => finalize the previous question, then start a new one
finalizeQuestion();
currentQuestion = {
question: questionMatch[1].trim(),
options: [],
correctAnswers: []
};
} else if (currentQuestion) {
// Probably one of the bullet lines for an answer
// Usually: "• Some text" or "• Some text *"
// Let's strip leading "• " if present
let answerText = line.replace(/^•\s*/, "");
// Check if there's a trailing '*'
const starIndex = answerText.indexOf('*');
let isCorrect = false;
if (starIndex >= 0) {
isCorrect = true;
// Remove the asterisk (and anything after it if needed)
answerText = answerText.substring(0, starIndex).trim();
}
// Add to options
const optionIndex = currentQuestion.options.length;
currentQuestion.options.push(answerText);
// If it was correct, note the index
if (isCorrect) {
currentQuestion.correctAnswers.push(optionIndex);
}
}
});
// Final question after loop ends
finalizeQuestion();
// Show result
const outputEl = document.getElementById('output');
outputEl.textContent = "const questionPool = " + JSON.stringify(questionPool, null, 2) + ";";
}
/*
MIT License
Copyright (c) 2025 Veljko Vuckovic (refloow.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
</script>
</body>
</html>