X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=torn.zsh;h=d9900de9acb1aac5af1764ca02df1f87ab837fdd;hb=HEAD;hp=11d0fccfdc7920a209876bb21735d8327b5faa37;hpb=aaf61acdbaa2b4489317f39012140fa23dda7978;p=torn.git diff --git a/torn.zsh b/torn.zsh index 11d0fcc..69ec947 100755 --- a/torn.zsh +++ b/torn.zsh @@ -1,15 +1,16 @@ # torn - Musical files renaming with russian language transliteration -# Copyright (C) 2021-2023 Sergey Matveev (stargrave@stargrave.org) +# Copyright (C) 2021-2024 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 # the Free Software Foundation, version 3 of the License. -# Initially this script was written on Perl, but now it is zsh function, -# that is intended to be used with zmv command. It is much more flexible -# to be used with zsh'es glob expansion. +# Initially this script was written on Perl, but now it is Z shell +# function, that is intended to be used with zmv command. It is much +# more flexible 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 @@ -21,13 +22,15 @@ # You can run like that: # $ fpath=(... $fpath) # $ autoload torn -# $ zmv "*" '`torn $f`" +# $ zmv "*" '`torn $f`' # # Transliteration function is separate tornt(), so you can use only # its abilities: # $ 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,13 +45,15 @@ 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() { local n=$1 n=${n:gs/ /_} n=${n//_[–-]_/-} + n=${n//_[–-]/-} + n=${n//[–-]_/-} n=${n:gs/,_/,} n=${n//[\[\]]/} n=${n:gs/_(/(} @@ -59,10 +64,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