-
Notifications
You must be signed in to change notification settings - Fork 0
/
answerMaker.js
95 lines (83 loc) · 2.89 KB
/
answerMaker.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
85
86
87
88
89
90
91
92
93
94
95
var autoCopy, autoFormat, autoClear, removeComments, removeWhitespaces, autoDetectLanguage, multipleAnswers;
function copyText(text) {
navigator.clipboard.writeText(text);
}
function printResults(results) {
document.getElementById("resultText").innerText = results.join("\n");
if (autoCopy) {
copyText(results.join("\n"));
}
return;
}
function autoProcessText() {
if (document.getElementById("autoProcess").checked) {
if (document.getElementById("code").value !== "") submit();
}
return;
}
function setOptionVaribles() {
var options = [
"autoCopy",
"autoFormat",
"autoClear",
"removeComments",
"removeWhitespaces",
"autoDetectLanguage",
"multipleAnswers",
];
options.forEach((v) => {
window[v] = document.getElementById(v).checked;
});
return;
}
function detectLanguage(text) {
var python = text.includes("input") + text.includes("open(0)");
var ruby = text.includes("gets") + text.includes("`dd`") + text.includes("$<");
if (python > 0 && ruby == 0) return "Python";
if (ruby > 0 && python == 0) return "Ruby";
alert("Unknown language");
return "Unknown";
}
async function submit() {
setOptionVaribles();
var language = document.getElementById("language").value;
var codeInput = document.getElementById("code").value;
if (!autoFormat && multipleAnswers) {
alert("You cannot have formating off with multiple answers");
return;
}
var answerList = [];
var languageList = [];
if (autoFormat) {
var formattedCode = `${JSON.stringify(String.raw`${codeInput}`)
.slice(1, -1)
.replace(/'/g, "\\'")}`;
if (removeComments) {
formattedCode = formattedCode.replace(/\\n# /g, "\\n");
}
if (multipleAnswers) {
var formattedSplit = formattedCode.split("\\n\\n");
formattedSplit.forEach((code) => {
var lang = detectLanguage(code);
if (lang === "Python" && removeWhitespaces) code = code.replace(/ /g, " ");
answerList.push(code.replace(/(\\n)*$/, ""));
languageList.push(lang);
});
} else {
var lang = detectLanguage(formattedCode);
if (lang === "Python" && removeWhitespaces) formattedCode = formattedCode.replace(/ /g, " ");
answerList.push(formattedCode);
languageList.push(lang);
}
} else {
answerList.push(codeInput.replace(/(?<!\\)'/g, "\\'"));
languageList.push(detectLanguage(codeInput));
}
//`{lang: '${language}', solution: '${formattedCode}'},`
var finalList = [];
answerList.forEach((ans, i) => {
finalList.push(`{lang: '${languageList[i]}', solution: '${ans}'},`);
});
printResults(finalList);
if (autoClear) document.getElementById("code").value = "";
}