her.esy.fun/engine/optim-classes.sh

72 lines
1.8 KiB
Bash
Raw Normal View History

2020-02-09 12:24:53 +00:00
#!/usr/bin/env nix-shell
#!nix-shell -i zsh
#!nix-shell -I nixpkgs="https://github.com/NixOS/nixpkgs/archive/19.09.tar.gz"
2019-12-05 13:55:51 +00:00
2020-02-10 08:42:17 +00:00
cd "$(git rev-parse --show-toplevel)" || exit 1
2019-12-06 14:34:36 +00:00
webdir="_site"
retrieve_classes_in_html () {
cat $webdir/**/*.html(N) | \
perl -pe 's/class="?([a-zA-Z0-9_-]*)/\nCLASS: $1\n/g'
}
retrieve_classes_in_css () {
cat $webdir/**/*.css(N) | \
perl -pe 's/\.([a-zA-Z-_][a-zA-Z0-9-_]*)/\nCLASS: $1\n/g'
}
classes=( $( {retrieve_classes_in_html; retrieve_classes_in_css}| \
egrep "^CLASS: [^ ]*$" |\
sort -u | \
2020-01-04 14:29:27 +00:00
awk 'length($2)>2 && $2 !~ /(web-file-size|article-date|example|src)/ {print length($2),$2}'|\
2019-12-06 14:34:36 +00:00
sort -rn | \
awk '{print $2}') )
2019-12-05 13:55:51 +00:00
chr() {
[ "$1" -lt 26 ] || return 1
printf "\\$(printf '%03o' $(( 97 + $1 )))"
}
shortName() {
if [ "$1" -gt 25 ]; then
print -- $(shortName $(( ( $1 / 26 ) - 1 )))$(shortName $(( $1 % 26 )))
else
chr $1
fi
}
i=0;
typeset -A assoc
for c in $classes; do
sn=$(shortName $i)
2019-12-06 18:05:06 +00:00
print -- "$c -> $sn"
2019-12-05 13:55:51 +00:00
assoc[$c]=$sn
((i++))
done
2019-12-06 14:34:36 +00:00
htmlreplacer=''
cssreplacer=''
for long in $classes; do
htmlreplacer=$htmlreplacer's#class=("?)'${long}'#class=$1'${assoc[$long]}'#g;'
cssreplacer=$cssreplacer's#\.'${long}'#.'${assoc[$long]}'#g;'
done
2019-12-06 18:05:06 +00:00
sizeof() {
stat --format="%s" "$*"
}
2019-12-05 13:55:51 +00:00
2019-12-06 14:34:36 +00:00
for fic in $webdir/**/*.{html,xml}(N); do
2019-12-06 18:05:06 +00:00
before=$(sizeof $fic)
print -n -- "$fic ($before"
2019-12-06 14:34:36 +00:00
perl -pi -e $htmlreplacer $fic
2019-12-06 18:05:06 +00:00
after=$(sizeof $fic)
print -- " => $after [$(( ((before - after) * 100) / before ))])"
2019-12-05 13:55:51 +00:00
done
2019-12-06 14:34:36 +00:00
for fic in $webdir/**/*.css(N); do
2019-12-06 18:05:06 +00:00
before=$(sizeof $fic)
print -n -- "$fic ($before"
2019-12-06 14:34:36 +00:00
perl -pi -e $cssreplacer $fic
2019-12-06 18:05:06 +00:00
after=$(sizeof $fic)
print -- " => $after [$(( ((before - after) * 100) / before ))])"
2019-12-05 13:55:51 +00:00
done