-
Notifications
You must be signed in to change notification settings - Fork 0
/
html_builder.go
65 lines (58 loc) · 1.37 KB
/
html_builder.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
package html
import (
"github.com/mrasu/GravityR/injection"
"github.com/pkg/errors"
"html/template"
"io/fs"
"os"
)
const (
TypeMain = "main"
TypeMermaid = "mermaid"
)
func CreateHtml(htmlType, filename string, bo *BuildOption) error {
script, err := fs.ReadFile(injection.ClientDist, "client/dist/assets/"+htmlType+".js")
if err != nil {
return errors.Wrap(err, "failed to open file")
}
style, err := fs.ReadFile(injection.ClientDist, "client/dist/assets/"+htmlType+".css")
if err != nil {
return errors.Wrap(err, "failed to open file")
}
tmpl := `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>GravityR</title>
<script>
window.grParam = {{.gr}}
</script>
<style>{{.style}}</style>
</head>
<body>
<div id="app"></div>
<script>{{.script}}</script>
</body>
</html>
`
tpl, err := template.New("").Parse(tmpl)
if err != nil {
return errors.Wrap(err, "failed create template")
}
m := map[string]interface{}{
"script": template.JS(script),
"style": template.CSS(style),
"gr": bo.createGrMap(),
}
f, err := os.Create(filename)
if err != nil {
return errors.Wrap(err, "failed to create html file")
}
err = tpl.Execute(f, m)
if err != nil {
return errors.Wrap(err, "failed to write template to html")
}
return nil
}