-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender.py
More file actions
49 lines (36 loc) · 1.67 KB
/
render.py
File metadata and controls
49 lines (36 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from bs4 import BeautifulSoup
from pathlib import Path
def render_now(input_html, output_html):
with open(input_html, 'r', encoding='utf-8') as f:
soup = BeautifulSoup(f.read(), 'html.parser')
# Remove Tailwind CDN (not needed, everything is inline styled)
for script in soup.find_all('script', src=lambda x: x and 'tailwind' in x):
script.decompose()
# Inline all iframe HTML files
for iframe in soup.find_all('iframe'):
src = iframe.get('src', '')
if not src or src.startswith('http'):
continue
try:
with open(src, 'r', encoding='utf-8') as f:
iframe_soup = BeautifulSoup(f.read(), 'html.parser')
div = soup.new_tag('div')
div['style'] = iframe.get('style', '')
# Move iframe styles to head
for style in iframe_soup.find_all('style'):
soup.head.append(style)
# Move iframe content to div
if iframe_soup.body:
for child in list(iframe_soup.body.children):
div.append(child)
# Move iframe scripts to body
for script in iframe_soup.find_all('script'):
soup.body.append(script)
iframe.replace_with(div)
print(f"✓ {src}")
except Exception as e:
print(f"✗ {src}: {e}")
with open(output_html, 'w', encoding='utf-8') as f:
f.write(str(soup.prettify()))
print(f"\n✅ {output_html} ({Path(output_html).stat().st_size / 1024:.0f} KB)")
render_now('blog.html', 'rendered_blog.html')