]> Sergey Matveev's repositories - torn.git/blobdiff - torn.zsh
Do not exit, it is not a script
[torn.git] / torn.zsh
index 511d544960a1159cb94c5f51c46976f1c481aee9..7c2876c611b5631242cfaa274351aa6eaa0597f5 100755 (executable)
--- a/torn.zsh
+++ b/torn.zsh
@@ -1,5 +1,5 @@
 # torn - Musical files renaming with russian language transliteration
-# Copyright (C) 2007-2021 Sergey Matveev (stargrave@stargrave.org)
+# Copyright (C) 2021-2023 Sergey Matveev (stargrave@stargrave.org)
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -10,6 +10,7 @@
 # to be used with zsh'es glob expansion.
 #
 # * transliterates everything
+# * replaces non FAT-compatible characters with underscores
 # * replaces spaces with underscores
 # * lowercases file's extension
 # * replaces "_-_" with single dash
@@ -28,6 +29,8 @@
 #   $ torn # assume that it is not autoloaded
 #   $ zmv "*" '`tornt $f`'
 #
+# torna() replaces non FAT-compatible characters with underscores.
+#
 # Recursively dive into subdirectories:
 #   $ zmv -Q "(**/)*(.)" '`torn $f`'
 
@@ -42,7 +45,7 @@ tornt() {
     local rusL=( ж  ч  ш   щ  я  ю  Ж  Ч  Ш   Щ  Я  Ю)
     local engL=(zh ch sh sch ja ju Zh Ch Sh Sch Ja Ju)
     for i ({1..${#rusL}}) eval n=\${n:gs/${rusL[$i]}/${engL[$i]}}
-    print $n
+    print -- $n
 }
 
 tornm() {
@@ -59,10 +62,23 @@ tornm() {
     n=${n:gs/&/and}
     [[ $n =~ "\." ]] && n=${n:r}.${(L)n:e}
     [[ $n =~ "^([[:digit:]]+)[-.]_*(.+)$" ]] && n="$match[1].$match[2]"
-    print $n
+    print -- $n
+}
+
+torna() {
+    local n=$1
+    setopt REMATCH_PCRE
+    for i ({1..${#n}}) {
+        [[ ${n[$i]} =~ "[[:ascii:]]" ]] || n[$i]=_
+        [[ ${n[$i]} =~ "[[:print:]]" ]] || n[$i]=_
+        [[ ${n[$i]} =~ "[^,+/:;<=>?[]|]" ]] || n[$i]=_
+    }
+    print -- $n
 }
 
+[[ -n $1 ]] || return
 local n=${1:t}
 n=`tornm $n`
 n=`tornt $n`
-print ${1:h}/$n
+n=`torna $n`
+print -- ${1:h}/$n