From c541151dff2dcedc5ee22deb28fec724f5c6196e Mon Sep 17 00:00:00 2001 From: askiiart Date: Sat, 4 Jan 2025 17:19:32 -0600 Subject: [PATCH] add support for css and making relative links absolute --- README.md | 8 +++++--- owug.py | 20 +++++++++++++++++++- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 93b2f6c..dc6828f 100644 --- a/README.md +++ b/README.md @@ -4,12 +4,15 @@ With "liberty" and JS for none. --- -It's a thing to make a webpage into a single file with no external dependencies, with, of course, "liberty" (no options) and JS for none (it doesn't do JS). +This makes any page a portable, single file with no external dependencies - with, of course, "liberty" (no options) and JS for none (it doesn't do JS). Currently it supports hardcoding: - favicons - images +- CSS + +And also fixes any relative links to be absolute. ## Usage @@ -23,5 +26,4 @@ OWUG is licensed under `GPL-3.0-only`. ## TODO -- Add CSS support -- Add support for making relative links into absolute ones (`/.a.html` -> `https://example.com/a.html`) +- Add font support diff --git a/owug.py b/owug.py index c20e32f..7efb86f 100644 --- a/owug.py +++ b/owug.py @@ -29,7 +29,7 @@ soup = BeautifulSoup(html, 'html5lib') # hardcode favicon favicons = soup.find_all('link', rel='icon') for favicon in favicons: - if favicon.attrs['rel'].count('icon') > 0: + if 'icon' in favicon.attrs['rel']: url = absolute_url(favicon.attrs['href'], domain_thing) mime_type = requests.head(url).headers['Content-Type'] @@ -47,5 +47,23 @@ for item in imgs: new_url = f'data:{mime_type};base64,{as_base64}' item.attrs['src'] = new_url +# hardcode css +# note: not sure it matters, but this puts the CSS in +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() + head = soup.find('head') + new_tag = soup.new_tag('style') + new_tag.string = style_data + soup.head.append(new_tag) + item.decompose() + +# change relative links to absolute +links = soup.find_all('link') +for item in links: + if 'icon' not in item.attrs['rel']: + item.attrs['href'] = absolute_url(item.attrs['href'], domain_thing) + with open(sys.argv[2], 'wt') as f: f.write(str(soup))