chore: inline online fonts with base64

This commit is contained in:
Yaotian-Liu 2023-03-16 01:10:59 +08:00
parent c248bacec8
commit dcef609fe2
No known key found for this signature in database
GPG key ID: 4AEEE42596AA7552
4 changed files with 2659 additions and 44 deletions

2
.gitignore vendored
View file

@ -20,3 +20,5 @@ data.json
# Exclude macOS Finder (System Explorer) View States # Exclude macOS Finder (System Explorer) View States
.DS_Store .DS_Store
tmp/

36
extract_katexcss.py Normal file
View file

@ -0,0 +1,36 @@
import re
import urllib.request
import base64
import os
# define a function to encode a file as base64
def encode_file_as_base64(filename):
print("processing: ", filename)
url_base = "https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.11.1/fonts/"
url = url_base + filename
if not os.path.isfile("tmp/" + filename):
urllib.request.urlretrieve(url, "tmp/" + filename)
with open("tmp/" + filename, 'rb') as f:
return base64.b64encode(f.read()).decode("utf-8")
# read the CSS file into a string
with open('katex.min.css', 'r') as f:
css_string = f.read()
# define the regular expression pattern
pattern = r'url\((.*?)\) format\((.*?)\)'
# define a function to replace the matched pattern with the base64-encoded data URI
def replace_match(match):
url = match.group(1)
filename = url[6:]
format = match.group(2)
base64_code = encode_file_as_base64(filename)
return f'url(data:application/{filename};charset=utf-8;base64,{base64_code}) format({format})'
new_css_string = re.sub(pattern, replace_match, css_string)
with open('new_styles.css', 'w') as f:
f.write(new_css_string)

1237
katex.min.css vendored Normal file

File diff suppressed because it is too large Load diff

1424
styles.css

File diff suppressed because one or more lines are too long