ytdl-web/web/views.go

53 lines
1.0 KiB
Go
Raw Permalink Normal View History

2023-04-14 11:58:32 -04:00
package web
import (
"embed"
2023-04-14 16:07:57 -04:00
"encoding/json"
2023-04-14 11:58:32 -04:00
"html/template"
"net/http"
2023-04-14 16:07:57 -04:00
"net/url"
2023-04-14 11:58:32 -04:00
"github.com/gofiber/template/html"
"github.com/htfy96/reformism"
2023-05-23 18:44:05 -04:00
"go.fifitido.net/ytdl-web/ytdl/metadata"
2023-04-14 11:58:32 -04:00
)
//go:embed views/*
var viewsfs embed.FS
func ViewsEngine() *html.Engine {
engine := html.NewFileSystem(http.FS(viewsfs), ".html")
engine.AddFunc(
2023-04-14 16:07:57 -04:00
"unsafe", func(s string) template.HTML {
2023-04-14 11:58:32 -04:00
return template.HTML(s)
},
)
2023-04-14 16:07:57 -04:00
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(
2023-05-23 18:44:05 -04:00
"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)
2023-04-14 11:58:32 -04:00
return engine
}