diff --git a/main.js b/main.js index a331806..961350c 100644 --- a/main.js +++ b/main.js @@ -743,6 +743,101 @@ function ReadNetDB(cwith, useNetDB) { } } +/* + * Returns a question database from the given data. + * Parameter should be raw read file in string with "\n"-s + * TODO: ??? -s are not listed as errors, tho works correctly + * */ +function ParseRawData(data) { + + const d = data.split("\n"); + const r = new QuestionDB(); + var logs = []; + var currSubj = ""; // the current subjects name + var ExpectedIdentifier = ['+', '?']; + let currQuestion = new Question(); + + var i = -1; + while (i < d.length) { + let currIdentifier; + let skipped = 0; + do { + if (skipped >= 1) + logs.push(i + ": " + d[i]); + i++; + if (i >= d.length) { + if (currQuestion.IsComplete()) + r.AddQuestion(currSubj, currQuestion); + return { + result: r, + logs: logs + }; + } + currIdentifier = d[i][0]; + skipped++; + } while (!ExpectedIdentifier.includes(currIdentifier) && i < d.length); + + let currData = d[i].substring(1).trim(); + + if (currIdentifier == '+') { + if (currQuestion.IsComplete()) + r.AddQuestion(currSubj, currQuestion); + currQuestion = new Question(); + currSubj = currData; + ExpectedIdentifier = ['?']; + continue; + } + + if (currIdentifier == '?') { + if (currQuestion.IsComplete()) { + r.AddQuestion(currSubj, currQuestion); + currQuestion = new Question(); + } + // overwriting is allowed here, bcus: + // ?????!> + currQuestion.Q = currData; + ExpectedIdentifier = ['!', '?']; + continue; + } + + if (currIdentifier == '!') { + // if dont have question continue + if (!currQuestion.HasQuestion()) + throw "No question! (A)"; + // dont allow overwriting + // ?!!!! + if (!currQuestion.HasAnswer()) { + currData = currData.replace("A helyes válaszok: ", ""); + currData = currData.replace("A helyes válasz: ", ""); + + currQuestion.A = currData; + } + ExpectedIdentifier = ['?', '>', '+']; + continue; + } + + if (currIdentifier == '>') { + // if dont have question or answer continue + if (!currQuestion.HasQuestion()) + throw "No question! (I)"; + if (!currQuestion.HasAnswer()) + throw "No asnwer! (I)"; + // dont allow overwriting + // ?!>>> + if (!currQuestion.HasImage()) { + currQuestion.I = JSON.parse(currData); + } + ExpectedIdentifier = ['?', '+']; + continue; + } + } + + return { + result: r, + logs: logs + }; +} + function Load(cwith) { var useNetDB = GM_getValue("useNetDB"); if (useNetDB != undefined && (useNetDB == 0 || useNetDB == 1)) @@ -765,7 +860,13 @@ function LoadMOTD(resource) { function NLoad(resource, cwith) { var count = -1; try { - var d = JSON.parse(resource); + var d = {}; + try { + d = JSON.parse(resource); + } catch (e) { + console.log("Old data, trying with old methods...."); + d = ParseRawData(resource).result; + } var r = new QuestionDB(); var rt = []; var allCount = -1; @@ -1125,6 +1226,8 @@ function GetQuiz() { //: }}} +//: Helpers {{{ + function SimplifyImages(imgs) { var questionImages = []; // the array for the image names in question for (var i = 0; i < imgs.length; i++) // going through all image @@ -1139,10 +1242,6 @@ function SimplifyImages(imgs) { return questionImages; } -//: }}} - -//: Minor logic stuff {{{ - // adds image names to image nodes function AddImageNamesToImages(imgs) { for (var i = 0; i < imgs.length; i++) // going through all image @@ -1235,7 +1334,6 @@ function RemoveLetterMarking(inp) { // highlights the possible solutions to the current question function HighLightAnswer(results, currQuestionNumber) { - // TODO try { if (results.length > 0) // if there are items in the result { @@ -1272,6 +1370,8 @@ function HighLightAnswer(results, currQuestionNumber) { //: }}} +//: }}} + //: Minor UI stuff {{{ // shows a message with "msg" text, "matchPercent" tip and transp, and "timeout" time @@ -1886,7 +1986,7 @@ function ShowHelp() { } //: }}} -// + // I am not too proud to cry that He and he // Will never never go out of my mind. // All his bones crying, and poor in all but pain, @@ -1902,10 +2002,10 @@ function ShowHelp() { // Out of his eyes I saw the last light glide. // Here among the liught of the lording sky // An old man is with me where I go -// + // Walking in the meadows of his son's eye // Too proud to cry, too frail to check the tears, // And caught between two nights, blindness and death. -// + // O deepest wound of all that he should die // On that darkest day.