-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsafe-characters.sh
More file actions
94 lines (82 loc) · 1.76 KB
/
safe-characters.sh
File metadata and controls
94 lines (82 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/bin/bash
# Script to replace "ugly" characters for safe ones
# rename_to_simple_string find . -type f -not -regex '\.[.()a-zA-Z0-9\/_\-]*$'
GIT_DIFF_HIGHLIGHT_PATH="/usr/share/doc/git/contrib/diff-highlight/diff-highlight"
chars_to_replace=(
'Á/A'
'á/a'
'É/E'
'Ě/E'
'é/e'
'ě/e'
'Í/I'
'í/i'
'Ó/O'
'ó/o'
'Ú/U'
'Ů/U'
'ú/u'
'ů/u'
'Ý/Y'
'ý/y'
'Č/C'
'č/c'
'Ď/D'
'ď/d'
'Ň/N'
'ň/n'
'Ř/R'
'ř/r'
'Š/S'
'š/s'
'Ť/T'
'ť/t'
'Ž/Z'
'ž/z'
'\?/'
"'/"
'\&/and'
'’/'
':/'
'!/'
'=/-'
',/'
'(/'
')/'
'"/'
'\[/'
'\]/'
)
create_sed_expression() {
local sed_expression='s/#//g'
for regex in ${chars_to_replace[*]}; do
new_string=$(echo "$new_string; s/$regex/g")
done
echo $new_string
}
sed_expression=$(create_sed_expression)
create_simple_string() {
local new_string=$(echo $1 | sed -e "$sed_expression")
new_string=$(echo $new_string | sed -e 's/ /_/g')
echo $new_string
}
rename_to_simple_string() {
# Change Input Field Separator to new line, instead of space
IFS=$'\n';
for line in $("$@"); do
local new_name=$(create_simple_string $line)
if [ "$line" = "$new_name" ]; then
echo -E "$(colors::foreground violet 'ignore:') $line"
else
echo -E "$(colors::foreground blue from:) $line"
if [ -f "$GIT_DIFF_HIGHLIGHT_PATH" ]; then
local name_diff=$(diff -u <(echo "$line") <(echo "$new_name") | $GIT_DIFF_HIGHLIGHT_PATH | tail -1)
echo -E "$(colors::foreground green ' to:') ${name_diff:1}"
else
echo -E "$(colors::foreground red 'Missing diff-highlight check GIT_DIFF_HIGHLIGHT_PATH") ')"
echo -E "$(colors::foreground green ' to:') $new_name"
fi
mv -i "$line" "$new_name"
fi
done
}