portfolio/build_tools/static_build.sh

92 lines
3.1 KiB
Bash
Raw Normal View History

2022-08-02 20:09:45 +00:00
#!/bin/bash
# This script will generate a completely static website by downloading pages generated by PHP
# Working directory expected: root of the project
export PORT=8452
export SERVER_URL=127.0.0.1:$PORT
echo "Trying to start a PHP server on $SERVER_URL"
sh build_tools/server.sh start
sleep 0.1
[ ! -d ./build ] && mkdir ./build
locales=('fr' 'en')
for locale in "${locales[@]}"; do
echo "==== Start of build for locale '$locale'"
p=./build/$locale
mkdir -p $p
# wget with custom header to not render link to change language because wget will follow them
cd $p
wget -k -K -E -r -l 10 -p -N -F -nH --header="Enable-Locale-Switch: no" http://127.0.0.1:$PORT/$locale
cd ../../
resumePath=cv-matthieu-bessat-$locale
# render latex resume
# look for cache in .latex-cache
if [ -d ./.latex_cache ] && [ -e ./.latex_cache/resume_$locale.pdf ]
then
echo "using latex cache"
cp .latex-cache/resume_$locale.pdf $p/$resumePath.pdf
else
echo "failed to find cache"
compile_latex $locale $resumePath
export TARGET_LOCALE=$locale
sh build_tools/latex.sh
mv build/latex/resume_$locale.pdf $p/$resumePath.pdf
fi
mv $p/$locale.html $p/index.html
# render json resume
wget -O $p/$resumePath.json http://127.0.0.1:$PORT/json-resume --header="Accept-Language: $locale"
rm $p/json-resume
# Multiple sed commands to clean up the mess made by wget replacing stuff
# sed -E -i 's/\"([a-z]{2})\.html/\"\/\1/g' *.html
# for the main page, remove broken link to the same page (because we renamed to index.html)
sed -E -i "s/\"$locale\.html/\"/g" $p/index.html
# add proper link for alternate files
sed -E -i "s%data-target\=\"pdf-resume\" href\=\".+\"%href\=\"/$locale/$resumePath\.pdf\"%g" $p/index.html
sed -E -i "s%data-target\=\"json-resume\" href\=\".+\"%href\=\"/$locale/$resumePath\.json\"%g" $p/index.html
# for each file other than index.html, put a proper link back to the main page
find $p -not -name "index.html" -name "*.html" -exec sed -E -i "s/\"(\.\.\/)?$locale\.html/\"\1index.html/g" {} \+
# remove assets
rm -rv $p/*.woff $p/dist $p/imgs
echo "==== END of build for locale '$locale'"
done
# repair the links of the footer locale switcher
# for each line with data-lang-switch="{locale}" replace the href with the link to /{locale}/path...
# attempt to do with pipe failed, so I use a non optimal for loop
# find ./ -name "*.html" -exec sh -c 'sed -E -i "s/data-lang-switch=\"([a-z]{2})\".+href\=\".*\"/href\=\"\/\1\/{}\"/g" {}' \;
for path in $(find ./build -name "*.html"); do
pathM=`echo $path | cut -c 6-`
sed -E -i "s%data-lang-switch=\"([a-z]{2})\".+href\=\".*\"%href\=\"/\1/$pathM\"%g" $path
done
find ./build -name '*.orig' -delete
# change the path of all assets
find ./build -name "*.html" -exec sed -E -i "s%\"\.\./dist/%\"/dist/%g; s%\"dist/%\"/dist/%g; s%\"\.\./imgs/%\"/imgs/%g; s%\"imgs/%\"/imgs/%g" {} \+
cp -r ./public/* ./build
rm -r build/latex
rm build/index.php
sh build_tools/server.sh stop
echo ""
echo "Done."