repochecker/grammalecte: Check for forbidden strings (raw flags) in resolution.md

This commit is contained in:
nemunaire 2022-11-24 13:11:21 +01:00
parent 80422daffb
commit 3421286c9b
4 changed files with 38 additions and 3 deletions

View File

@ -64,6 +64,17 @@ type ExerciceFlag struct {
NumberStep interface{} `toml:"step,omitempty"`
}
func (f ExerciceFlag) RawString() []string {
switch f.Raw.(type) {
case string:
return []string{f.Raw.(string)}
case []string:
return f.Raw.([]string)
default:
return nil
}
}
// ExerciceFlagChoice holds informations about a choice (for MCQ and UCQ).
type ExerciceFlagChoice struct {
ExerciceFlag
@ -83,6 +94,21 @@ type ExerciceParams struct {
FlagsUCQ []ExerciceFlag `toml:"flag_ucq"`
}
func (p ExerciceParams) GetRawFlags() (ret []string) {
for _, f := range append(p.Flags, p.FlagsUCQ...) {
raw := f.RawString()
if len(raw) > 0 {
ret = append(ret, raw...)
}
}
for _, f := range p.FlagsMCQ {
for _, c := range f.Choice {
ret = append(ret, c.Label)
}
}
return
}
// parseExerciceParams reads challenge definitions from defines.txt and extract usefull data to set up the challenge.
func parseExerciceParams(i Importer, exPath string) (p ExerciceParams, md toml.MetaData, err error) {
var defs string

View File

@ -289,7 +289,7 @@ func BuildExercice(i Importer, theme *fic.Theme, epath string, dmap *map[int64]*
} else {
// Call checks hooks
for _, h := range hooks.mdTextHooks {
for _, err := range h(e.Resolution, exceptions.GetFileExceptions("resolution.md")) {
for _, err := range h(e.Resolution, exceptions.GetFileExceptions("resolution.md"), p.GetRawFlags()...) {
errs = append(errs, NewExerciceError(e, fmt.Errorf("resolution.md: %w", err)))
}
}

View File

@ -16,7 +16,7 @@ type CheckFlagLabelHook func(*fic.FlagLabel, *CheckExceptions) []error
type CheckFlagMCQHook func(*fic.MCQ, []*fic.MCQ_entry, *CheckExceptions) []error
type CheckFileHook func(*fic.EFile, *CheckExceptions) []error
type CheckHintHook func(*fic.EHint, *CheckExceptions) []error
type CheckMDTextHook func(string, *CheckExceptions) []error
type CheckMDTextHook func(string, *CheckExceptions, ...string) []error
type CheckExerciceHook func(*fic.Exercice, *CheckExceptions) []error
type CustomCheckHook func(interface{}, *CheckExceptions) []error

View File

@ -2,6 +2,7 @@ package main
import (
"bytes"
"fmt"
"strings"
"srs.epita.fr/fic-server/admin/sync"
@ -15,7 +16,7 @@ import (
"github.com/yuin/goldmark/util"
)
func GrammalecteCheckMDText(str string, exceptions *sync.CheckExceptions) (errs []error) {
func GrammalecteCheckMDText(str string, exceptions *sync.CheckExceptions, forbiddenStrings ...string) (errs []error) {
if exceptions != nil {
for k := range *exceptions {
tmp := strings.SplitN(k, ":", 3)
@ -25,6 +26,14 @@ func GrammalecteCheckMDText(str string, exceptions *sync.CheckExceptions) (errs
}
}
for _, s := range forbiddenStrings {
if strings.Contains(str, s) {
if !exceptions.HasException(":not-forbidden-string:" + s) {
errs = append(errs, fmt.Errorf("Forbidden raw string %q included in file content, don't write it (:not-forbidden-string:%s)", s, s))
}
}
}
checker := &grammarChecker{
exceptions: exceptions,
}