From 8f552a2440f581a6ecf34fffbc326bfd491550cd Mon Sep 17 00:00:00 2001 From: gutmet Date: Mon, 28 Jun 2021 09:21:43 +0200 Subject: [PATCH] replace non-ascii characters in filename --- finstr.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/finstr.go b/finstr.go index 3950d3c..92c777d 100644 --- a/finstr.go +++ b/finstr.go @@ -19,6 +19,7 @@ import ( "strings" "text/template" "time" + "unicode" ) var dir string @@ -130,8 +131,26 @@ func byModified(p []photo) func(int, int) bool { /*-------------------------*/ +func isASCII(r rune) bool { + return r <= 127 +} + +func convASCII(s string) string { + out := []rune(s) + for i, c := range out { + if (isASCII(c) && (unicode.IsLetter(c) || unicode.IsDigit(c))) || c == '.' { + // keep unchanged + } else if unicode.IsSpace(c) || unicode.IsPunct(c) { + out[i] = '-' + } else { + out[i] = 'x' + } + } + return string(out) +} + func filename(s string) string { - return url.QueryEscape(s) + return url.QueryEscape(convASCII(s)) } func filenameDate(date calendarMonth) string {