-
Notifications
You must be signed in to change notification settings - Fork 2
/
template.go
39 lines (32 loc) · 1.02 KB
/
template.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
// SPDX-License-Identifier: MIT
package builder
import (
"html/template"
"io/fs"
"path"
"regexp"
"time"
"github.com/caixw/blogit/internal/data"
"github.com/caixw/blogit/internal/vars"
)
func newTemplate(d *data.Data, src fs.FS) (*template.Template, error) {
templateFuncs := template.FuncMap{
"strip": stripTags,
"html": func(html string) interface{} { return template.HTML(html) },
"js": func(js string) interface{} { return template.JS(js) },
"rfc3339": func(t time.Time) string { return t.Format(time.RFC3339) },
"date": func(t time.Time, format string) string { return t.Format(format) },
"themeURL": func(p string) string {
return data.BuildURL(d.URL, vars.ThemesDir, p)
},
}
return template.New(d.Theme.ID).
Funcs(templateFuncs).
ParseFS(src, path.Join(vars.ThemesDir, d.Theme.ID, vars.LayoutDir, "/*"))
}
// 去掉所有的标签信息
var stripExpr = regexp.MustCompile("</?[^</>]+/?>")
// 过滤标签
func stripTags(html string) string {
return stripExpr.ReplaceAllString(html, "")
}