Help with self-hosting fonts on *.elm-lang.org

Based on the information here, I created a script that should download all the files needed for modern browsers. You would run it like this:

./get-fonts.sh "fonts" "Source+Sans+Pro|Source+Code+Pro"

The file itself is defined as follows:

#!/bin/bash

DIR="$1"
FAMILY="$2"

mkdir -p $DIR

function download {
	CSS="$DIR/$1.css"
	curl -s -o "$CSS" -H "User-Agent: $2" "https://fonts.googleapis.com/css?family=$FAMILY"

	for WOFF2_URL in $(sed -n 's!.*\(https.*\.woff2\).*!\1!p' $CSS)
	do
		WOFF2="$DIR/$(echo $WOFF2_URL | grep -o '[a-zA-Z0-9_-]*\.woff2')"
		if [ ! -f $WOFF2 ]; then
			curl -s -o "$WOFF2" "$WOFF2_URL"
		fi
	done

	sed -i "" "s!https.*/\([a-zA-Z0-9_-]*\.woff2\)!/$DIR/\1!g" $CSS
}

download "_mac" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:73.0) Gecko/20100101 Firefox/73.0"
download "_win" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36"
download "_lin" "Mozilla/5.0 (X11; Linux x86_64; rv:74.0) Gecko/20100101 Firefox/74.0"

It’s not strictly necessary to serve _lin.css and _win.css but it’s easy to change. It also seems pretty easy to add an _old.css if you want to support that as well.

Thanks to everyone for helping sleuth and gather data on this!

1 Like