|
| 1 | +{{- $question := .question -}} |
| 2 | +{{- $index := .index -}} |
| 3 | +{{- $quizFilePath := printf "%v" .filePath -}} |
| 4 | +{{- $questionKey := trim (printf "%v" (or $question.id (printf "question-%d" (add $index 1)))) " \n\r\t" -}} |
| 5 | +{{- $questionScope := printf "%s#question:%s" $quizFilePath $questionKey -}} |
| 6 | +{{- $questionType := lower (replace (printf "%v" (or $question.type "single-answer")) "_" "-") -}} |
| 7 | +{{- $options := slice -}} |
| 8 | +{{- $correctOptionIds := slice -}} |
| 9 | +{{- $optionIdMap := dict -}} |
| 10 | + |
| 11 | +{{- range $optionIndex, $option := ($question.options | default (slice)) -}} |
| 12 | + {{- $optionKey := trim (printf "%v" (or $option.id (printf "option-%d" (add $optionIndex 1)))) " \n\r\t" -}} |
| 13 | + {{- $canonicalOption := partial "test/normalize-question-option.html" (dict |
| 14 | + "index" $optionIndex |
| 15 | + "option" $option |
| 16 | + "questionScope" $questionScope |
| 17 | + ) -}} |
| 18 | + {{- $options = $options | append $canonicalOption -}} |
| 19 | + {{- $optionIdMap = merge $optionIdMap (dict $optionKey $canonicalOption.id) -}} |
| 20 | + {{- if $canonicalOption.isCorrect -}} |
| 21 | + {{- $correctOptionIds = $correctOptionIds | append $canonicalOption.id -}} |
| 22 | + {{- end -}} |
| 23 | +{{- end -}} |
| 24 | + |
| 25 | +{{- $correctAnswer := trim (printf "%v" (or $question.correctAnswer $question.correct_answer "")) " \n\r\t" -}} |
| 26 | +{{- if eq $correctAnswer "" -}} |
| 27 | + {{- if eq $questionType "multiple-answers" -}} |
| 28 | + {{- $correctAnswer = delimit $correctOptionIds "," -}} |
| 29 | + {{- else if gt (len $correctOptionIds) 0 -}} |
| 30 | + {{- $correctAnswer = index $correctOptionIds 0 -}} |
| 31 | + {{- end -}} |
| 32 | +{{- else if or (eq $questionType "multiple-answers") (eq $questionType "single-answer") -}} |
| 33 | + {{- $normalizedAnswers := slice -}} |
| 34 | + {{- range (split $correctAnswer ",") -}} |
| 35 | + {{- $answer := trim . " \n\r\t" -}} |
| 36 | + {{- if ne $answer "" -}} |
| 37 | + {{- if isset $optionIdMap $answer -}} |
| 38 | + {{- $normalizedAnswers = $normalizedAnswers | append (index $optionIdMap $answer) -}} |
| 39 | + {{- else -}} |
| 40 | + {{- $normalizedAnswers = $normalizedAnswers | append $answer -}} |
| 41 | + {{- end -}} |
| 42 | + {{- end -}} |
| 43 | + {{- end -}} |
| 44 | + {{- if eq $questionType "multiple-answers" -}} |
| 45 | + {{- $correctAnswer = delimit $normalizedAnswers "," -}} |
| 46 | + {{- else if gt (len $normalizedAnswers) 0 -}} |
| 47 | + {{- $correctAnswer = index $normalizedAnswers 0 -}} |
| 48 | + {{- else -}} |
| 49 | + {{- $correctAnswer = "" -}} |
| 50 | + {{- end -}} |
| 51 | +{{- end -}} |
| 52 | + |
| 53 | +{{- $canonicalQuestion := dict |
| 54 | + "id" (partial "academy/normalize-id.html" (dict "value" $questionKey "scope" $questionScope)) |
| 55 | + "text" (printf "%v" (or $question.text "")) |
| 56 | + "type" $questionType |
| 57 | + "marks" (int (or $question.marks 0)) |
| 58 | + "explanation" (printf "%v" (or $question.explanation "")) |
| 59 | + "caseSensitive" (or $question.caseSensitive $question.case_sensitive false) |
| 60 | + "options" $options |
| 61 | + "correctAnswer" $correctAnswer |
| 62 | +-}} |
| 63 | + |
| 64 | +{{- if or (eq $questionType "multiple-answers") (eq $questionType "single-answer") $question.multipleAnswers $question.multiple_answers -}} |
| 65 | + {{- $canonicalQuestion = merge $canonicalQuestion (dict |
| 66 | + "multipleAnswers" (or $question.multipleAnswers $question.multiple_answers (eq $questionType "multiple-answers")) |
| 67 | + ) -}} |
| 68 | +{{- end -}} |
| 69 | + |
| 70 | +{{- return $canonicalQuestion -}} |
0 commit comments