Created
March 8, 2024 02:18
-
-
Save alifeee/8607c787a0c60684827b5f07bbb95a5d to your computer and use it in GitHub Desktop.
Convert a font (`.woff` or otherwise) filename to CSS classes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# converts a font (.woff) filename to a CSS @font-face and class-selector based on filename | |
# example: | |
# ./filename_to_css.sh "path/to/my-font.woff" | |
# with multiple files | |
# find path/to/fonts -name "*.woff" -exec ./filename_to_css.sh {} \; | |
# get name from regex match | |
name_regex='\/([^\/]*)\.woff$' | |
if [[ $1 =~ $name_regex ]]; then | |
name="${BASH_REMATCH[1]}" | |
else | |
echo "Error: filename must end in .woff" | |
exit 1 | |
fi | |
# lowercase and replace spaces with hyphens | |
name_without_spaces=$(echo $name | tr '[:upper:]' '[:lower:]' | tr ' ' '-') | |
echo '@font-face { | |
font-family: "'$name'"; | |
src: url("'$1'") format('woff'); | |
} | |
.font--'$name_without_spaces' { | |
font-family: "'$name'" !important; | |
} | |
' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment