Created
March 2, 2025 17:30
-
-
Save opencoca/afb322f8aff59385269b1f007bab9fe7 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python3 | |
""" | |
Google Fonts CSS Processor - Startr LLC | |
Copyright (c) 2025 Startr LLC, MIT License | |
Processes Google Fonts CSS to: | |
1. Generate wget commands to download all font files | |
2. Create revised CSS pointing to local font files | |
3. Organize fonts with semantic filenames | |
Usage: | |
python process_fonts.py input.css | |
Inputs: | |
- CSS file containing @font-face rules from Google Fonts | |
Outputs: | |
- revised.css (local font paths) | |
- download_fonts.sh (download script) | |
Dependencies: Python 3.6+ | |
""" | |
import re | |
import os | |
import sys | |
def process_css(input_file): | |
"""Main processing function that handles CSS transformation""" | |
with open(input_file, 'r') as f: | |
css = f.read() | |
# Pattern to extract font-face blocks with preceding comments | |
pattern = re.compile(r'/\*\s*(.*?)\s*\*/(.*?@font-face\s*\{.*?src:\s*url\((.*?)\).*?\})', re.DOTALL) | |
matches = re.findall(pattern, css) | |
download_commands = [] | |
new_css = [] | |
for comment, block, url in matches: | |
# Extract font metadata | |
font_family = re.search(r"font-family:\s*['\"](.*?)['\"]", block).group(1) | |
style = re.search(r"font-style:\s*(.*?);", block).group(1).strip() | |
weight = re.search(r"font-weight:\s*(.*?);", block).group(1).strip().replace(' ', '-') | |
clean_comment = comment.strip().lower().replace(' ', '-') | |
# Generate filename and commands | |
filename = f"{font_family.replace(' ', '-')}-{style}-{weight}-{clean_comment}.woff2" | |
download_commands.append(f"wget '{url}' -O '{filename}'") | |
# Update CSS block | |
new_block = block.replace(url, filename) | |
new_css.append(f"/* {comment} */{new_block}") | |
# Write output files | |
with open('revised.css', 'w') as f: | |
f.write('\n\n'.join(new_css)) | |
with open('download_fonts.sh', 'w') as f: | |
f.write("#!/bin/bash\n") | |
f.write('# Auto-generated download script - Startr LLC\n') | |
f.write('# Execute this to download all font files\n\n') | |
f.write('\n'.join(download_commands)) | |
os.chmod('download_fonts.sh', 0o755) | |
if __name__ == "__main__": | |
if len(sys.argv) != 2: | |
print("Usage: python process_fonts.py <input.css>") | |
sys.exit(1) | |
process_css(sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment