repochecker/grammalecte: Check resolution.md
This commit is contained in:
parent
1f3f0fd55b
commit
80422daffb
4 changed files with 232 additions and 19 deletions
85
repochecker/grammalecte/markdown.go
Normal file
85
repochecker/grammalecte/markdown.go
Normal file
|
@ -0,0 +1,85 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
|
||||
"srs.epita.fr/fic-server/admin/sync"
|
||||
"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"
|
||||
)
|
||||
|
||||
func GrammalecteCheckMDText(str string, exceptions *sync.CheckExceptions) (errs []error) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
|
||||
errs = append(errs, checker.errs...)
|
||||
errs = append(errs, voidRenderer.Errors()...)
|
||||
errs = append(errs, grammalecte("", buf.String(), 0, exceptions, &CommonOpts)...)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
type grammarChecker struct {
|
||||
exceptions *sync.CheckExceptions
|
||||
errs []error
|
||||
}
|
||||
|
||||
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 {
|
||||
t.errs = append(t.errs, grammalecte("", string(child.Title), 0, t.exceptions, &CommonOpts)...)
|
||||
}
|
||||
case *ast.Link:
|
||||
if len(child.Title) > 0 {
|
||||
t.errs = append(t.errs, grammalecte("", string(child.Title), 0, t.exceptions, &CommonOpts)...)
|
||||
}
|
||||
}
|
||||
|
||||
return ast.WalkContinue, nil
|
||||
})
|
||||
}
|
Reference in a new issue