Detect theme and exercice language at runtime (not stored)

This commit is contained in:
nemunaire 2023-01-17 18:26:04 +01:00
parent 99cc79421f
commit aa0e7406c1
17 changed files with 99 additions and 42 deletions

View file

@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"log"
"strings"
"unicode"
"srs.epita.fr/fic-server/admin/sync"
@ -11,7 +12,11 @@ import (
lib "srs.epita.fr/fic-server/repochecker/grammalecte/lib"
)
func GrammalecteCheckKeyFlag(flag *fic.FlagKey, raw string, exceptions *sync.CheckExceptions) (errs []error) {
func GrammalecteCheckKeyFlag(flag *fic.FlagKey, raw string, exercice *fic.Exercice, exceptions *sync.CheckExceptions) (errs []error) {
if !isRecognizedLanguage(exercice.Language) {
return
}
label, _, _, _ := flag.AnalyzeFlagLabel()
for _, err := range grammalecte("label ", label, -1, exceptions, &CommonOpts) {
if e, ok := err.(lib.GrammarError); ok && e.RuleId == "poncfin_règle1" {
@ -28,7 +33,11 @@ func GrammalecteCheckKeyFlag(flag *fic.FlagKey, raw string, exceptions *sync.Che
return
}
func GrammalecteCheckFlagChoice(choice *fic.FlagChoice, exceptions *sync.CheckExceptions) (errs []error) {
func GrammalecteCheckFlagChoice(choice *fic.FlagChoice, exercice *fic.Exercice, exceptions *sync.CheckExceptions) (errs []error) {
if !isRecognizedLanguage(exercice.Language) {
return
}
errs = append(errs, grammalecte("label ", choice.Label, -1, exceptions, &CommonOpts)...)
if len(errs) == 0 && !unicode.IsUpper(bytes.Runes([]byte(choice.Label))[0]) && !exceptions.HasException(":label_majuscule") {
@ -38,7 +47,11 @@ func GrammalecteCheckFlagChoice(choice *fic.FlagChoice, exceptions *sync.CheckEx
return
}
func GrammalecteCheckHint(hint *fic.EHint, exceptions *sync.CheckExceptions) (errs []error) {
func GrammalecteCheckHint(hint *fic.EHint, exercice *fic.Exercice, exceptions *sync.CheckExceptions) (errs []error) {
if !isRecognizedLanguage(exercice.Language) {
return
}
if len(hint.Title) > 0 {
errs = append(errs, grammalecte("title ", hint.Title, -1, exceptions, &CommonOpts)...)
if len(errs) == 0 && !unicode.IsUpper(bytes.Runes([]byte(hint.Title))[0]) && !exceptions.HasException(":title_majuscule") {
@ -52,8 +65,15 @@ func GrammalecteCheckHint(hint *fic.EHint, exceptions *sync.CheckExceptions) (er
}
func GrammalecteCheckGrammar(data interface{}, exceptions *sync.CheckExceptions) []error {
if s, ok := data.(string); ok {
return grammalecte("", s, 0, exceptions, &CommonOpts)
if s, ok := data.(struct {
Str string
Language string
}); ok {
if s.Language != "" && strings.HasPrefix(s.Language, "fr") {
return nil
}
return grammalecte("", s.Str, 0, exceptions, &CommonOpts)
} else {
log.Printf("Unknown data given to GrammalecteCheckGrammar: %T", data)
return nil

View file

@ -4,6 +4,7 @@ import (
"log"
"os"
"os/exec"
"strings"
"time"
"srs.epita.fr/fic-server/admin/sync"
@ -104,6 +105,11 @@ func runGrammalecteServer() error {
return nil
}
func isRecognizedLanguage(lang string) bool {
// Grammalecte can only check french texts
return lang == "" || strings.HasPrefix(lang, "fr")
}
func RegisterChecksHooks(h *sync.CheckHooks) {
if err := runGrammalecteServer(); err != nil {
log.Fatal("Unable to start grammalecte-server:", err)

View file

@ -17,7 +17,11 @@ import (
"github.com/yuin/goldmark/util"
)
func GrammalecteCheckMDText(str string, exceptions *sync.CheckExceptions, forbiddenStrings ...string) (errs []error) {
func GrammalecteCheckMDText(str string, lang string, exceptions *sync.CheckExceptions, forbiddenStrings ...string) (errs []error) {
if !isRecognizedLanguage(lang) {
return
}
if exceptions != nil {
for k := range *exceptions {
tmp := strings.SplitN(k, ":", 3)