summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulien Dessaux2024-03-27 10:08:09 +0100
committerJulien Dessaux2024-03-27 10:08:09 +0100
commita6fd4a153d28a8c1c801003cec4e09ddfe1dcd60 (patch)
treee50e668927796c47b3610e0764346c7058d5d325
parentfeat(ods): trim spaces around the searched word (diff)
downloadods-a6fd4a153d28a8c1c801003cec4e09ddfe1dcd60.tar.gz
ods-a6fd4a153d28a8c1c801003cec4e09ddfe1dcd60.tar.bz2
ods-a6fd4a153d28a8c1c801003cec4e09ddfe1dcd60.zip
feat(ods): normalize all unicode characters to their ascii equivalents
-rw-r--r--go.mod2
-rw-r--r--main.go15
2 files changed, 12 insertions, 5 deletions
diff --git a/go.mod b/go.mod
index f212181..d9226b0 100644
--- a/go.mod
+++ b/go.mod
@@ -1,3 +1,5 @@
module git.adyxax.org/adyxax/ods/v2
go 1.22.1
+
+require golang.org/x/text v0.14.0 // indirect
diff --git a/main.go b/main.go
index 6fe702d..42ea1a1 100644
--- a/main.go
+++ b/main.go
@@ -2,10 +2,14 @@ package main
import (
"embed"
+ "golang.org/x/text/runes"
+ "golang.org/x/text/transform"
+ "golang.org/x/text/unicode/norm"
"html/template"
"log/slog"
"net/http"
"strings"
+ "unicode"
)
// Variables to customise the search behaviour
@@ -28,14 +32,14 @@ var indexTemplate = template.Must(template.New("index").ParseFS(templatesFS, "in
type IndexData struct {
HasQuery bool
Query string
- Invalid bool
+ Invalid bool
}
func getIndex() http.Handler {
data := IndexData{
HasQuery: false,
Query: "",
- Invalid: true,
+ Invalid: true,
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "no-store, no-cache")
@@ -44,14 +48,15 @@ func getIndex() http.Handler {
}
func postIndex() http.Handler {
- words := strings.Split(ods,"\n")
+ normalizer := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
+ words := strings.Split(ods, "\n")
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
data := IndexData{
HasQuery: true,
Query: r.FormValue("query"),
- Invalid: true,
+ Invalid: true,
}
- query := strings.TrimSpace(strings.ToUpper(data.Query))
+ query, _, _ := transform.String(normalizer, strings.TrimSpace(strings.ToUpper(data.Query)))
for _, w := range words {
if w == query {
data.Invalid = false