forked from nocd5/md2html
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassets.go
90 lines (81 loc) · 2.25 KB
/
assets.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"embed"
"log"
"path/filepath"
"strings"
)
var (
//go:embed assets/*.js
js embed.FS
//go:embed assets/*.css
css embed.FS
jsStr string
mathjaxCfgStr string
mathjaxStr string
cssStr string
)
func init() {
jsStr = getEmbedFilesToStr(js, "assets", "mathjax", true)
mathjaxCfgStr = getEmbedFilesToStr(js, "assets", "mathjax-config.min.js", false)
mathjaxStr = getEmbedFilesToStr(js, "assets", "MathJax-TeXSVG.min.js", false)
cssStr = getEmbedFilesToStr(css, "assets", "", false)
}
func getEmbedFilesToStr(embedFS embed.FS, embedDir, keyword string, exclude bool) string {
resultBuilder := strings.Builder{}
if embedDirs, err := embedFS.ReadDir(embedDir); err == nil {
var onlyThis bool
for _, dir := range embedDirs {
currName := dir.Name()
embedPath := strings.Join([]string{embedDir, currName}, "/")
if dir.IsDir() {
resultBuilder.WriteString(getEmbedFilesToStr(embedFS, embedPath, keyword, exclude))
continue
}
lowerName := strings.ToLower(currName)
if keyword != "" {
found := strings.Contains(lowerName, strings.ToLower(keyword))
if exclude && found {
continue // 排除
}
onlyThis = !exclude && found // 只获取匹配的文件内容
}
// 读取文件内容为字符串
var embedContent []byte
if embedContent, err = embedFS.ReadFile(embedPath); err != nil {
log.SetPrefix("[WARN]")
log.Println("Read embed file content error: ", err)
continue
}
var prefix, suffix string
switch filepath.Ext(lowerName) {
case ".js":
name := filepath.Base(lowerName)
if strings.Contains(name, "mathjax") && strings.Contains(name, "config") {
prefix = "<script type=\"text/x-mathjax-config\">"
} else {
prefix = "<script type=\"text/javascript\">"
}
prefix += " /* " + currName + " */\n"
suffix = "\n</script>\n"
case ".css":
prefix = "<style> /* " + currName + " */\n"
suffix = "\n</style>\n"
default:
}
if onlyThis {
resultBuilder.Reset()
}
resultBuilder.WriteString(prefix)
resultBuilder.Write(embedContent)
resultBuilder.WriteString(suffix)
if onlyThis {
break
}
}
} else {
log.SetPrefix("[WARN]")
log.Println("Read embed file error: ", err)
}
return resultBuilder.String()
}