1 # torn - Musical files renaming with russian language transliteration
2 # Copyright (C) 2021-2023 Sergey Matveev (stargrave@stargrave.org)
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, version 3 of the License.
8 # Initially this script was written on Perl, but now it is zsh function,
9 # that is intended to be used with zmv command. It is much more flexible
10 # to be used with zsh'es glob expansion.
12 # * transliterates everything
13 # * replaces non FAT-compatible characters with underscores
14 # * replaces spaces with underscores
15 # * lowercases file's extension
16 # * replaces "_-_" with single dash
17 # * removes spaces before and after brackets
18 # * removes space after comma
19 # * removes "[]" brackets
20 # * replaces ampersand with "and" word
22 # You can run like that:
23 # $ fpath=(... $fpath)
25 # $ zmv "*" '`torn $f`"
27 # Transliteration function is separate tornt(), so you can use only
29 # $ torn # assume that it is not autoloaded
30 # $ zmv "*" '`tornt $f`'
32 # torna() replaces non FAT-compatible characters with underscores.
34 # Recursively dive into subdirectories:
35 # $ zmv -Q "(**/)*(.)" '`torn $f`'
39 local rusS=абвгдеёзийклмнопрстуфхцьъыэ
40 local engS=abvgdeezijklmnoprstufhcjjye
41 for i ({1..${#rusS}}) {
42 eval n=\${n:gs/${rusS[$i]}/${engS[$i]}}
43 eval n=\${n:gs/${(U)rusS[$i]}/${(U)engS[$i]}}
45 local rusL=( ж ч ш щ я ю Ж Ч Ш Щ Я Ю)
46 local engL=(zh ch sh sch ja ju Zh Ch Sh Sch Ja Ju)
47 for i ({1..${#rusL}}) eval n=\${n:gs/${rusL[$i]}/${engL[$i]}}
65 [[ $n =~ "\." ]] && n=${n:r}.${(L)n:e}
66 [[ $n =~ "^([[:digit:]]+)[-.]_*(.+)$" ]] && n="$match[1].$match[2]"
74 [[ ${n[$i]} =~ "[[:ascii:]]" ]] || n[$i]=_
75 [[ ${n[$i]} =~ "[[:print:]]" ]] || n[$i]=_
76 [[ ${n[$i]} =~ "[^,+/:;<=>?[]|]" ]] || n[$i]=_