From 87471acf98ebbc1cd9ad165a197d074e599cfd26 Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Sun, 2 Dec 2018 16:24:33 +0100 Subject: [PATCH] sync: import files in markdown, relative to theme/exercice dir --- admin/sync/exercice_hints.go | 3 +- admin/sync/exercices.go | 4 +-- admin/sync/markdown.go | 66 ++++++++++++++++++++++++++++++++++++ admin/sync/themes.go | 2 +- libfic/team_my.go | 5 +-- libfic/theme_export.go | 3 +- 6 files changed, 75 insertions(+), 8 deletions(-) create mode 100644 admin/sync/markdown.go diff --git a/admin/sync/exercice_hints.go b/admin/sync/exercice_hints.go index b1590a43..143288c5 100644 --- a/admin/sync/exercice_hints.go +++ b/admin/sync/exercice_hints.go @@ -12,7 +12,6 @@ import ( "srs.epita.fr/fic-server/libfic" - "gopkg.in/russross/blackfriday.v2" _ "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)) continue } else { - hint.Content = string(blackfriday.Run([]byte(hint.Content))) + hint.Content = ProcessMarkdown(i, hint.Content, exercice.Path) } // Import hint diff --git a/admin/sync/exercices.go b/admin/sync/exercices.go index 3a018c85..d840336a 100644 --- a/admin/sync/exercices.go +++ b/admin/sync/exercices.go @@ -114,8 +114,8 @@ func SyncExercices(i Importer, theme fic.Theme) []string { } // Markdown pre-formating - statement = string(blackfriday.Run([]byte(statement))) - overview = string(blackfriday.Run([]byte(overview))) + statement = ProcessMarkdown(i, statement, edir) + overview = ProcessMarkdown(i, overview, edir) headline = string(blackfriday.Run([]byte(headline))) e, err := theme.GetExerciceByTitle(ename) diff --git a/admin/sync/markdown.go b/admin/sync/markdown.go new file mode 100644 index 00000000..a505408d --- /dev/null +++ b/admin/sync/markdown.go @@ -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 +} diff --git a/admin/sync/themes.go b/admin/sync/themes.go index b60abf47..daf85756 100644 --- a/admin/sync/themes.go +++ b/admin/sync/themes.go @@ -99,7 +99,7 @@ func SyncThemes(i Importer) []string { authors_str := strings.Join(authors, ", ") // Format overview (markdown) - intro = string(blackfriday.Run([]byte(intro))) + intro = ProcessMarkdown(i, intro, tdir) headline = string(blackfriday.Run([]byte(headline))) if i.exists(path.Join(tdir, "heading.jpg")) { diff --git a/libfic/team_my.go b/libfic/team_my.go index f2842f3e..300d9bdf 100644 --- a/libfic/team_my.go +++ b/libfic/team_my.go @@ -6,6 +6,7 @@ import ( "log" "time" "path" + "strings" ) type myTeamFile struct { @@ -96,7 +97,7 @@ func MyJSONTeam(t *Team, started bool) (interface{}, error) { exercice.ThemeId = tid } - exercice.Statement = e.Statement + exercice.Statement = strings.Replace(e.Statement, "$FILES$", FilesDir, -1) if len(e.Issue) > 0 { exercice.Issue = e.Issue @@ -104,7 +105,7 @@ func MyJSONTeam(t *Team, started bool) (interface{}, error) { } if t == nil { - exercice.Overview = e.Overview + exercice.Overview = strings.Replace(e.Overview, "$FILES$", FilesDir, -1) exercice.VideoURI = e.VideoURI exercice.Tries = e.TriedCount() exercice.Gain = int(float64(e.Gain) * e.Coefficient) diff --git a/libfic/theme_export.go b/libfic/theme_export.go index e9eb2c03..c60caa3d 100644 --- a/libfic/theme_export.go +++ b/libfic/theme_export.go @@ -3,6 +3,7 @@ package fic import ( "fmt" "path" + "strings" ) // exportedExercice is a structure representing a challenge, as exposed to players. @@ -63,7 +64,7 @@ func ExportThemes() (interface{}, error) { theme.URLId, theme.Authors, theme.Headline, - theme.Intro, + strings.Replace(theme.Intro, "$FILES$", FilesDir, -1), imgpath, exos, }