-
Notifications
You must be signed in to change notification settings - Fork 18
/
html.go
125 lines (97 loc) · 2.92 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
114
115
116
117
118
119
120
121
122
123
124
125
package compress
import (
"fmt"
"html/template"
"os"
"path"
"path/filepath"
"strings"
)
type Group struct {
DistFile string
SourceFiles []string
SkipFiles []string
}
type compress struct {
StaticURL string
SrcPath string
DistPath string
SrcURL string
DistURL string
Groups map[string]Group
IsProMode bool
caches map[string]template.HTML
}
func (c *compress) SetProMode(isPro bool) {
c.IsProMode = isPro
}
func (c *compress) SetStaticURL(url string) {
c.StaticURL = url
}
func errHtml(err string, args ...interface{}) string {
err = fmt.Sprintf("Beego Compress: "+err, args...)
fmt.Fprintln(os.Stderr, err)
return "<!-- " + err + " -->"
}
type compressJs struct {
compress
}
func (c *compressJs) CompressJs(name string) template.HTML {
return generateHTML(name, c.compress, JsTagTemplate)
}
type compressCss struct {
compress
}
func (c *compressCss) CompressCss(name string) template.HTML {
return generateHTML(name, c.compress, CssTagTemplate)
}
func generateHTML(name string, c compress, t *template.Template) template.HTML {
if group, ok := c.Groups[name]; ok {
if c.IsProMode {
if c.caches == nil {
c.caches = make(map[string]template.HTML, len(c.Groups))
}
if scripts, ok := c.caches[name]; ok {
return scripts
}
scripts := fmt.Sprintf("<script>/* Beego Compress Powered */</script>\n\t")
filePath := filepath.Join(c.DistPath, group.DistFile)
if info, err := os.Stat(filePath); err == nil {
URL := c.StaticURL + path.Join(c.DistURL, group.DistFile) + "?ver=" + fmt.Sprint(info.ModTime().Unix())
if res, err := parseTmpl(t, map[string]string{"URL": URL}); err != nil {
errHtml("tempalte execute error: %s", err)
} else {
scripts += res
}
} else {
errHtml("load file `%s` for path `%s` error: %s", group.DistFile, filePath, err.Error())
}
if len(scripts) > 0 {
res := template.HTML(scripts + "\n")
c.caches[name] = res
return res
}
} else {
scripts := make([]string, 0, len(group.SourceFiles)+2)
scripts = append(scripts, fmt.Sprintf("<script>/* Beego Compress group `%s` begin */</script>", name))
for _, file := range group.SourceFiles {
filePath := filepath.Join(c.SrcPath, file)
if info, err := os.Stat(filePath); err == nil {
URL := c.StaticURL + path.Join(c.SrcPath, file) + "?ver=" + fmt.Sprint(info.ModTime().Unix())
if res, err := parseTmpl(t, map[string]string{"URL": URL}); err != nil {
scripts = append(scripts, errHtml("tempalte execute error: %s", err))
} else {
scripts = append(scripts, res)
}
} else {
scripts = append(scripts, errHtml("load file `%s` for path `%s` error: %s", file, filePath, err.Error()))
}
}
scripts = append(scripts, fmt.Sprintf("<script>/* end */</script>"))
return template.HTML(strings.Join(scripts, "\n\t"))
}
} else {
return template.HTML(errHtml("not found compress group `%s`", name))
}
return ""
}