sync: Use goldmark instead of blackfriday

This commit is contained in:
nemunaire 2022-05-19 12:45:32 +02:00
commit f690a4e1c8
5 changed files with 111 additions and 111 deletions

View file

@ -1,6 +1,7 @@
package sync
import (
"bytes"
"fmt"
"log"
"net/http"
@ -10,7 +11,7 @@ import (
"github.com/BurntSushi/toml"
"github.com/gin-gonic/gin"
"github.com/russross/blackfriday/v2"
"github.com/yuin/goldmark"
"srs.epita.fr/fic-server/libfic"
)
@ -123,10 +124,17 @@ func BuildExercice(i Importer, theme *fic.Theme, epath string, dmap *map[int64]*
errs = append(errs, fmt.Sprintf("%q: overview.txt: %s", edir, err))
} else {
e.Overview = fixnbsp(e.Overview)
e.Headline = string(blackfriday.Run([]byte(strings.Split(e.Overview, "\n")[0])))
if e.Overview, err = ProcessMarkdown(i, e.Overview, epath); err != nil {
errs = append(errs, fmt.Sprintf("%q: overview.txt: an error occurs during markdown formating: %s", edir, err))
}
var buf bytes.Buffer
err := goldmark.Convert([]byte(strings.Split(e.Overview, "\n")[0]), &buf)
if err != nil {
errs = append(errs, fmt.Sprintf("%q: overview.txt: an error occurs during markdown formating of the headline: %s", edir, err))
} else {
e.Headline = string(buf.Bytes())
}
}
e.Statement, err = getFileContent(i, path.Join(epath, "statement.txt"))

View file

@ -2,65 +2,99 @@ package sync
import (
"bufio"
"bytes"
"encoding/base32"
"os"
"path"
"regexp"
"strings"
"srs.epita.fr/fic-server/libfic"
"github.com/russross/blackfriday/v2"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer/html"
"github.com/yuin/goldmark/text"
"github.com/yuin/goldmark/util"
"golang.org/x/crypto/blake2b"
)
func ProcessMarkdown(i Importer, input string, rootDir string) (output string, err error) {
// Define the path where save linked files
hash := blake2b.Sum512([]byte(rootDir))
absPath := "$FILES$/" + strings.ToLower(base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(hash[:]))
// Process md
output = string(blackfriday.Run(
[]byte(input),
blackfriday.WithRenderer(blackfriday.NewHTMLRenderer(
blackfriday.HTMLRendererParameters{
AbsolutePrefix: absPath,
Flags: blackfriday.CommonHTMLFlags,
},
)),
))
markdown := goldmark.New(
goldmark.WithExtensions(extension.DefinitionList),
goldmark.WithExtensions(extension.Linkify),
goldmark.WithExtensions(extension.Strikethrough),
goldmark.WithExtensions(extension.Table),
goldmark.WithExtensions(extension.Typographer),
goldmark.WithParserOptions(
parser.WithASTTransformers(
util.Prioritized(NewImageImporterTransformer(i, rootDir, hash), 200),
),
),
goldmark.WithRendererOptions(
html.WithHardWraps(),
),
)
// Import files
var re *regexp.Regexp
re, err = regexp.Compile(strings.Replace(absPath, "$", "\\$", -1) + "/[^\"]+")
if err != nil {
var buf bytes.Buffer
context := parser.NewContext()
if err = markdown.Convert([]byte(input), &buf, parser.WithContext(context)); err != nil {
return
}
files := re.FindAllString(output, -1)
for _, filePath := range files {
iPath := strings.TrimPrefix(filePath, absPath)
dPath := path.Join(fic.FilesDir, strings.ToLower(base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(hash[:])), iPath)
if err = os.MkdirAll(path.Dir(dPath), 0755); err != nil {
return
}
var fdto *os.File
if fdto, err = os.Create(dPath); err != nil {
return
} else {
defer fdto.Close()
writer := bufio.NewWriter(fdto)
if err = getFile(i, rootDir+iPath, writer); err != nil {
os.Remove(dPath)
return
}
}
}
output = string(buf.Bytes())
// Trim output
output = strings.TrimSpace(output)
return
}
type imageImporterTransformer struct {
importer Importer
rootDir string
hash [blake2b.Size]byte
absPath string
}
func NewImageImporterTransformer(i Importer, rootDir string, hash [blake2b.Size]byte) parser.ASTTransformer {
absPath := "$FILES$/" + strings.ToLower(base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(hash[:]))
return &imageImporterTransformer{i, rootDir, hash, absPath}
}
func (t *imageImporterTransformer) 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:
iPath := string(child.Destination)
dPath := path.Join(fic.FilesDir, strings.ToLower(base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(t.hash[:])), iPath)
child.Destination = []byte(path.Join(t.absPath, string(child.Destination)))
if err := os.MkdirAll(path.Dir(dPath), 0755); err != nil {
return ast.WalkStop, err
}
if fdto, err := os.Create(dPath); err != nil {
return ast.WalkStop, err
} else {
defer fdto.Close()
writer := bufio.NewWriter(fdto)
if err := getFile(t.importer, path.Join(t.rootDir, iPath), writer); err != nil {
os.Remove(dPath)
return ast.WalkStop, err
}
}
}
return ast.WalkContinue, nil
})
}

View file

@ -1,6 +1,7 @@
package sync
import (
"bytes"
"fmt"
"image"
"image/jpeg"
@ -13,7 +14,7 @@ import (
"unicode"
"github.com/gin-gonic/gin"
"github.com/russross/blackfriday/v2"
"github.com/yuin/goldmark"
"golang.org/x/image/draw"
"srs.epita.fr/fic-server/libfic"
@ -143,7 +144,13 @@ func BuildTheme(i Importer, tdir string) (th *fic.Theme, errs []string) {
if err != nil {
errs = append(errs, fmt.Sprintf("%q: overview.txt: an error occurs during markdown formating: %s", tdir, err))
}
th.Headline = string(blackfriday.Run([]byte(th.Headline)))
var buf bytes.Buffer
err := goldmark.Convert([]byte(th.Headline), &buf)
if err != nil {
errs = append(errs, fmt.Sprintf("%q: overview.txt: an error occurs during markdown formating of the headline: %s", tdir, err))
} else {
th.Headline = string(buf.Bytes())
}
}
if i.exists(path.Join(tdir, "heading.jpg")) {