sync: Extract function that import hints from importer
This commit is contained in:
parent
4039a394b5
commit
eb95d861d3
1 changed files with 75 additions and 55 deletions
|
@ -15,24 +15,24 @@ import (
|
||||||
_ "golang.org/x/crypto/blake2b"
|
_ "golang.org/x/crypto/blake2b"
|
||||||
)
|
)
|
||||||
|
|
||||||
// SyncExerciceHints reads the content of hints/ directories and import it as EHint for the given challenge.
|
func buildExerciceHints(i Importer, exercice fic.Exercice) (hints []fic.EHint, errs []string) {
|
||||||
func SyncExerciceHints(i Importer, exercice fic.Exercice) (errs []string) {
|
|
||||||
params, err := parseExerciceParams(i, exercice.Path)
|
params, err := parseExerciceParams(i, exercice.Path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errs = append(errs, fmt.Sprintf("%q: challenge.txt: %s", path.Base(exercice.Path), err))
|
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 {
|
for n, hint := range params.Hints {
|
||||||
|
h := fic.EHint{}
|
||||||
if hint.Title == "" {
|
if hint.Title == "" {
|
||||||
hint.Title = fmt.Sprintf("Astuce #%d", n+1)
|
h.Title = fmt.Sprintf("Astuce #%d", n+1)
|
||||||
} else {
|
} else {
|
||||||
hint.Title = fixnbsp(hint.Title)
|
h.Title = fixnbsp(hint.Title)
|
||||||
}
|
}
|
||||||
if hint.Cost <= 0 {
|
if hint.Cost <= 0 {
|
||||||
hint.Cost = exercice.Gain / 4
|
h.Cost = exercice.Gain / 4
|
||||||
|
} else {
|
||||||
|
h.Cost = hint.Cost
|
||||||
}
|
}
|
||||||
|
|
||||||
if hint.Filename != "" {
|
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))
|
errs = append(errs, fmt.Sprintf("%q: unable to import hint file %q: invalid string returned as filename", path.Base(exercice.Path), hint.Filename))
|
||||||
continue
|
continue
|
||||||
} else {
|
} else {
|
||||||
hint.Content = s
|
h.Content = s
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if hint.Content == "" {
|
} 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))
|
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
|
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))
|
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
|
// Import hint
|
||||||
if _, err := exercice.AddHint(hint.Title, hint.Content, hint.Cost); err != nil {
|
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))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue