From 0c2c0f7dfd4b044112e7a6fb5e57b09ffdee4901 Mon Sep 17 00:00:00 2001 From: Arya Kiran Date: Tue, 27 Aug 2024 22:36:11 +0530 Subject: [PATCH] add selectable engines support to web (closes #20) --- cmd/translate.go | 4 ++-- pages/index.go | 47 +++++++++++++++++++++++++++++++++++++++++------ serve/serve.go | 21 +++++++++++++++------ utils/main.go | 7 ++++--- views/index.html | 18 ++++++++++++++++-- 5 files changed, 78 insertions(+), 19 deletions(-) diff --git a/cmd/translate.go b/cmd/translate.go index 10c7b90..f7272f4 100644 --- a/cmd/translate.go +++ b/cmd/translate.go @@ -2,8 +2,8 @@ package cmd import ( "encoding/json" - "strings" "fmt" + "strings" "codeberg.org/aryak/libmozhi" @@ -12,7 +12,7 @@ import ( var ( engine string - engines string + engines string query string source string dest string diff --git a/pages/index.go b/pages/index.go index e47b4b2..0319b88 100644 --- a/pages/index.go +++ b/pages/index.go @@ -3,16 +3,17 @@ package pages import ( "os" "slices" + "strings" "codeberg.org/aryak/libmozhi" "codeberg.org/aryak/mozhi/utils" "github.com/gofiber/fiber/v2" ) -func langListMerge(engines map[string]string) ([]libmozhi.List, []libmozhi.List) { +func langListMerge(engines []string) ([]libmozhi.List, []libmozhi.List) { sl := []libmozhi.List{} tl := []libmozhi.List{} - for key := range engines { + for _, key := range engines { temp, _ := libmozhi.LangList(key, "sl") temp2, _ := libmozhi.LangList(key, "tl") sl = append(sl, temp...) @@ -23,6 +24,11 @@ func langListMerge(engines map[string]string) ([]libmozhi.List, []libmozhi.List) func HandleIndex(c *fiber.Ctx) error { engines := utils.EngineList() + type enginesStruct struct { + Engines []string `query:"engines"` + } + enginesSome := new(enginesStruct) + c.QueryParser(enginesSome) var enginesAsArray []string for engine := range engines { enginesAsArray = append(enginesAsArray, engine) @@ -42,7 +48,25 @@ func HandleIndex(c *fiber.Ctx) error { var sourceLanguages []libmozhi.List var targetLanguages []libmozhi.List if engine == "all" { - sourceLanguages, targetLanguages = langListMerge(engines) + sourceLanguages, targetLanguages = langListMerge(enginesAsArray) + } else if engine == "some" { + if c.Cookies("engines") == "" && enginesSome.Engines == nil { + enginesSome.Engines = append(enginesSome.Engines, "google") + } else if enginesSome.Engines == nil && c.Cookies("engines") != "" { + enginesSome.Engines = strings.Split(c.Cookies("engines"), ",") + } + for i, engine := range enginesSome.Engines { + if !slices.Contains(enginesAsArray, engine) || engine == "some" { + // Delete array from slice if its not in engines list + enginesSome.Engines = append(enginesSome.Engines[:i], enginesSome.Engines[i+1:]...) + } else if engine == "all" { + enginesSome.Engines = enginesAsArray + } + } + if enginesSome.Engines == nil { + enginesSome.Engines = append(enginesSome.Engines, "google") + } + sourceLanguages, targetLanguages = langListMerge(enginesSome.Engines) } else { sourceLanguages, _ = libmozhi.LangList(engine, "sl") targetLanguages, _ = libmozhi.LangList(engine, "tl") @@ -66,13 +90,16 @@ func HandleIndex(c *fiber.Ctx) error { var translation libmozhi.LangOut var translationExists bool - var transall []libmozhi.LangOut + var transmany []libmozhi.LangOut var tlerr error var ttsFrom string var ttsTo string if engine != "" && originalText != "" && from != "" && to != "" { if engine == "all" { - transall = libmozhi.TranslateAll(to, from, originalText) + transmany = libmozhi.TranslateAll(to, from, originalText) + } else if engine == "some" { + // The error doesn't really matter since it just checks if the engines are valid, which is already checked in the code at the beginning of the func + transmany, _ = libmozhi.TranslateSome(enginesSome.Engines, to, from, originalText) } else { translation, tlerr = libmozhi.Translate(engine, to, from, originalText) if tlerr != nil { @@ -117,15 +144,23 @@ func HandleIndex(c *fiber.Ctx) error { cookie.Value = to c.Cookie(cookie) } + if enginesSome.Engines != nil { + cookie := new(fiber.Cookie) + cookie.Name = "engines" + cookie.Value = strings.Join(enginesSome.Engines, ",") + c.Cookie(cookie) + } return c.Render("index", fiber.Map{ "Engine": engine, "enginesNames": engines, + "SomeEngines": enginesSome.Engines, + "SomeEnginesStr": strings.Join(enginesSome.Engines, ","), "SourceLanguages": sourceLanguages, "TargetLanguages": targetLanguages, "OriginalText": originalText, "Translation": translation, "TranslationExists": translationExists, - "TranslateAll": transall, + "TranslateMany": transmany, "From": from, "To": to, "TtsFrom": ttsFrom, diff --git a/serve/serve.go b/serve/serve.go index 0f3bc12..0f28859 100644 --- a/serve/serve.go +++ b/serve/serve.go @@ -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", "
"), "\r", "")) - }, - ) + engine.AddFunc("newlinetobr", func(s string) template.HTML { + return template.HTML(strings.ReplaceAll(strings.ReplaceAll(s, "\n", "
"), "\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 diff --git a/utils/main.go b/utils/main.go index 03216cd..0003625 100644 --- a/utils/main.go +++ b/utils/main.go @@ -1,10 +1,11 @@ package utils import ( - "codeberg.org/aryak/libmozhi" - "github.com/gofiber/fiber/v2" "os" "regexp" + + "codeberg.org/aryak/libmozhi" + "github.com/gofiber/fiber/v2" ) var nonAlphanumericRegex = regexp.MustCompile(`[^a-zA-Z,]+`) @@ -34,7 +35,7 @@ func Sanitize(str string, strip string) string { } func EngineList() map[string]string { - engines := map[string]string{"all": "All Engines", "google": "Google", "deepl": "DeepL", "duckduckgo": "DuckDuckGo", "libre": "LibreTranslate", "mymemory": "MyMemory", "reverso": "Reverso", "yandex": "Yandex"} + engines := map[string]string{"all": "All Engines", "some": "Some Engines", "google": "Google", "deepl": "DeepL", "duckduckgo": "DuckDuckGo", "libre": "LibreTranslate", "mymemory": "MyMemory", "reverso": "Reverso", "yandex": "Yandex"} if EnvTrueNoExist("MOZHI_GOOGLE_ENABLED") == false { delete(engines, "google") } else if EnvTrueNoExist("MOZHI_DEEPL_ENABLED") == false { diff --git a/views/index.html b/views/index.html index cc68bf2..f5a257a 100644 --- a/views/index.html +++ b/views/index.html @@ -14,9 +14,23 @@

+ {{ if eq .Engine "some" }} +
+ + + {{ end }} + {{ if eq .Engine "some" }} + + {{ end }}