From 64d323874592061627129fef0d99b5c180d725dd Mon Sep 17 00:00:00 2001 From: askiiart Date: Sat, 4 Jan 2025 18:39:13 -0600 Subject: [PATCH] add font support --- README.md | 4 ---- owug.py | 18 ++++++++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 59bc66a..980d0d3 100644 --- a/README.md +++ b/README.md @@ -32,10 +32,6 @@ python3 owug.py 'https://example.com' 'out.html' OWUG is licensed under `GPL-3.0-only`. -## TODO - -- Add font support - --- There's definitely not a secret patch to add JS support [here](/secret-js.patch). diff --git a/owug.py b/owug.py index 4fd6d4a..f4bcdf1 100644 --- a/owug.py +++ b/owug.py @@ -52,6 +52,24 @@ head = soup.find_all('link', rel='stylesheet') for item in head: url = absolute_url(item.attrs['href'], domain_thing) style_data = requests.get(url).content.decode() + + # hardcode fonts + index = -1 + while True: + index = style_data.find('url(', index + 1) + if index == -1: + break + + original_url = style_data[index + 5 : style_data.find(')', index) - 1] + absolute = absolute_url( + original_url, domain_thing + ) + mime_type = requests.head(absolute).headers['Content-Type'] + as_base64 = b64encode_as_string(requests.get(absolute).content) + new_url = f'data:{mime_type};base64,{as_base64}' + + style_data = style_data.replace(original_url, new_url) + head = soup.find('head') new_tag = soup.new_tag('style') new_tag.string = style_data