Source

diveintopython3-it / publish

#!/bin/sh

die () {
  echo "$1" >/dev/stderr
  exit 1
}

# Let's start by removing by-products from the latest build
rm -rf build

echo "started build"

# build table of contents
# Well, not really, since it mostly won't change anymore now
#python3.1 util/buildtoc.py

revision=`hg tip | grep changeset|cut -d":" -f2|cut -d" " -f4`
today=`date +"%Y-%m-%d"`

# make build directory and copy original files there for preflighting
mkdir build
mkdir build/d
# No robots.txt since we don't live on a website root
#cp robots.txt *.css build/
cp *.css build/
cp -R j build/
cp -R i build/
# not running examples right now
#rm examples/*.pyc
cp -R esempi build/
# not copying .htaccess files because of my server limitations
#cp .htaccess build/

echo "validating HTML"
for f in *.html; do
  python3.1 util/validate.py "$f" > /dev/null || die "Failed to validate $f"
done

echo "building HTML distribution"
htmlbasedir=diveintopython3-it-r"$revision"-"$today"
htmldir=build/"$htmlbasedir"
mkdir -p "$htmldir"
cp *.html "$htmldir"/
cp dip3.css "$htmldir"/
cp README.txt "$htmldir"/LEGGIMI.txt
mkdir "$htmldir"/i
cp i/*.png "$htmldir"/i/
mkdir "$htmldir"/j
cp j/dip3.js j/html5.js j/jquery.js j/prettify.js "$htmldir"/j/
mkdir "$htmldir"/esempi
cp esempi/*.jpg esempi/*.json esempi/*.pickle esempi/*.py esempi/*.txt esempi/*.xml "$htmldir"/esempi/
cd build/ && \
    zip -9rq diveintopython3-it-html-r"$revision"-"$today".zip "$htmlbasedir" && \
cd ..
echo "Redirect /dip3-it/d/diveintopython3-it-html-latest.zip http://gpiancastelli.altervista.org/dip3-it/d/diveintopython3-it-html-r$revision-$today.zip" >> build/d/.htaccess
mv "$htmldir" "$htmldir".html.bak

echo "building PDF distribution"
python3.1 util/flatten.py # outputs to build/single.html
pdfbasedir="$htmlbasedir"
pdfdir="$htmldir"
mkdir -p "$pdfdir"
pdffile="$pdfdir"/diveintopython3-it-r"$revision".pdf
prince --style=prince.css --output="$pdffile" build/single.html 2>build/prince.log
mkdir "$pdfdir"/esempi
cp esempi/*.jpg esempi/*.json esempi/*.pickle esempi/*.py esempi/*.txt esempi/*.xml "$pdfdir"/esempi/
cd build/ && \
    zip -9rq diveintopython3-it-pdf-r"$revision"-"$today".zip "$pdfbasedir" && \
cd ..
echo "Redirect /dip3-it/d/diveintopython3-it-pdf-latest.zip http://gpiancastelli.altervista.org/dip3-it/d/diveintopython3-it-pdf-r$revision-$today.zip" >> build/d/.htaccess
mv "$pdfdir" "$pdfdir".pdf.bak
mv build/single.html build/single.html.bak

# No JavaScript linting; just trust Mark on this one.
#
# No per-page dates; they don't really make much sense for translations.

echo "minimizing HTML"
# minimize HTML (NB: this script is quite fragile and relies on knowledge of how I write HTML)
# Yeah, I'll try and do it anyway, since I didn't really changed the original HTML structure
for f in *.html; do
  python3.1 util/htmlminimizer.py "$f" build/"$f" || die "Failed to minimize $f"
done

# build sitemap
ls build/*.html | sed -e "s|build/|http://gpiancastelli.altervista.org/dip3-it/|g" -e "s|/index.html|/|g" > build/sitemap.txt || die "Failed to build sitemap"

# minimize JS and CSS
echo "minimizing JS"
java -jar util/compiler.jar -js build/j/prettify.js > build/j/prettify.min.js && \
    java -jar util/compiler.jar -js build/j/dip3.js > build/j/dip3.min.js || die "Failed to minimize JS"

# combine jQuery and our script
echo "combining JS"
cat build/j/jquery.min.js build/j/prettify.min.js build/j/dip3.min.js > build/j/$revision.js && \
    sed -i -e "s|<script src=j/jquery.js></script>||g" build/*.html && \
    sed -i -e "s|<script src=j/prettify.js></script>||g" build/*.html && \
    sed -i -e "s|<script src=j/dip3.js>|<script src=j/${revision}.js>|g" build/*.html || die "Failed to combine JS"

echo "minimizing CSS"
java -jar util/yuicompressor-2.4.2.jar build/dip3.css > build/$revision.css && \
    java -jar util/yuicompressor-2.4.2.jar build/mobile.css > build/m-$revision.css && \
    java -jar util/yuicompressor-2.4.2.jar build/print.css > build/p-$revision.css && \
    sed -i -e "s|;}|}|g" build/$revision.css && \
    sed -i -e "s|;}|}|g" build/m-$revision.css && \
    sed -i -e "s|;}|}|g" build/p-$revision.css || die "Failed to minimize CSS"

# put CSS inline and remove unused CSS properties on a page-by-page basis
# minimize URLs by stripping "http:" prefix
# add asynchronous Google Analytics script in head (after inline styles)
echo "inlining CSS, minimizing URLs, adding evil tracking code"
ga=`cat j/ga.js`
for f in build/*.html; do
  css=`python2.6 util/lesscss.py "$f" "build/$revision.css"` || die "Failed to remove unused CSS"
  mobilecss=`python2.6 util/lesscss.py "$f" "build/m-$revision.css"` || die "Failed to remove unused CSS"
  printcss=`python2.6 util/lesscss.py "$f" "build/p-$revision.css"` || die "Failed to remove unused CSS"
  sed -i -e "s|<link rel=stylesheet href=dip3.css>|<style>${css}</style>|g" -e "s|<link rel=stylesheet media='only screen and (max-device-width: 480px)' href=mobile.css>|<style>@media screen and (max-device-width:480px){${mobilecss}}</style>|g" -e "s|<link rel=stylesheet media=print href=print.css>|<style>@media print{${printcss}}</style>|g" -e "s|</style><style>||g" "$f" || die "Failed to inline CSS"
  # files are not available from URL root
  sed -i -e "s|=http:|=|g" -e "s|href=index.html|href=/dip3-it|g" "$f" || die "Failed to minimize URLs"
  sed -i -e "s|</style>|</style>${ga}|g" "$f" || die "Failed to add evil tracking code"
done

# set file permissions (hg resets these, don't know why)
chmod 755 build/esempi build/j build/i build/d && \
#chmod 644 build/*.html build/*.css build/*.txt build/*.zip build/examples/* build/examples/.htaccess build/j/* build/j/.htaccess build/i/* build/i/.htaccess build/d/.htaccess build/.htaccess || die "Failed to reset file permissions"
    chmod 644 build/*.html build/*.css build/esempi/* build/j/* build/j/.htaccess build/i/* build/i/.htaccess build/d/.htaccess || die "Failed to reset file permissions"

# ship it!
#die "Aborting without publishing"
echo "publishing"
# using FTP to ship
HOST=$1
USER=$2
PASS=$3
# NB: won't upload some .htaccess files because of some server limitations
ftp -inv $HOST <<EOF
quote user $USER
quote pass $PASS
cd dip3-it
lcd build
mput *.css
mput *.html
cd j
lcd j
mput *.js
cd ../i
lcd ../i
mput *.png
cd ../esempi
lcd ../esempi
mput *.py
mput *.xml
mput *.txt
cd ../d
lcd ..
mput *.zip
cd ..
lcd d
put .htaccess
bye
EOF