-
Notifications
You must be signed in to change notification settings - Fork 2
/
html.go
113 lines (87 loc) · 2.51 KB
/
html.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
package utils
import (
"log"
"regexp"
"strings"
"github.com/tdewolff/minify/v2"
"github.com/tdewolff/minify/v2/css"
"github.com/tdewolff/minify/v2/html"
"github.com/tdewolff/minify/v2/js"
)
// MinScript minifies the script
func MinScript(sctiptString string) (string, error) {
m := minify.New()
// m.AddFunc("text/css", css.Minify)
// m.AddFunc("text/html", html.Minify)
// m.AddFunc("image/svg+xml", svg.Minify)
m.AddFuncRegexp(regexp.MustCompile("^(application|text)/(x-)?(java|ecma)script$"), js.Minify)
// m.AddFuncRegexp(regexp.MustCompile("[/+]json$"), json.Minify)
// m.AddFuncRegexp(regexp.MustCompile("[/+]xml$"), xml.Minify)
return m.String("text/javascript", sctiptString)
}
// MinCSS minifies the CSS
func MinCSS(cssString string) (string, error) {
m := minify.New()
m.AddFunc("text/css", css.Minify)
return m.String("text/css", cssString)
}
// MinHTML minifies the CSS
func MinHTML(htmlString string) (string, error) {
m := minify.New()
m.AddFunc("text/html", html.Minify)
return m.String("text/html", htmlString)
}
// ScriptsHTML the HTML from scripts string
func ScriptsHTML(str string) string {
scripts := strings.Split(str, ",")
scriptsHTML := ""
for _, script := range scripts {
if strings.HasPrefix(script, "http://") || strings.HasPrefix(script, "https://") {
scriptsHTML += "<script src=\"" + script + "\"></script>"
continue
}
path := "views/" + script
if FileExists(path) == false {
println(path + " does not exists")
continue
}
scriptText, getContentsError := FileGetContents(path)
if getContentsError != nil {
log.Println(path + " get contents error")
continue
}
scriptText, err := MinScript(scriptText)
if err != nil {
log.Println(err)
}
scriptsHTML += "<script>" + scriptText + "</script>"
}
return scriptsHTML
}
// StylesHTML the styles
func StylesHTML(str string) string {
styles := strings.Split(str, ",")
htmlStyles := ""
for _, style := range styles {
if strings.HasPrefix(style, "http://") || strings.HasPrefix(style, "https://") {
htmlStyles += "<link href=\"" + style + "\" rel=\"stylesheet\" />"
continue
}
path := "views/" + style
if FileExists(path) == false {
println(path + " does not exists")
continue
}
scriptText, getContentsError := FileGetContents(path)
if getContentsError != nil {
log.Println(path + " get contents error")
continue
}
styleText, err := MinScript(scriptText)
if err != nil {
log.Println(err)
}
htmlStyles += "<style>" + styleText + "</style>"
}
return htmlStyles
}