add selectable engines support to web (closes #20)
All checks were successful
mozhi pipeline / Push Docker image to Codeberg docker registry (push) Successful in 19m45s
mozhi pipeline / Build and publish artifacts (push) Successful in 1h5m59s

This commit is contained in:
2024-08-27 22:36:11 +05:30
parent a2ac174314
commit 0c2c0f7dfd
5 changed files with 78 additions and 19 deletions

View File

@@ -17,6 +17,7 @@ import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/compress"
"github.com/gofiber/fiber/v2/middleware/filesystem"
//"github.com/gofiber/fiber/v2/middleware/limiter"
// For debugging purposes
// "github.com/gofiber/fiber/v2/middleware/logger"
@@ -37,11 +38,19 @@ func Serve(port string) {
views := http.FS(views.GetFiles())
engine := html.NewFileSystem(views, ".html")
engine.AddFunc(
"newlinetobr", func(s string) template.HTML {
return template.HTML(strings.ReplaceAll(strings.ReplaceAll(s, "\n", "<br>"), "\r", ""))
},
)
engine.AddFunc("newlinetobr", func(s string) template.HTML {
return template.HTML(strings.ReplaceAll(strings.ReplaceAll(s, "\n", "<br>"), "\r", ""))
})
// Returns specific string if a string is in another slice
engine.AddFunc("contains", func(data []string, s string, retval string) template.HTML {
for _, val := range data {
if val == s {
return template.HTML(retval)
}
}
return template.HTML("")
})
app := fiber.New(fiber.Config{
Views: engine,
@@ -107,7 +116,7 @@ func Serve(port string) {
to := utils.Sanitize(utils.GetQueryOrFormValue(c, "to"), "alpha")
text := utils.GetQueryOrFormValue(c, "text")
var swapText string
if engine != "all" && text != "" {
if engine != "all" && engine != "some" && text != "" {
translation, tlerr := libmozhi.Translate(engine, to, from, text)
if tlerr == nil {
swapText = translation.OutputText