-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgenerate.go
149 lines (124 loc) · 3.12 KB
/
generate.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// +build ignore
package main
import (
"bytes"
"fmt"
"github.com/statping/statping/utils"
"github.com/tdewolff/minify/v2"
"github.com/tdewolff/minify/v2/html"
"io/ioutil"
"os"
"os/exec"
)
func main() {
fmt.Println("Generating success/failure email templates from MJML to a HTML golang constant")
success := convertMJML("success.mjml")
failure := convertMJML("failure.mjml")
verify := convertMJML("verify.mjml")
forgotPass := convertMJML("forgot_password.mjml")
message := convertMJML("message.mjml")
transaction := convertMJML("transaction.mjml")
codeOut := `// Code generated by MJML on ` + utils.Now().String() + `, DO NOT EDIT.
package emails
//go:generate go run generate.go
import (
"bytes"
"html/template"
)
const (
` + constVar("Success", success) + `
` + constVar("Failure", failure) + `
` + constVar("Verify", verify) + `
` + constVar("ForgotPassword", forgotPass) + `
` + constVar("Message", message) + `
` + constVar("Transaction", transaction) + `
)
// Parse accepts a HTML template and data to render into the template
func Parse(temp string, data interface{}) (string, error) {
var err error
tmpl := template.New("email").Option("missingkey=error")
tmpl, err = tmpl.Parse(temp)
if err != nil {
return "", err
}
w := bytes.NewBufferString("")
if err := tmpl.Execute(w, data); err != nil {
return "", err
}
return w.String(), nil
}
`
utils.SaveFile("compiled.go", []byte(codeOut))
createIndex()
fmt.Println("Email MJML to HTML const saved: compiled.go")
}
func createIndex() {
out := `<html>
<body>
<h1>Statping Email Templates</h1>
<p>
<a href="failure.mjml.html">Service Failure Email</a>
</p>
<p>
<a href="success.mjml.html">Service Online Email</a>
</p>
<p>
<a href="verify.mjml.html">Verify Email</a>
</p>
<p>
<a href="forgot_password.mjml.html">Forgot Password</a>
</p>
<p>
<a href="message.mjml.html">Message</a>
</p>
<p>
<a href="transaction.mjml.html">Transaction</a>
</p>
<h1>Statping Email Images</h1>
<p>
<img width="650" src="https://assets.statping.com/greenbackground.png">
https://assets.statping.com/greenbackground.png
</p>
<p>
<img width="650" src="https://assets.statping.com/offlinebanner.png">
https://assets.statping.com/offlinebanner.png
</p>
<p>
<img src="https://assets.statping.com/iconlight.png">
https://assets.statping.com/iconlight.png
</p>
<p>
<img src="https://assets.statping.com/statpingcom.png">
https://assets.statping.com/statpingcom.png
</p>
</body>
</html>`
if err := ioutil.WriteFile("docs/index.html", []byte(out), os.FileMode(0755)); err != nil {
panic(err)
}
}
func constVar(key, data string) string {
return fmt.Sprintf("%s = `%s`\n", key, data)
}
func minimize(val string) string {
m := minify.New()
m.Add("text/html", &html.Minifier{
KeepDefaultAttrVals: true,
})
s, err := m.String("text/html", val)
if err != nil {
panic(err)
}
return s
}
func convertMJML(file string) string {
w := bytes.NewBufferString("")
fmt.Println("mjml", file)
cmd := exec.Command("mjml", "./mjml/"+file, "-s")
cmd.Stdout = w
cmd.Start()
cmd.Wait()
out := minimize(w.String())
ioutil.WriteFile("./docs/"+file+".html", []byte(out), os.FileMode(0755))
return out
}