diff --git a/go.mod b/go.mod index 739bb7e..5486116 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,6 @@ go 1.15 require ( github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 github.com/eiblog/blackfriday v0.0.0-20161010144836-c0ec111761ae - github.com/eiblog/utils v0.0.0-20181119015747-92c93e218753 github.com/gin-contrib/sessions v0.0.3 github.com/gin-gonic/gin v1.6.3 github.com/gofrs/uuid v3.3.0+incompatible diff --git a/go.sum b/go.sum index 1860fd6..c478509 100644 --- a/go.sum +++ b/go.sum @@ -24,8 +24,6 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/eiblog/blackfriday v0.0.0-20161010144836-c0ec111761ae h1:V6YC640Gs5jEUYfCimyuXsTW5gzNcIEESG4MGmOJCtA= github.com/eiblog/blackfriday v0.0.0-20161010144836-c0ec111761ae/go.mod h1:HzHqTCGEAkSSzBM3shBvQHsHRQYUvjNOIC4mHipZ6tI= -github.com/eiblog/utils v0.0.0-20181119015747-92c93e218753 h1:Nygjtnh1nF5zejJF7pZnsoFh77wOPS+jlfhikjkJg60= -github.com/eiblog/utils v0.0.0-20181119015747-92c93e218753/go.mod h1:mZHWnipRp41yw/rti2DgpbMiBE5i6ifqg7PKooEwRh4= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk= diff --git a/pkg/core/blog/file/timer.go b/pkg/core/blog/file/timer.go index 295ed6e..266d236 100644 --- a/pkg/core/blog/file/timer.go +++ b/pkg/core/blog/file/timer.go @@ -9,8 +9,8 @@ import ( "github.com/eiblog/eiblog/pkg/cache" "github.com/eiblog/eiblog/pkg/config" + "github.com/eiblog/eiblog/tools" - "github.com/eiblog/utils/tmpl" "github.com/sirupsen/logrus" ) @@ -21,7 +21,7 @@ func init() { var err error xmlTmpl, err = template.New("").Funcs(template.FuncMap{ - "dateformat": tmpl.DateFormat, + "dateformat": tools.DateFormat, }).ParseGlob(root) if err != nil { panic(err) diff --git a/pkg/core/blog/page/page.go b/pkg/core/blog/page/page.go index 90f1795..620febc 100644 --- a/pkg/core/blog/page/page.go +++ b/pkg/core/blog/page/page.go @@ -8,7 +8,6 @@ import ( "github.com/eiblog/eiblog/pkg/config" "github.com/eiblog/eiblog/tools" - "github.com/eiblog/utils/tmpl" "github.com/gin-gonic/gin" ) @@ -16,7 +15,7 @@ import ( var htmlTmpl *template.Template func init() { - htmlTmpl = template.New("eiblog").Funcs(tmpl.TplFuncMap) + htmlTmpl = template.New("eiblog").Funcs(tools.TplFuncMap) root := filepath.Join(config.WorkDir, "website") files := tools.ReadDirFiles(root, func(name string) bool { if name == ".DS_Store" { diff --git a/tools/tmplfunc.go b/tools/tmplfunc.go new file mode 100644 index 0000000..6814de9 --- /dev/null +++ b/tools/tmplfunc.go @@ -0,0 +1,65 @@ +// Package tools provides ... +package tools + +import ( + "encoding/base64" + htmpl "html/template" + "io/ioutil" + "log" + "net/http" + "strings" + "text/template" + "time" +) + +var TplFuncMap = make(template.FuncMap) + +func init() { + TplFuncMap["dateformat"] = DateFormat + TplFuncMap["str2html"] = Str2html + TplFuncMap["join"] = Join + TplFuncMap["isnotzero"] = IsNotZero + TplFuncMap["getavatar"] = GetAvatar +} + +func Str2html(raw string) htmpl.HTML { + return htmpl.HTML(raw) +} + +// DateFormat takes a time and a layout string and returns a string with the formatted date. Used by the template parser as "dateformat" +func DateFormat(t time.Time, layout string) string { + return t.Format(layout) +} + +func Join(a []string, sep string) string { + return strings.Join(a, sep) +} + +func IsNotZero(t time.Time) bool { + return !t.IsZero() +} + +// cache avatar image +// url: https:///static/img/avatar.png +var avatar string + +func GetAvatar(domain string) string { + if avatar == "" { + resp, err := http.Get("https://" + domain + "/static/img/avatar.png") + if err != nil { + log.Println(err) + return "" + } + defer resp.Body.Close() + + data, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Println(err) + return "" + } + + avatar = "data:" + resp.Header.Get("content-type") + ";base64," + base64.StdEncoding.EncodeToString(data) + } + + return avatar +}