-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsanchar
More file actions
executable file
·110 lines (100 loc) · 3.66 KB
/
Copy pathsanchar
File metadata and controls
executable file
·110 lines (100 loc) · 3.66 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env bash
# Get text from clipboard
# and transform certain characters to simpler ones
# and write the result back to the clipboard.
#
# Alternatively, sanitize given text files directly.
#
# Usage:
#
# sanchar # copy sanitized clipboard text back to clipboard
# sanchar FILE [FILE...] # sanitize file(s) in-place
#
# Beware: editing some lines in this script in some editors may lead to quirky results, e.g. in vim,
# which apparently has issues coping with some of the invisible characters.
#
# author: andreasl
replace_chars() {
sed \
-e "s/[’‘‚‛]/'/g" \
-e 's/[“”„"]/"/g' \
-e 's/[–—−‑‒―]/-/g' \
-e 's/[…]/.../g' \
-e 's/[•‣◦▪●]/-/g' \
-e 's/[×]/x/g' \
-e 's|[÷]|/|g' \
-e 's/[,]/,/g' \
-e 's/[.]/./g' \
-e 's/[!]/!/g' \
-e 's/[?]/?/g' \
-e 's/[;]/;/g' \
-e 's/[~]/~/g' \
-e 's/[(]/(/g' \
-e 's/[)]/)/g' \
-e 's/[[]/[/g' \
-e 's/[]]/]/g' \
-e 's/[:]/:/g' \
-e 's/[|]/|/g' \
-e 's|[⧸⁄∕]|/|g' \
-e 's|[<]|<|g' \
-e 's|[>]|>|g' \
-e 's/[ ]/ /g' \
-e 's/[]//g' # some invisible characters
}
remove_leading_and_trailing_blank_lines() {
# Remove leading and trailing blank lines, but keep the ones in the middle.
# Blank lines are not printed right away, they are held back until a line with content
# shows up and releases them. Leading blanks are dropped because no content has been seen
# yet, trailing ones because the input ends before anything releases them. The blanks in
# between survive, because the next content line flushes them.
awk '
/[^[:space:]]/ { # line has at least one non-whitespace character
printf "%s", blanks # print the blank lines collected in `blanks`
blanks = "" # reset `blanks`
seen = 1 # remember that content has appeared
print # print this line
next # done with this line, skip the rule below
}
seen { blanks = blanks $0 "\n" } # blank line after content: hold it, do not print
'
}
show_help() {
cat <<'EOF'
Usage:
sanchar sanitize clipboard text and write back to clipboard
sanchar FILE [FILE...] sanitize file(s) in-place
Options:
-h, --help show this help message and exit
EOF
exit 0
}
case "${1:-}" in
-h | --help) show_help ;;
esac
if [ $# -eq 0 ]; then
command -v pbpaste >/dev/null && clip_out=(pbpaste) || clip_out=(xclip -selection clipboard -out)
command -v pbcopy >/dev/null && clip_in=(pbcopy) || clip_in=(xclip -selection clipboard)
# sanitize clipboard
input_text="$("${clip_out[@]}")"
if [ -z "$input_text" ]; then
echo "Clipboard is empty or does not contain text."
exit 1
fi
output_text="$(echo "$input_text" | replace_chars | remove_leading_and_trailing_blank_lines)"
printf '%s' "$output_text" | "${clip_in[@]}"
echo "Special characters have been replaced and the clipboard has been updated."
else
# sanitize given files
for file in "$@"; do
# add a sentinel `x` character to presever trailing newlines
input_text="$(
cat "$file"
printf x
)"
input_text="${input_text%x}"
tmp="$(mktemp)"
printf '%s' "$input_text" | replace_chars >"$tmp"
mv "$tmp" "$file"
echo "Sanitized: $file"
done
fi