2017-12-08 20:01:42 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
2019-09-06 23:25:42 +00:00
|
|
|
"errors"
|
2017-12-08 20:01:42 +00:00
|
|
|
"fmt"
|
2018-12-02 04:05:59 +00:00
|
|
|
"math/rand"
|
2017-12-08 20:01:42 +00:00
|
|
|
"path"
|
2018-08-19 20:40:08 +00:00
|
|
|
"regexp"
|
2017-12-08 20:01:42 +00:00
|
|
|
"strings"
|
2018-12-09 18:34:01 +00:00
|
|
|
"unicode"
|
2017-12-08 20:01:42 +00:00
|
|
|
|
|
|
|
"github.com/julienschmidt/httprouter"
|
2018-08-17 19:18:10 +00:00
|
|
|
"gopkg.in/russross/blackfriday.v2"
|
2019-07-04 17:18:52 +00:00
|
|
|
"srs.epita.fr/fic-server/libfic"
|
2017-12-08 20:01:42 +00:00
|
|
|
)
|
|
|
|
|
2019-07-04 17:18:52 +00:00
|
|
|
// GetThemes returns all theme directories in the base directory.
|
|
|
|
func GetThemes(i Importer) ([]string, error) {
|
2017-12-08 20:01:42 +00:00
|
|
|
var themes []string
|
|
|
|
|
|
|
|
if dirs, err := i.listDir("/"); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
for _, dir := range dirs {
|
|
|
|
if _, err := i.listDir(dir); err == nil {
|
|
|
|
themes = append(themes, dir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return themes, nil
|
|
|
|
}
|
|
|
|
|
2018-05-11 23:08:37 +00:00
|
|
|
// getAuthors parses the AUTHORS file.
|
2017-12-08 20:01:42 +00:00
|
|
|
func getAuthors(i Importer, tname string) ([]string, error) {
|
|
|
|
if authors, err := getFileContent(i, path.Join(tname, "AUTHORS.txt")); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
2018-08-19 20:40:08 +00:00
|
|
|
var ret []string
|
|
|
|
re := regexp.MustCompile("^([^<]+)(?: +<(.*)>)?$")
|
2018-12-09 18:34:01 +00:00
|
|
|
for _, a := range strings.Split(authors, "\n") {
|
|
|
|
a = strings.TrimFunc(a, unicode.IsSpace)
|
2018-08-19 20:40:08 +00:00
|
|
|
grp := re.FindStringSubmatch(a)
|
|
|
|
if len(grp) < 2 || grp[2] == "" {
|
|
|
|
ret = append(ret, a)
|
|
|
|
} else {
|
|
|
|
ret = append(ret, fmt.Sprintf("<a href=\"%s\">%s</a>", grp[2], grp[1]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret, nil
|
2017-12-08 20:01:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-04 17:18:52 +00:00
|
|
|
// BuildTheme creates a Theme from a given importer.
|
|
|
|
func BuildTheme(i Importer, tdir string) (th *fic.Theme, errs []string) {
|
|
|
|
th = &fic.Theme{}
|
|
|
|
|
|
|
|
th.Path = tdir
|
|
|
|
|
|
|
|
// Extract theme's label
|
2019-09-06 18:12:01 +00:00
|
|
|
if tname, err := getFileContent(i, path.Join(tdir, "title.txt")); err == nil {
|
|
|
|
th.Name = fixnbsp(tname)
|
|
|
|
} else if f := strings.Index(tdir, "-"); f >= 0 {
|
2019-07-04 17:18:52 +00:00
|
|
|
th.Name = fixnbsp(tdir[f+1:])
|
|
|
|
} else {
|
|
|
|
th.Name = fixnbsp(tdir)
|
|
|
|
}
|
|
|
|
th.URLId = fic.ToURLid(th.Name)
|
|
|
|
|
|
|
|
if authors, err := getAuthors(i, tdir); err != nil {
|
|
|
|
errs = append(errs, fmt.Sprintf("%q: unable to get AUTHORS.txt: %s", th.Name, err))
|
|
|
|
return nil, errs
|
|
|
|
} else {
|
|
|
|
// Format authors
|
|
|
|
th.Authors = strings.Join(authors, ", ")
|
|
|
|
}
|
|
|
|
|
|
|
|
if intro, err := getFileContent(i, path.Join(tdir, "overview.txt")); err != nil {
|
|
|
|
errs = append(errs, fmt.Sprintf("%q: unable to get theme's overview: %s", th.Name, err))
|
|
|
|
} else {
|
|
|
|
// Split headline from intro
|
|
|
|
ovrvw := strings.Split(fixnbsp(intro), "\n")
|
|
|
|
th.Headline = ovrvw[0]
|
|
|
|
if len(ovrvw) > 1 {
|
|
|
|
intro = strings.Join(ovrvw[1:], "\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Format overview (markdown)
|
|
|
|
th.Intro, err = ProcessMarkdown(i, intro, tdir)
|
|
|
|
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)))
|
|
|
|
}
|
|
|
|
|
|
|
|
if i.exists(path.Join(tdir, "heading.jpg")) {
|
|
|
|
th.Image = path.Join(tdir, "heading.jpg")
|
|
|
|
} else if i.exists(path.Join(tdir, "heading.png")) {
|
|
|
|
th.Image = path.Join(tdir, "heading.png")
|
2019-11-23 11:00:21 +00:00
|
|
|
} else {
|
|
|
|
errs = append(errs, fmt.Sprintf("%q: heading.jpg: No such file", tdir))
|
2019-07-04 17:18:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-11 23:08:37 +00:00
|
|
|
// SyncThemes imports new or updates existing themes.
|
2017-12-08 20:01:42 +00:00
|
|
|
func SyncThemes(i Importer) []string {
|
|
|
|
var errs []string
|
|
|
|
|
2019-07-04 17:18:52 +00:00
|
|
|
if themes, err := GetThemes(i); err != nil {
|
2017-12-08 20:01:42 +00:00
|
|
|
errs = append(errs, err.Error())
|
|
|
|
} else {
|
2018-12-02 04:05:59 +00:00
|
|
|
rand.Shuffle(len(themes), func(i, j int) {
|
|
|
|
themes[i], themes[j] = themes[j], themes[i]
|
|
|
|
})
|
|
|
|
|
2018-07-13 05:25:21 +00:00
|
|
|
for _, tdir := range themes {
|
2019-07-04 17:18:52 +00:00
|
|
|
btheme, berrs := BuildTheme(i, tdir)
|
|
|
|
errs = append(errs, berrs...)
|
2018-07-13 05:25:21 +00:00
|
|
|
|
2019-07-04 17:18:52 +00:00
|
|
|
if btheme == nil {
|
2017-12-08 20:01:42 +00:00
|
|
|
continue
|
2017-12-21 21:17:41 +00:00
|
|
|
}
|
2017-12-08 20:01:42 +00:00
|
|
|
|
2019-11-23 10:59:13 +00:00
|
|
|
if len(btheme.Image) > 0 {
|
|
|
|
if _, err := i.importFile(btheme.Image,
|
|
|
|
func(filePath string, origin string) (interface{}, error) {
|
|
|
|
btheme.Image = strings.TrimPrefix(filePath, fic.FilesDir)
|
|
|
|
return nil, nil
|
|
|
|
}); err != nil {
|
|
|
|
errs = append(errs, fmt.Sprintf("%q: unable to import heading image: %s", tdir, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-04 17:18:52 +00:00
|
|
|
var theme fic.Theme
|
2019-09-06 18:12:01 +00:00
|
|
|
if theme, err = fic.GetThemeByPath(btheme.Path); err != nil {
|
2019-07-04 17:18:52 +00:00
|
|
|
if _, err := fic.CreateTheme(*btheme); err != nil {
|
|
|
|
errs = append(errs, fmt.Sprintf("%q: an error occurs during add: %s", tdir, err))
|
|
|
|
continue
|
|
|
|
}
|
2018-11-25 02:35:54 +00:00
|
|
|
}
|
|
|
|
|
2019-07-04 17:18:52 +00:00
|
|
|
if !fic.CmpTheme(theme, *btheme) {
|
|
|
|
btheme.Id = theme.Id
|
|
|
|
if _, err := btheme.Update(); err != nil {
|
2018-07-13 05:25:21 +00:00
|
|
|
errs = append(errs, fmt.Sprintf("%q: an error occurs during update: %s", tdir, err))
|
2017-12-21 21:17:41 +00:00
|
|
|
continue
|
2017-12-08 20:01:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
2018-05-11 23:08:37 +00:00
|
|
|
// ApiListRemoteThemes is an accessor letting foreign packages to access remote themes list.
|
2017-12-08 20:01:42 +00:00
|
|
|
func ApiListRemoteThemes(_ httprouter.Params, _ []byte) (interface{}, error) {
|
2019-07-04 17:18:52 +00:00
|
|
|
return GetThemes(GlobalImporter)
|
2017-12-08 20:01:42 +00:00
|
|
|
}
|
|
|
|
|
2018-05-11 23:08:37 +00:00
|
|
|
// ApiListRemoteTheme is an accessor letting foreign packages to access remote main theme attributes.
|
2017-12-08 20:01:42 +00:00
|
|
|
func ApiGetRemoteTheme(ps httprouter.Params, _ []byte) (interface{}, error) {
|
2019-09-06 23:25:42 +00:00
|
|
|
r, errs := BuildTheme(GlobalImporter, ps.ByName("thid"))
|
|
|
|
if r == nil {
|
|
|
|
return r, errors.New(fmt.Sprintf("%q", errs))
|
|
|
|
} else {
|
|
|
|
return r, nil
|
|
|
|
}
|
2017-12-08 20:01:42 +00:00
|
|
|
}
|