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"
|
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
|
|
|
|
},
|
|
|
|
)
|
2023-04-15 12:38:33 -04:00
|
|
|
engine.AddFunc(
|
2023-05-23 18:44:05 -04:00
|
|
|
"downloadContext", func(meta metadata.Metadata, url, basePath string, format metadata.Format) map[string]any {
|
2023-04-15 12:38:33 -04:00
|
|
|
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
|
|
|
|
}
|