move packages to src

This commit is contained in:
Nazar Kanaev
2021-02-26 13:39:30 +00:00
parent d825ce9bdf
commit 3fac9bb1bd
71 changed files with 0 additions and 0 deletions

45
src/assets/assets.go Normal file
View File

@@ -0,0 +1,45 @@
package assets
import (
"embed"
"html/template"
"io"
"io/ioutil"
"io/fs"
"os"
)
type assetsfs struct {
embedded *embed.FS
templates map[string]*template.Template
}
var FS assetsfs
func (afs assetsfs) Open(name string) (fs.File, error) {
if afs.embedded != nil {
return afs.embedded.Open(name)
}
return os.DirFS("assets").Open(name)
}
func Render(path string, writer io.Writer, data interface{}) {
var tmpl *template.Template
tmpl, found := FS.templates[path]
if !found {
tmpl = template.Must(template.New(path).Delims("{%", "%}").Funcs(template.FuncMap{
"inline": func(svg string) template.HTML {
svgfile, _ := FS.Open("graphicarts/" + svg)
content, _ := ioutil.ReadAll(svgfile)
svgfile.Close()
return template.HTML(content)
},
}).ParseFS(FS, path))
FS.templates[path] = tmpl
}
tmpl.Execute(writer, data)
}
func init() {
FS.templates = make(map[string]*template.Template)
}