From: Sergey Matveev Date: Sat, 27 Nov 2021 19:29:34 +0000 (+0300) Subject: zsh implementation X-Git-Url: http://www.git.stargrave.org/?p=torn.git;a=commitdiff_plain;h=1f81cbd89921a131ad9d6f56ef8948eff4d9cf4e zsh implementation --- diff --git a/torn.zsh b/torn.zsh new file mode 100755 index 0000000..3ea8bec --- /dev/null +++ b/torn.zsh @@ -0,0 +1,68 @@ +# torn - Musical files renaming with russian language transliteration +# Copyright (C) 2007-2021 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. +# +# * transliterates everything +# * replaces spaces with underscores +# * lowercases file's extension +# * replaces "_-_" with single dash +# * removes spaces before and after brackets +# * removes space after comma +# * removes "[]" brackets +# * replaces ampersand with "and" word +# +# You can run like that: +# $ fpath=(... $fpath) +# $ autoload torn +# $ 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`' +# +# Recursively dive into subdirectories: +# $ zmv -Q "(**/)*(.)" '`torn $f`' + +tornt() { + local n=$1 + local rusS=абвгдеёзийклмнопрстуфхцьъыэ + local engS=abvgdeezijklmnoprstufhcjjye + for i ({1..${#rusS}}) { + eval n=\${n:gs/${rusS[$i]}/${engS[$i]}} + eval n=\${n:gs/${(U)rusS[$i]}/${(U)engS[$i]}} + } + 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 +} + +tornm() { + local n=$1 + n=${n:gs/ /_} + n=${n:gs/_-_/-} + n=${n:gs/,_/,} + n=${n//[\[\]]/} + n=${n:gs/_(/(} + n=${n:gs/-(/-} + n=${n:gs/(_/(} + n=${n:gs/)_/-} + n=${n:gs/_)/)} + n=${n:gs/&/and} + [[ $n =~ "\." ]] && n=${n:r}.${(L)n:e} + [[ $n =~ "^([[:digit:]]+)[-.]_*(.+)$" ]] && n="$match[1].$match[2]" + print $n +} + +local n=${1:t} +n=`tornm $n` +n=`tornt $n` +print ${1:h}/$n