diff --git a/README.md b/README.md index 543c1b1..ba2a358 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,8 @@ the terms. ### Options
+
-u URL, --url=URL
+
Dowload the fonts specified in the URL. Note: You can mix it with normal arguments (See below).
-f FORMAT, --format=FORMAT
Download the specified set of webfont formats from Google's servers. FORMAT is a comma-separated list of identifiers for @@ -66,6 +68,7 @@ the terms. optional font style of "italic" (or "i") for italics. In [EBNF](https://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_form): ```ebnf + url = URL fontspec = fontname, [ ":", [ fontweight ], [ fontstyle ] ] fontweight = number | "bold" number = { digit } @@ -76,7 +79,7 @@ the terms. While Google's servers will accept other inputs and abbreviations for font weight and font style, they are not supported by this script. - Note that your font spec should *not* be URL-encoded and only one font weight + Note: if you don't use the URL argument, your font spec should *not* be URL-encoded and only one font weight is supported per font specification. If you want to download multiple font weights or styles, provide multiple font specs. @@ -92,3 +95,12 @@ google-font-download \ "Open Sans:300" "Open Sans:400" "Open Sans:400italic" \ "Open Sans:700" "Open Sans:700italic" ``` +or in URL format: +```bash +google-font-download --url="https://fonts.google.com/?selection.family=Open+Sans:300,400,400i,700,700i" +``` +You can also mix the arguments: +```bash +google-font-download --url="https://fonts.google.com/?selection.family=Open+Sans:300,400,400i" \ +"Open Sans:700" "Open Sans:700i" +``` diff --git a/google-font-download b/google-font-download index 001e905..2fba0db 100755 --- a/google-font-download +++ b/google-font-download @@ -28,6 +28,7 @@ # - Chris Jung, campino2k.de, and # - Robert, github.com/rotx. # - Thomas Papamichail, https://gist.github.com/pointergr +# - Musikid ## ## @@ -47,6 +48,7 @@ css="font.css" lang="latin" format="all" url="https://fonts.googleapis.com/css" +urlref="" # Usage message usage() { @@ -118,6 +120,13 @@ misuse_exit() { usage } +# function that act like split in perl. Syntax: splitarr IFS $var array +splitarr() { + if [[ "$2" =~ $1 ]]; then + IFS="$1" read -r -a "$3" <<< "$2" + fi +} + # Check for modern getopt(1) that quotes correctly; see #1 for rationale ret=0 modern_getopt=1 @@ -129,7 +138,7 @@ fi # Parse options if [ $modern_getopt -eq 1 ]; then ret=0 - temp=$(getopt -o f:hl:o: --long format:,help,languages:,output -n "${0:-google-font-download}" -- "$@") || ret=$? + temp=$(getopt -o u:f:hl:o: --long url:,format:,help,languages:,output -n "${0:-google-font-download}" -- "$@") || ret=$? if [ $ret -ne 0 ]; then echo >&2 usage @@ -149,7 +158,7 @@ else ret=0 # shellcheck disable=SC2048,SC2086 - temp=$(getopt f:hl:o: $*) || ret=$? + temp=$(getopt u:f:hl:o: $*) || ret=$? if [ $ret -ne 0 ]; then echo >&2 usage @@ -162,6 +171,10 @@ fi eval set -- "$temp" while true; do case "$1" in + -u|--url) + urlref=$2 + shift 2 + ;; -f|--format) format=$2 shift 2 @@ -185,11 +198,102 @@ while true; do esac done -# Validate font family input + declare -a families families=() +declare -a commas +commas=() +# Detect and parse url +if [[ $urlref != "" ]]; then + urlref=$(echo "$urlref" | grep -Po 'family=\K(.*)') + if [[ "$urlref" =~ \| ]]; then + splitarr '|' "$urlref" temp + for line in "${temp[@]}" + do + if [[ "$line" =~ \+ ]] || [[ "$line" =~ , ]] ; then + if [[ "$line" =~ \+ ]]; then + line=${line//\+/ } + fi + if [[ "$line" =~ , ]]; then + number=$(echo "$line" | grep -Po ':\K(.*)') + name=$(echo "$line" | grep -Po '(.*):') + splitarr ',' "$number" commas + for (( i = 0; i < ${#commas[@]}; i++ )); do + families+=("$name${commas[$i]}") + done + else + families+=("$line") + fi + else + families+=("$line") + fi + done + else + if [[ "$urlref" =~ \+ ]] || [[ "$urlref" =~ , ]] ; then + if [[ "$urlref" =~ \+ ]]; then + line=${line//\+/ } + fi + if [[ "$urlref" =~ , ]]; then + number=$(echo "$urlref" | grep -Po ':\K(.*)') + name=$(echo "$urlref" | grep -Po '(.*):') + splitarr ',' "$number" commas + for (( i = 0; i < ${#commas[@]}; i++ )); do + families+=("$name${commas[$i]}") + done + else + families+=("$line") + fi + fi + fi +fi + +# Validate font family input for family do + # Directly parse url + if [[ "$family" =~ http ]]; then + family=$(echo "$family" | grep -Po 'family=\K(.*)') + if [[ "$family" =~ \| ]]; then + splitarr '|' "$family" temp + for line in "${temp[@]}" + do + if [[ "$line" =~ \+ ]] || [[ "$line" =~ , ]] ; then + if [[ "$line" =~ \+ ]]; then + line=${line//\+/ } + fi + if [[ "$line" =~ , ]]; then + number=$(echo "$line" | grep -Po ':\K(.*)') + name=$(echo "$line" | grep -Po '(.*):') + splitarr ',' "$number" commas + for (( i = 0; i < ${#commas[@]}; i++ )); do + families+=("$name${commas[$i]}") + done + else + families+=("$line") + fi + else + families+=("$line") + fi + done + else + if [[ "$family" =~ \+ ]] || [[ "$family" =~ , ]] ; then + if [[ "$family" =~ \+ ]]; then + family=${family//\+/ } + fi + if [[ "$family" =~ , ]]; then + number=$(echo "$family" | grep -Po ':\K(.*)') + name=$(echo "$family" | grep -Po '(.*):') + splitarr ',' "$number" commas + for (( i = 0; i < ${#commas[@]}; i++ )); do + families+=("$name${commas[$i]}") + done + else + families+=("$family") + fi + fi + fi + else families+=("$family") + fi done if [ ${#families[@]} -eq 0 ]; then misuse_exit "No font families given" diff --git a/test/Makefile b/test/Makefile index 3481da9..c953dc1 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,4 +1,4 @@ -TESTS=eot svg ttf woff woff2 fonts-with-spaces multiple-fonts format-param output-param font-weight font-style +TESTS=eot svg ttf woff woff2 fonts-with-spaces multiple-fonts format-param output-param font-weight font-style url-arg TUT=../../google-font-download # tests use bashisms, avoid failures on systems where dash is used @@ -73,3 +73,8 @@ font-style: $(V)echo "---> Testing font style support" $(V)mkdir -p $@ && (cd $@ && $(TUT) -f woff "Ubuntu:700italic" && grep "font-style: italic;" "font.css" >/dev/null); ret=$$?; rm -rf $@ && exit $$ret $(V)echo " OK" + +url-arg: + $(V)echo "---> Testing download with a URL" + $(V)mkdir -p $@ && (cd $@ && $(TUT) -f woff -u "https://fonts.google.com/?query=lora&selection.family=Lora|Ubuntu|Roboto:300,400|Mukta+Malar" && [ -f "Ubuntu.woff" ] && [ -f "Lora.woff" ] && [ -f "Roboto_300.woff" ] && [ -f "Roboto_400.woff" ] && [ -f "Mukta_Malar.woff" ]); ret=$$?; rm -rf $@ && exit $$ret + $(V)echo " OK"