More fool-proof parsing

This commit is contained in:
Oskar Winkels 2020-01-27 20:20:54 +01:00 committed by Oskar
parent 574726cf9d
commit 52592defe6
2 changed files with 51 additions and 20 deletions

View File

@ -6,12 +6,13 @@ Tool for fs teams to train for registration qualification quizzes
## ToDo ## ToDo
- Show answers and correctness - Show answers and correctness
- Check for invalid input - Server systemd service file
## Long term plans ## Long term plans
- Other versioons but FSCzech: - Other versions but FSCzech:
- FSG - FSG
- FSA - FSA
- FSEast - FSEast
- FSSpain
- Penalties/Bonuses - Penalties/Bonuses

View File

@ -162,28 +162,56 @@ function parseLine(line) {
// replace newline markers with html newlines // replace newline markers with html newlines
var els = line.replace(/⎊/g, '<br>').split('\t') var els = line.replace(/⎊/g, '<br>').split('\t')
if (els.length < 4) { var q = {}
alert('Information missing. At least 4 columns needed.')
if (els[0] != '') {
if ( els[0] == 'ChooseOne' ||
els[0] == 'ChooseAny' ||
els[0] == 'Text') {
q.type = els[0]
} else {
alert('Invalid type given on one line')
return null
}
} else {
alert('No type given on one line')
return null return null
} }
var type = els[0] if (els[1]) {
q.question = els[1]
} else {
alert('No question given on one line')
return null
}
// For multiple-choice, split lines into array // For multiple-choice, split lines into array
var choices = els[2].split('<br>') if (q.type == 'ChooseOne' || q.type == 'ChooseAny') {
if (els[2] || els[2] != '') {
// If multiple answers are allowed, they are ampersand-separated q.choices = els[2].split('<br>')
var answers = (els[3]) ? els[3].split('<br>') : null } else {
alert('"Choose" question but no choices given at question "'
return { + q.question + '"')
question: els[1] || '[No question provided]', return null
type: type,
choices: choices,
answers: answers,
explanation: els[4] || '[No explanation provided]',
author: els[5] || '[No author provided]',
picture: els[6] || null,
} }
}
// If multiple answers are allowed, they are newline-separated
// Also filter out empty lines because some people can't use spreadsheets
if (els[3] || els[3] != '') {
q.answers = els[3].split('<br>').filter(el => el != "");
} else {
alert('No answers given at question "'
+ q.question + '"')
return null
}
// Optional parameters
q.explanation = els[4] || '[No explanation provided]'
q.author = els[5] || '[No author provided]'
q.picture = els[6] || null
return q
} }
@ -204,7 +232,7 @@ function parseSpreadsheet() {
* 6. Re-add outer tabs (<tab>a<specialchar>b<tab>) * 6. Re-add outer tabs (<tab>a<specialchar>b<tab>)
*/ */
var text = textEl.value.replace(/\t"([^"\n]|"")+\n([^"]|"")+"\t/g, var text = textEl.value.replace(/\t"([^"\n]|"")+\n([^"]|"")*"\t/g,
m => m.replace(/\n/g, '⎊') m => m.replace(/\n/g, '⎊')
.replace(/""/g, '"') .replace(/""/g, '"')
.replace(/^\t"(.*)"\t$/, '$1') .replace(/^\t"(.*)"\t$/, '$1')
@ -214,8 +242,10 @@ function parseSpreadsheet() {
state.questions = text.split('\n').map(parseLine) state.questions = text.split('\n').map(parseLine)
if (state.questions[0] == null) if (state.questions.some(el => el == null)) {
alert('Erroneous data. Please check and re-enter.')
return {success: false} return {success: false}
}
localStorage.setItem('state', JSON.stringify(state)) localStorage.setItem('state', JSON.stringify(state))