sync: import files in markdown, relative to theme/exercice dir

This commit is contained in:
nemunaire 2018-12-02 16:24:33 +01:00
parent 8c95782eff
commit 87471acf98
6 changed files with 75 additions and 8 deletions

View File

@ -12,7 +12,6 @@ import (
"srs.epita.fr/fic-server/libfic" "srs.epita.fr/fic-server/libfic"
"gopkg.in/russross/blackfriday.v2"
_ "golang.org/x/crypto/blake2b" _ "golang.org/x/crypto/blake2b"
) )
@ -74,7 +73,7 @@ func SyncExerciceHints(i Importer, exercice fic.Exercice) (errs []string) {
errs = append(errs, fmt.Sprintf("%q: challenge.txt: hint %s (%d): content and filename can't be empty at the same time", path.Base(exercice.Path), hint.Title, n+1)) errs = append(errs, fmt.Sprintf("%q: challenge.txt: hint %s (%d): content and filename can't be empty at the same time", path.Base(exercice.Path), hint.Title, n+1))
continue continue
} else { } else {
hint.Content = string(blackfriday.Run([]byte(hint.Content))) hint.Content = ProcessMarkdown(i, hint.Content, exercice.Path)
} }
// Import hint // Import hint

View File

@ -114,8 +114,8 @@ func SyncExercices(i Importer, theme fic.Theme) []string {
} }
// Markdown pre-formating // Markdown pre-formating
statement = string(blackfriday.Run([]byte(statement))) statement = ProcessMarkdown(i, statement, edir)
overview = string(blackfriday.Run([]byte(overview))) overview = ProcessMarkdown(i, overview, edir)
headline = string(blackfriday.Run([]byte(headline))) headline = string(blackfriday.Run([]byte(headline)))
e, err := theme.GetExerciceByTitle(ename) e, err := theme.GetExerciceByTitle(ename)

66
admin/sync/markdown.go Normal file
View File

@ -0,0 +1,66 @@
package sync
import (
"bufio"
"encoding/base32"
"log"
"os"
"path"
"regexp"
"strings"
"srs.epita.fr/fic-server/libfic"
"golang.org/x/crypto/blake2b"
"gopkg.in/russross/blackfriday.v2"
)
func ProcessMarkdown(i Importer, input string, rootDir string) (output string) {
// 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,
},
)),
))
// Import files
re, err := regexp.Compile(strings.Replace(absPath, "$", "\\$", -1) + "/[^\"]+")
if err != nil {
log.Println("Unable to compile regexp:", err)
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 {
log.Println("Unable to make directories:", err)
return
}
if fdto, err := os.Create(dPath); err != nil {
log.Println("Unable to create destination file:", err)
return
} else {
defer fdto.Close()
writer := bufio.NewWriter(fdto)
if err := getFile(i, rootDir + iPath, writer); err != nil {
os.Remove(dPath)
log.Println("Unable to create destination file:", err)
return
}
}
}
return
}

View File

@ -99,7 +99,7 @@ func SyncThemes(i Importer) []string {
authors_str := strings.Join(authors, ", ") authors_str := strings.Join(authors, ", ")
// Format overview (markdown) // Format overview (markdown)
intro = string(blackfriday.Run([]byte(intro))) intro = ProcessMarkdown(i, intro, tdir)
headline = string(blackfriday.Run([]byte(headline))) headline = string(blackfriday.Run([]byte(headline)))
if i.exists(path.Join(tdir, "heading.jpg")) { if i.exists(path.Join(tdir, "heading.jpg")) {

View File

@ -6,6 +6,7 @@ import (
"log" "log"
"time" "time"
"path" "path"
"strings"
) )
type myTeamFile struct { type myTeamFile struct {
@ -96,7 +97,7 @@ func MyJSONTeam(t *Team, started bool) (interface{}, error) {
exercice.ThemeId = tid exercice.ThemeId = tid
} }
exercice.Statement = e.Statement exercice.Statement = strings.Replace(e.Statement, "$FILES$", FilesDir, -1)
if len(e.Issue) > 0 { if len(e.Issue) > 0 {
exercice.Issue = e.Issue exercice.Issue = e.Issue
@ -104,7 +105,7 @@ func MyJSONTeam(t *Team, started bool) (interface{}, error) {
} }
if t == nil { if t == nil {
exercice.Overview = e.Overview exercice.Overview = strings.Replace(e.Overview, "$FILES$", FilesDir, -1)
exercice.VideoURI = e.VideoURI exercice.VideoURI = e.VideoURI
exercice.Tries = e.TriedCount() exercice.Tries = e.TriedCount()
exercice.Gain = int(float64(e.Gain) * e.Coefficient) exercice.Gain = int(float64(e.Gain) * e.Coefficient)

View File

@ -3,6 +3,7 @@ package fic
import ( import (
"fmt" "fmt"
"path" "path"
"strings"
) )
// exportedExercice is a structure representing a challenge, as exposed to players. // exportedExercice is a structure representing a challenge, as exposed to players.
@ -63,7 +64,7 @@ func ExportThemes() (interface{}, error) {
theme.URLId, theme.URLId,
theme.Authors, theme.Authors,
theme.Headline, theme.Headline,
theme.Intro, strings.Replace(theme.Intro, "$FILES$", FilesDir, -1),
imgpath, imgpath,
exos, exos,
} }