Created
March 17, 2020 19:03
-
-
Save haggen/d332e0c47cf37114472edee1710bcf3c to your computer and use it in GitHub Desktop.
Randomize id attributes in a svg (or other markup)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -euo pipefail | |
help() { | |
cat <<-EOF >&2 | |
Usage: | |
$0 [-h|-v|-i ...] <path> | |
Options: | |
-v|--verbose Print replacements. | |
-i|--ignore ... Comma separated list of IDs to ignore. | |
EOF | |
} | |
if test -z "$*"; then | |
help | |
exit 1 | |
fi | |
options=$(getopt -n "$0" -o hvi: -l help,verbose,ignore -- "$@") | |
if test $? -ne 0; then | |
exit 1 | |
fi | |
eval set -- "$options" | |
ignore="" | |
verbose="" | |
while :; do | |
case "$1" in | |
-i|--ignore) | |
ignore="$2" | |
shift | |
shift | |
;; | |
-v|--verbose) | |
verbose="yes" | |
shift | |
;; | |
-h|--help) | |
help | |
exit | |
;; | |
--) | |
shift | |
break | |
;; | |
esac | |
done | |
grep -Po 'id="\K[^"]+' $1 | while read id; do | |
if echo ",$ignore," | grep -q ",$id,"; then | |
test -n "$verbose" && echo "Ignoring '$id'" | |
continue | |
fi | |
replacement="id$(openssl rand -hex 2)" | |
test -n "$verbose" && echo "Replacing '$id' with '$replacement'" | |
sed -i "s/$id/$replacement/g" $1 | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment