2022-11-24 12:10:19 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-11-24 12:11:21 +00:00
|
|
|
"fmt"
|
2022-11-24 12:10:19 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"srs.epita.fr/fic-server/admin/sync"
|
2022-11-24 18:41:45 +00:00
|
|
|
lib "srs.epita.fr/fic-server/repochecker/grammalecte/lib"
|
2022-11-24 12:10:19 +00:00
|
|
|
"srs.epita.fr/fic-server/repochecker/grammalecte/void"
|
|
|
|
|
|
|
|
"github.com/yuin/goldmark"
|
|
|
|
"github.com/yuin/goldmark/ast"
|
|
|
|
"github.com/yuin/goldmark/parser"
|
|
|
|
"github.com/yuin/goldmark/renderer"
|
|
|
|
"github.com/yuin/goldmark/text"
|
|
|
|
"github.com/yuin/goldmark/util"
|
2023-11-22 11:16:53 +00:00
|
|
|
"go.uber.org/multierr"
|
2022-11-24 12:10:19 +00:00
|
|
|
)
|
|
|
|
|
2023-11-22 11:16:53 +00:00
|
|
|
func GrammalecteCheckMDText(str string, lang string, exceptions *sync.CheckExceptions, forbiddenStrings ...string) (errs error) {
|
2023-01-17 17:26:04 +00:00
|
|
|
if !isRecognizedLanguage(lang) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-24 12:10:19 +00:00
|
|
|
if exceptions != nil {
|
|
|
|
for k := range *exceptions {
|
|
|
|
tmp := strings.SplitN(k, ":", 3)
|
|
|
|
if len(tmp) == 3 && tmp[1] == "quote" {
|
|
|
|
str = strings.Replace(str, tmp[2], "une citation a été remplacée ici", -1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-24 12:11:21 +00:00
|
|
|
for _, s := range forbiddenStrings {
|
|
|
|
if strings.Contains(str, s) {
|
|
|
|
if !exceptions.HasException(":not-forbidden-string:" + s) {
|
2024-05-18 01:17:15 +00:00
|
|
|
errs = multierr.Append(errs, fmt.Errorf("Forbidden string %q included in file content, don't write your flag in text", s))
|
2022-11-24 12:11:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-24 12:10:19 +00:00
|
|
|
checker := &grammarChecker{
|
|
|
|
exceptions: exceptions,
|
|
|
|
}
|
|
|
|
|
|
|
|
voidRenderer := void.NewVoidRenderer()
|
|
|
|
|
|
|
|
markdown := goldmark.New(
|
|
|
|
goldmark.WithParserOptions(
|
|
|
|
parser.WithASTTransformers(
|
|
|
|
util.Prioritized(checker, 200),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
goldmark.WithRenderer(
|
|
|
|
renderer.NewRenderer(
|
|
|
|
renderer.WithNodeRenderers(
|
|
|
|
util.Prioritized(voidRenderer, 30),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
if err := markdown.Convert([]byte(str), &buf); err != nil {
|
2023-11-22 11:16:53 +00:00
|
|
|
errs = multierr.Append(errs, err)
|
2022-11-24 12:10:19 +00:00
|
|
|
}
|
|
|
|
|
2023-11-22 11:16:53 +00:00
|
|
|
errs = multierr.Append(errs, checker.errs)
|
|
|
|
errs = multierr.Append(errs, voidRenderer.Errors())
|
2022-11-24 18:41:45 +00:00
|
|
|
|
2023-11-22 11:16:53 +00:00
|
|
|
for _, err := range multierr.Errors(grammalecte("", buf.String(), 0, exceptions, &CommonOpts)) {
|
2022-11-24 18:41:45 +00:00
|
|
|
if gerror, ok := err.(lib.GrammarError); ok {
|
|
|
|
if (gerror.RuleId == "redondances_paragraphe" || gerror.RuleId == "redondances_phrase") && gerror.GetPassage() == "SubstitutDeCode" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
} else if serror, ok := err.(lib.SpellingError); ok {
|
|
|
|
if serror.Value == "SubstitutDeCode" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-22 11:16:53 +00:00
|
|
|
errs = multierr.Append(errs, err)
|
2022-11-24 18:41:45 +00:00
|
|
|
}
|
2022-11-24 12:10:19 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type grammarChecker struct {
|
|
|
|
exceptions *sync.CheckExceptions
|
2023-11-22 11:16:53 +00:00
|
|
|
errs error
|
2022-11-24 12:10:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *grammarChecker) Transform(doc *ast.Document, reader text.Reader, pc parser.Context) {
|
|
|
|
ast.Walk(doc, func(node ast.Node, enter bool) (ast.WalkStatus, error) {
|
|
|
|
if !enter {
|
|
|
|
return ast.WalkContinue, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch child := node.(type) {
|
|
|
|
case *ast.Image:
|
|
|
|
if len(child.Title) > 0 {
|
2023-11-22 11:16:53 +00:00
|
|
|
t.errs = multierr.Append(t.errs, grammalecte("", string(child.Title), 0, t.exceptions, &CommonOpts))
|
2022-11-24 12:10:19 +00:00
|
|
|
}
|
|
|
|
case *ast.Link:
|
|
|
|
if len(child.Title) > 0 {
|
2023-11-22 11:16:53 +00:00
|
|
|
t.errs = multierr.Append(t.errs, grammalecte("", string(child.Title), 0, t.exceptions, &CommonOpts))
|
2022-11-24 12:10:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ast.WalkContinue, nil
|
|
|
|
})
|
|
|
|
}
|