/
html.go
128 lines (109 loc) · 2.94 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
126
127
128
package webby
import (
"bytes"
html "html/template"
"io"
"io/ioutil"
"sync"
"time"
)
func init() {
HtmlFuncBoot.Register(func(w *Web) {
// HTML Marksafe
w.HtmlFunc["html"] = func(str string) html.HTML {
return html.HTML(str)
}
// HTML Attr MarkSafe
w.HtmlFunc["htmlattr"] = func(str string) html.HTMLAttr {
return html.HTMLAttr(str)
}
// JS Marksafe
w.HtmlFunc["js"] = func(str string) html.JS {
return html.JS(str)
}
// JS String Marksafe
w.HtmlFunc["jsstr"] = func(str string) html.JSStr {
return html.JSStr(str)
}
// CSS Marksafe
w.HtmlFunc["css"] = func(str string) html.CSS {
return html.CSS(str)
}
})
}
func (w *Web) parseHtml(htmlstr string, value_map interface{}, buf io.Writer) {
if buf == nil {
buf = w
}
t, err := html.New("html").Funcs(w.HtmlFunc).Parse(htmlstr)
w.Check(err)
err = t.Execute(buf, value_map)
w.Check(err)
}
// Parse HTML
//
// Note: Marksafe functions/filters avaliable are 'html', 'htmlattr', 'js' and 'jsattr'.
func (w *Web) ParseHtml(htmlstr string, value_map interface{}) string {
buf := &bytes.Buffer{}
defer buf.Reset()
w.parseHtml(htmlstr, value_map, buf)
return buf.String()
}
// Parse HTML and Send Response to Client
//
// Note: Marksafe functions/filters avaliable are 'html', 'htmlattr', 'js' and 'jsattr'.
func (w *Web) ParseHtmlSend(htmlstr string, value_map interface{}) {
w.parseHtml(htmlstr, value_map, nil)
}
type htmlFileCacheStruct struct {
content string
expire time.Time
}
var htmlFileCache = struct {
sync.Mutex
m map[string]interface{}
}{m: map[string]interface{}{}}
var HtmlTemplateCacheExpire = 24 * time.Hour
// Get HTML File
//
// Note: Can also be used to get other kind of files. DO NOT USE THIS WITH LARGE FILES.
func (w *Web) GetHtmlFile(htmlfile string) string {
var content string
var content_in_byte []byte
var err error
htmlFileCache.Lock()
defer htmlFileCache.Unlock()
switch t := htmlFileCache.m[htmlfile].(type) {
case htmlFileCacheStruct:
if time.Now().Unix() > t.expire.Unix() {
goto getfile_and_cache
}
content = t.content
goto return_content
}
getfile_and_cache:
content_in_byte, err = ioutil.ReadFile(htmlfile)
if err != nil {
return err.Error()
}
content = string(content_in_byte)
if !DEBUG {
htmlFileCache.m[htmlfile] = htmlFileCacheStruct{content, time.Now().Add(HtmlTemplateCacheExpire)}
}
return_content:
return content
}
// Parse HTML File
//
// Note: Marksafe functions/filters avaliable are 'html', 'htmlattr', 'js' and 'jsattr'.
// DO NOT USE THIS WITH LARGE FILES.
func (w *Web) ParseHtmlFile(htmlfile string, value_map interface{}) string {
return w.ParseHtml(w.GetHtmlFile(htmlfile), value_map)
}
// Parse HTML File and Send Response to Client
//
// Note: Marksafe functions/filters avaliable are 'html', 'htmlattr', 'js' and 'jsattr'.
// DO NOT USE THIS WITH LARGE FILES.
func (w *Web) ParseHtmlFileSend(htmlfile string, value_map interface{}) {
w.ParseHtmlSend(w.GetHtmlFile(htmlfile), value_map)
}