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"
|
2023-04-15 12:38:33 -04:00
|
|
|
"github.com/htfy96/reformism"
|
|
|
|
"go.fifitido.net/ytdl-web/ytdl"
|
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
|
|
|
|
},
|
|
|
|
)
|
2023-04-15 12:38:33 -04:00
|
|
|
engine.AddFunc(
|
|
|
|
"downloadContext", func(meta ytdl.Metadata, url, basePath string, format ytdl.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
|
|
|
|
}
|