2023-04-18 10:59:36 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Quiz struct {
|
|
|
|
Question string `json:"Question"`
|
|
|
|
Answers []string `json:"Answers"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// App struct
|
|
|
|
type App struct {
|
|
|
|
ctx context.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewApp creates a new App application struct
|
|
|
|
func NewApp() *App {
|
|
|
|
return &App{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// startup is called when the app starts. The context is saved
|
|
|
|
// so we can call the runtime methods
|
|
|
|
func (a *App) startup(ctx context.Context) {
|
|
|
|
a.ctx = ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
// Greet returns a greeting for the given name
|
|
|
|
func (a *App) FetchQuizzes() []Quiz {
|
|
|
|
return []Quiz{
|
|
|
|
{
|
|
|
|
Question: "Quanto fa 1+1?",
|
|
|
|
Answers: []string{"2", "3", "4", "5"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Question: "Quanto fa 3·2?",
|
|
|
|
Answers: []string{"6", "3", "4", "5"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) Markdown(quiz Quiz) string {
|
|
|
|
question := fmt.Sprintf("# %s\n\n", quiz.Question)
|
|
|
|
answers := make([]string, len(quiz.Answers))
|
|
|
|
for i, answer := range quiz.Answers {
|
|
|
|
answers[i] = fmt.Sprintf("- %s", answer)
|
|
|
|
}
|
|
|
|
return question + strings.Join(answers, "\n")
|
|
|
|
}
|
2023-04-19 10:47:06 +02:00
|
|
|
|
|
|
|
func (a *App) ParseMarkdown(markdown string) Quiz {
|
|
|
|
lines := strings.Split(markdown, "\n")
|
|
|
|
question := ""
|
|
|
|
answers := []string{}
|
|
|
|
|
|
|
|
for _, line := range lines {
|
|
|
|
trimmedLine := strings.TrimSpace(line)
|
|
|
|
if trimmedLine == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(trimmedLine, "#") {
|
|
|
|
question = strings.TrimPrefix(trimmedLine, "#")
|
|
|
|
question = strings.TrimSpace(question)
|
|
|
|
} else if strings.HasPrefix(trimmedLine, "-") {
|
|
|
|
answer := strings.TrimPrefix(trimmedLine, "-")
|
|
|
|
answer = strings.TrimSpace(answer)
|
|
|
|
answers = append(answers, answer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Quiz{
|
|
|
|
Question: question,
|
|
|
|
Answers: answers,
|
|
|
|
}
|
|
|
|
}
|