quiz.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. package models
  2. import (
  3. "crypto/sha256"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "sort"
  8. "strings"
  9. "gopkg.in/yaml.v2"
  10. )
  11. type Quiz struct {
  12. Meta
  13. Hash string `json:"hash"`
  14. Question *Question `json:"question"`
  15. Answers []*Answer `json:"answers"`
  16. Tags []string `json:"tags" yaml:"-"`
  17. Correct *Answer `json:"correct"`
  18. CorrectPos uint // Position of the correct answer during quiz creation
  19. Type int `json:"type"`
  20. }
  21. func MarkdownToQuiz(quiz *Quiz, markdown string) error {
  22. meta, remainingMarkdown, err := ParseMetaHeaderFromMarkdown(markdown)
  23. if err != nil {
  24. return err
  25. }
  26. lines := strings.Split(remainingMarkdown, "\n")
  27. questionText := ""
  28. answers := []*Answer{}
  29. tags := make([]string, 0)
  30. for _, line := range lines {
  31. if strings.HasPrefix(line, "*") {
  32. answerText := strings.TrimPrefix(line, "* ")
  33. answer := &Answer{Text: answerText}
  34. answers = append(answers, answer)
  35. } else {
  36. if questionText != "" {
  37. questionText += "\n"
  38. }
  39. questionText += line
  40. }
  41. parseTags(&tags, line)
  42. }
  43. questionText = strings.TrimRight(questionText, "\n")
  44. if questionText == "" {
  45. return fmt.Errorf("Question text should not be empty.")
  46. }
  47. if len(answers) < 2 {
  48. return fmt.Errorf("Number of answers should be at least 2 but parsed answers are %d.", len(answers))
  49. }
  50. question := &Question{Text: questionText}
  51. quiz.Question = question
  52. quiz.Answers = answers
  53. quiz.Tags = tags
  54. if meta != nil {
  55. quiz.Meta = *meta
  56. }
  57. return nil
  58. }
  59. func QuizToMarkdown(quiz *Quiz) (string, error) {
  60. if quiz.Question == nil {
  61. return "", errors.New("Quiz should contain a question but it wasn't provided.")
  62. }
  63. if len(quiz.Answers) < 2 {
  64. return "", errors.New("Quiz should contain at least 2 answers but none was provided.")
  65. }
  66. quiz.Correct = quiz.Answers[quiz.CorrectPos]
  67. if quiz.Correct == nil {
  68. return "", errors.New("Quiz should contain a correct answer but not was provided.")
  69. }
  70. correctAnswer := "* " + quiz.Correct.Text
  71. var otherAnswers string
  72. for pos, answer := range quiz.Answers {
  73. if quiz.CorrectPos != uint(pos) {
  74. otherAnswers += "* " + answer.Text + "\n"
  75. }
  76. }
  77. markdown := quiz.Question.Text + "\n\n" + correctAnswer + "\n" + otherAnswers
  78. return markdown, nil
  79. }
  80. func (q *Quiz) GetHash() string {
  81. return q.calculateHash()
  82. }
  83. func (q *Quiz) Marshal() ([]byte, error) {
  84. result, err := QuizToMarkdown(q)
  85. return []byte(result), err
  86. }
  87. func (q *Quiz) Unmarshal(data []byte) error {
  88. return MarkdownToQuiz(q, string(data))
  89. }
  90. func (q *Quiz) calculateHash() string {
  91. result := make([]string, 0)
  92. result = append(result, q.Question.GetHash())
  93. for _, a := range q.Answers {
  94. result = append(result, a.GetHash())
  95. }
  96. orderedHashes := make([]string, len(result))
  97. copy(orderedHashes, result)
  98. sort.Strings(orderedHashes)
  99. return fmt.Sprintf("%x", sha256.Sum256([]byte(strings.Join(orderedHashes, ""))))
  100. }
  101. func ParseMetaHeaderFromMarkdown(markdown string) (*Meta, string, error) {
  102. reader := strings.NewReader(markdown)
  103. var sb strings.Builder
  104. var line string
  105. var err error
  106. for {
  107. line, err = readLine(reader)
  108. if err != nil {
  109. if err == io.EOF {
  110. break
  111. }
  112. return nil, "", err
  113. }
  114. if strings.TrimSpace(line) == "---" {
  115. break
  116. }
  117. }
  118. for {
  119. line, err = readLine(reader)
  120. if err != nil {
  121. if err == io.EOF {
  122. break
  123. }
  124. return nil, "", err
  125. }
  126. if strings.TrimSpace(line) == "---" {
  127. break
  128. }
  129. sb.WriteString(line)
  130. }
  131. if sb.String() == "" {
  132. return nil, markdown, nil
  133. }
  134. var meta Meta
  135. err = yaml.Unmarshal([]byte(sb.String()), &meta)
  136. if err != nil {
  137. return nil, markdown, err
  138. }
  139. remainingMarkdown := markdown[strings.Index(markdown, "---\n"+sb.String()+"---\n")+len("---\n"+sb.String()+"---\n"):]
  140. return &meta, remainingMarkdown, nil
  141. }
  142. func readLine(reader *strings.Reader) (string, error) {
  143. var sb strings.Builder
  144. for {
  145. r, _, err := reader.ReadRune()
  146. if err != nil {
  147. if err == io.EOF {
  148. return sb.String(), io.EOF
  149. }
  150. return "", err
  151. }
  152. sb.WriteRune(r)
  153. if r == '\n' {
  154. break
  155. }
  156. }
  157. return sb.String(), nil
  158. }
  159. func parseTags(tags *[]string, text string) {
  160. // Trim the following chars
  161. trimChars := "*:.,/\\@()[]{}<>"
  162. // Split the text into words
  163. words := strings.Fields(text)
  164. for _, word := range words {
  165. // If the word starts with '#', it is considered as a tag
  166. if strings.HasPrefix(word, "#") {
  167. // Check if the tag already exists in the tags slice
  168. exists := false
  169. for _, tag := range *tags {
  170. if tag == word {
  171. exists = true
  172. break
  173. }
  174. }
  175. // If the tag does not exist in the tags slice, add it
  176. if !exists {
  177. *tags = append(*tags, strings.TrimRight(word, trimChars))
  178. }
  179. }
  180. }
  181. }