sync: Extract function that import hints from importer

This commit is contained in:
nemunaire 2019-07-06 00:40:31 +02:00
parent 4039a394b5
commit eb95d861d3

View file

@ -15,24 +15,24 @@ import (
_ "golang.org/x/crypto/blake2b"
)
// SyncExerciceHints reads the content of hints/ directories and import it as EHint for the given challenge.
func SyncExerciceHints(i Importer, exercice fic.Exercice) (errs []string) {
func buildExerciceHints(i Importer, exercice fic.Exercice) (hints []fic.EHint, errs []string) {
params, err := parseExerciceParams(i, exercice.Path)
if err != nil {
errs = append(errs, fmt.Sprintf("%q: challenge.txt: %s", path.Base(exercice.Path), err))
return
}
if _, err := exercice.WipeHints(); err != nil {
errs = append(errs, err.Error())
} else {
for n, hint := range params.Hints {
h := fic.EHint{}
if hint.Title == "" {
hint.Title = fmt.Sprintf("Astuce #%d", n+1)
h.Title = fmt.Sprintf("Astuce #%d", n+1)
} else {
hint.Title = fixnbsp(hint.Title)
h.Title = fixnbsp(hint.Title)
}
if hint.Cost <= 0 {
hint.Cost = exercice.Gain / 4
h.Cost = exercice.Gain / 4
} else {
h.Cost = hint.Cost
}
if hint.Filename != "" {
@ -68,19 +68,39 @@ func SyncExerciceHints(i Importer, exercice fic.Exercice) (errs []string) {
errs = append(errs, fmt.Sprintf("%q: unable to import hint file %q: invalid string returned as filename", path.Base(exercice.Path), hint.Filename))
continue
} else {
hint.Content = s
h.Content = s
}
}
} else if hint.Content == "" {
errs = append(errs, fmt.Sprintf("%q: challenge.txt: hint %s (%d): content and filename can't be empty at the same time", path.Base(exercice.Path), hint.Title, n+1))
continue
} else if hint.Content, err = ProcessMarkdown(i, fixnbsp(hint.Content), exercice.Path); err != nil{
} else if h.Content, err = ProcessMarkdown(i, fixnbsp(hint.Content), exercice.Path); err != nil {
errs = append(errs, fmt.Sprintf("%q: challenge.txt: hint %s (%d): error during markdown formating: %s", path.Base(exercice.Path), hint.Title, n+1, err))
}
hints = append(hints, h)
}
return
}
// CheckExerciceHints checks if all hints are corrects..
func CheckExerciceHints(i Importer, exercice fic.Exercice) ([]fic.EHint, []string) {
return buildExerciceHints(i, exercice)
}
// SyncExerciceHints reads the content of hints/ directories and import it as EHint for the given challenge.
func SyncExerciceHints(i Importer, exercice fic.Exercice) (errs []string) {
if _, err := exercice.WipeHints(); err != nil {
errs = append(errs, err.Error())
} else {
hints, berrs := buildExerciceHints(i, exercice)
errs = append(errs, berrs...)
for n, hint := range hints {
// Import hint
if _, err := exercice.AddHint(hint.Title, hint.Content, hint.Cost); err != nil {
errs = append(errs, fmt.Sprintf("%q: challenge.txt: hint %s (%d): %s", path.Base(exercice.Path), hint.Title, n+1, err))
errs = append(errs, fmt.Sprintf("%q: hint #%d %s: %s", path.Base(exercice.Path), n+1, hint.Title, err))
}
}
}