Normalize Unicode strings
This commit is contained in:
2
go.mod
2
go.mod
@@ -9,6 +9,7 @@ require (
|
||||
github.com/openai/openai-go/v3 v3.8.1
|
||||
github.com/pmezard/go-difflib v1.0.0
|
||||
golang.org/x/image v0.33.0
|
||||
golang.org/x/text v0.31.0
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
)
|
||||
|
||||
@@ -24,5 +25,4 @@ require (
|
||||
github.com/tidwall/sjson v1.2.5 // indirect
|
||||
golang.org/x/crypto v0.37.0 // indirect
|
||||
golang.org/x/sync v0.18.0 // indirect
|
||||
golang.org/x/text v0.31.0 // indirect
|
||||
)
|
||||
|
||||
@@ -7,6 +7,8 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/text/unicode/norm"
|
||||
|
||||
"github.com/akikareha/himewiki/internal/config"
|
||||
"github.com/akikareha/himewiki/internal/data"
|
||||
"github.com/akikareha/himewiki/internal/filter"
|
||||
@@ -80,7 +82,8 @@ func Edit(cfg *config.Config, w http.ResponseWriter, r *http.Request, params *Pa
|
||||
http.Error(w, "Invalid revision ID", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
content = r.FormValue("content")
|
||||
rawContent := r.FormValue("content")
|
||||
content = norm.NFC.String(rawContent)
|
||||
preview = r.FormValue("preview")
|
||||
save = r.FormValue("save")
|
||||
}
|
||||
|
||||
@@ -6,6 +6,8 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/text/unicode/norm"
|
||||
|
||||
"github.com/akikareha/himewiki/internal/config"
|
||||
)
|
||||
|
||||
@@ -18,7 +20,8 @@ type Params struct {
|
||||
}
|
||||
|
||||
func parse(cfg *config.Config, r *http.Request) Params {
|
||||
name := strings.TrimPrefix(r.URL.Path, "/")
|
||||
rawName := strings.TrimPrefix(r.URL.Path, "/")
|
||||
name := norm.NFC.String(rawName)
|
||||
if name == "" {
|
||||
name = cfg.Wiki.Front
|
||||
}
|
||||
|
||||
@@ -9,6 +9,8 @@ import (
|
||||
"net/url"
|
||||
"strconv"
|
||||
|
||||
"golang.org/x/text/unicode/norm"
|
||||
|
||||
"github.com/akikareha/himewiki/internal/config"
|
||||
"github.com/akikareha/himewiki/internal/data"
|
||||
"github.com/akikareha/himewiki/internal/filter"
|
||||
@@ -54,7 +56,8 @@ func Upload(cfg *config.Config, w http.ResponseWriter, r *http.Request, params *
|
||||
return
|
||||
}
|
||||
|
||||
name = r.FormValue("name")
|
||||
rawName := r.FormValue("name")
|
||||
name = norm.NFC.String(rawName)
|
||||
if name == "" {
|
||||
http.Error(w, "name required", http.StatusBadRequest)
|
||||
return
|
||||
|
||||
@@ -5,6 +5,8 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/text/unicode/norm"
|
||||
|
||||
"github.com/akikareha/himewiki/internal/config"
|
||||
"github.com/akikareha/himewiki/internal/data"
|
||||
"github.com/akikareha/himewiki/internal/templates"
|
||||
@@ -17,7 +19,8 @@ func Search(cfg *config.Config, w http.ResponseWriter, r *http.Request, params *
|
||||
page = 1
|
||||
}
|
||||
|
||||
word := r.URL.Query().Get("w")
|
||||
rawWord := r.URL.Query().Get("w")
|
||||
word := norm.NFC.String(rawWord)
|
||||
searchType := r.URL.Query().Get("t")
|
||||
var results []string
|
||||
if word != "" {
|
||||
|
||||
@@ -9,6 +9,8 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/text/unicode/norm"
|
||||
|
||||
"github.com/akikareha/himewiki/internal/config"
|
||||
)
|
||||
|
||||
@@ -18,7 +20,8 @@ func handleStatic(cfg *config.Config, w http.ResponseWriter, r *http.Request) bo
|
||||
log.Fatalf("Invalid static dir: %v", err)
|
||||
}
|
||||
|
||||
cleanPath := filepath.Clean(r.URL.Path)
|
||||
path := norm.NFC.String(r.URL.Path)
|
||||
cleanPath := filepath.Clean(path)
|
||||
staticPath := filepath.Join(baseDir, cleanPath)
|
||||
if !strings.HasPrefix(staticPath, baseDir + string(os.PathSeparator)) {
|
||||
return false
|
||||
|
||||
@@ -3,22 +3,30 @@ package filter
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"golang.org/x/text/unicode/norm"
|
||||
|
||||
"github.com/akikareha/himewiki/internal/config"
|
||||
)
|
||||
|
||||
func Apply(cfg *config.Config, title string, content string) (string, error) {
|
||||
normTitle := norm.NFC.String(title)
|
||||
normContent := norm.NFC.String(content)
|
||||
|
||||
if cfg.Filter.Agent == "openai" {
|
||||
return withOpenAI(cfg, title, content)
|
||||
filtered, err := withOpenAI(cfg, normTitle, normContent)
|
||||
return norm.NFC.String(filtered), err
|
||||
} else if cfg.Filter.Agent == "nil" {
|
||||
return content, nil
|
||||
return normContent, nil
|
||||
} else {
|
||||
return "", fmt.Errorf("Invalid filter agent. If you want to disable filter, set it to \"nil\".")
|
||||
}
|
||||
}
|
||||
|
||||
func ImageApply(cfg *config.Config, title string, data []byte) ([]byte, error) {
|
||||
normTitle := norm.NFC.String(title)
|
||||
|
||||
if cfg.ImageFilter.Agent == "openai" {
|
||||
return imageWithOpenAI(cfg, title, data)
|
||||
return imageWithOpenAI(cfg, normTitle, data)
|
||||
} else if cfg.ImageFilter.Agent == "nil" {
|
||||
return data, nil
|
||||
} else {
|
||||
@@ -27,10 +35,14 @@ func ImageApply(cfg *config.Config, title string, data []byte) ([]byte, error) {
|
||||
}
|
||||
|
||||
func GnomeApply(cfg *config.Config, title string, content string) (string, error) {
|
||||
normTitle := norm.NFC.String(title)
|
||||
normContent := norm.NFC.String(content)
|
||||
|
||||
if cfg.Gnome.Agent == "openai" {
|
||||
return gnomeWithOpenAI(cfg, title, content)
|
||||
filtered, err := gnomeWithOpenAI(cfg, normTitle, normContent)
|
||||
return norm.NFC.String(filtered), err
|
||||
} else if cfg.Gnome.Agent == "nil" {
|
||||
return content, nil
|
||||
return normContent, nil
|
||||
} else {
|
||||
return "", fmt.Errorf("Invalid gnome filter agent. If you want to disable filter, set it to \"nil\".")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user