Skip to content

Commit

Permalink
Add support to parse directly URLs.
Browse files Browse the repository at this point in the history
Closes: #16
  • Loading branch information
saidsay-so authored and neverpanic committed Jan 2, 2018
1 parent 921453b commit ba0f7fd
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 5 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ the terms.
### Options

<dl>
<dt><code>-u URL</code>, <code>--url=URL</code></dt>
<dd>Dowload the fonts specified in the URL. Note: You can mix it with normal arguments (See below).</dd>
<dt><code>-f FORMAT</code>, <code>--format=FORMAT</code></dt>
<dd>Download the specified set of webfont formats from Google's servers.
<code>FORMAT</code> is a comma-separated list of identifiers for
Expand Down Expand Up @@ -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 }
Expand All @@ -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.

Expand All @@ -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"
```
110 changes: 107 additions & 3 deletions google-font-download
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
# - Chris Jung, campino2k.de, and
# - Robert, github.com/rotx.
# - Thomas Papamichail, https://gist.github.com/pointergr
# - Musikid
##

##
Expand All @@ -47,6 +48,7 @@ css="font.css"
lang="latin"
format="all"
url="https://fonts.googleapis.com/css"
urlref=""

# Usage message
usage() {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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"
Expand Down
7 changes: 6 additions & 1 deletion test/Makefile
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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"

0 comments on commit ba0f7fd

Please sign in to comment.