ytdl-web/web/views.go

53 lines
1.0 KiB
Go

package web
import (
"embed"
"encoding/json"
"html/template"
"net/http"
"net/url"
"github.com/gofiber/template/html"
"github.com/htfy96/reformism"
"go.fifitido.net/ytdl-web/ytdl/metadata"
)
//go:embed views/*
var viewsfs embed.FS
func ViewsEngine() *html.Engine {
engine := html.NewFileSystem(http.FS(viewsfs), ".html")
engine.AddFunc(
"unsafe", func(s string) template.HTML {
return template.HTML(s)
},
)
engine.AddFunc(
"queryEscape", func(s string) string {
return url.QueryEscape(s)
},
)
engine.AddFunc(
"jsonMarshal", func(s any) (string, error) {
j, err := json.MarshalIndent(s, "", " ")
if err != nil {
return "", err
}
return string(j), nil
},
)
engine.AddFunc(
"downloadContext", func(meta metadata.Metadata, url, basePath string, format metadata.Format) map[string]any {
return map[string]any{
"Meta": meta,
"Url": url,
"BasePath": basePath,
"Format": format,
}
},
)
engine.AddFuncMap(reformism.FuncsHTML)
return engine
}