her.esy.fun/engine/update-file-size.sh

80 lines
2 KiB
Bash
Raw Normal View History

2020-05-25 20:28:06 +00:00
#!/usr/bin/env zsh
2019-11-12 23:36:17 +00:00
2020-02-10 08:42:17 +00:00
cd "$(git rev-parse --show-toplevel)" || exit 1
2021-08-21 15:19:30 +00:00
source ./engine/envvars.sh
2020-02-16 15:56:19 +00:00
webdir="_optim"
2019-11-12 23:36:17 +00:00
sizeof() {
stat --format="%s" "$*"
}
debug () {
2019-11-18 08:46:15 +00:00
print -- $* >/dev/null
2019-11-12 23:36:17 +00:00
}
toh () {
numfmt --to=iec $*
}
tmpdir=$(mktemp -d)
2019-11-19 14:14:20 +00:00
type -a filelist
if (($#>0)); then
filelist=( $* )
else
2021-04-27 12:34:29 +00:00
filelist=( $webdir/*/*.html(.) $webdir/posts/*.html )
2019-11-19 14:14:20 +00:00
fi
for fic in $filelist; do
2019-11-12 23:36:17 +00:00
print -n -- "$fic "
htmlsize=$(sizeof $fic)
debug HTML: $htmlsize
2020-06-27 12:57:04 +00:00
2020-05-03 19:50:56 +00:00
gzhtmlsize=$( gzip -c $fic|wc -c )
debug GZHTML: $gzhtmlsize
2019-11-12 23:36:17 +00:00
xfic=$tmpdir/$fic
mkdir -p $(dirname $xfic)
hxclean $fic > $xfic
images=( $( < $xfic hxselect -i -c -s '\n' 'img::attr(src)' | sed 's/^\.\.\///' ) )
imgsize=0
nbimg=0
for i in $images; do
((nbimg++))
isize=$( sizeof ${fic:h}/$i )
debug $i '=>' $isize
(( imgsize += isize ))
done
debug IMG: $imgsize
css=( $( < $xfic hxselect -i -c -s '\n' 'link[rel=stylesheet]::attr(href)'))
csssize=0
2020-05-03 19:50:56 +00:00
gzcsssize=0
2019-11-12 23:36:17 +00:00
for i in $css; do
isize=$( sizeof $webdir/$i )
2020-05-03 19:50:56 +00:00
gzisize=$( gzip -c $webdir/$i | wc -c )
2019-11-12 23:36:17 +00:00
debug $i '=>' $isize
(( csssize += isize ))
2020-05-03 19:50:56 +00:00
(( gzcsssize += gzisize ))
2019-11-12 23:36:17 +00:00
done
debug CSS: $csssize
2020-05-03 19:50:56 +00:00
debug GZCSS: $gzcsssize
2019-11-12 23:36:17 +00:00
total=$(( htmlsize + imgsize + csssize ))
2020-05-03 19:50:56 +00:00
gztotal=$(( gzhtmlsize + imgsize + gzcsssize ))
2020-05-01 13:25:59 +00:00
# the space is important before the toh total
2020-05-01 17:26:07 +00:00
sizeinfos=$(print -- " $(toh $total) (html $(toh $htmlsize), css $(toh $csssize)")
2020-05-03 19:50:56 +00:00
gzsizeinfos=$(print -- " $(toh $gztotal) (html $(toh $gzhtmlsize), css $(toh $gzcsssize)")
2020-05-01 17:26:07 +00:00
if ((imgsize>0)); then
sizeinfos="$sizeinfos, img $(toh $imgsize))"
2020-05-03 19:50:56 +00:00
gzsizeinfos="$gzsizeinfos, img $(toh $imgsize))"
2020-05-01 17:26:07 +00:00
else
sizeinfos="$sizeinfos)"
2020-05-03 19:50:56 +00:00
gzsizeinfos="$gzsizeinfos)"
2020-05-01 17:26:07 +00:00
fi
2019-11-12 23:36:17 +00:00
print -- $sizeinfos
2021-04-27 12:34:29 +00:00
perl -pi -e 's#(<span class="?webfilesize"?>)[^<]*(</span>)#$1'"$sizeinfos"'$2#;s#(<span class="?gzwebfilesize"?>)[^<]*(</span>)#$1'"$gzsizeinfos"'$2#' $fic
2019-11-12 23:36:17 +00:00
done
rm -rf $tmpdir