import os
import re
showcase_dir = "public/showcase/klz-cables"
main_css = "wp-content/cache/breeze-minification/css/breeze_klz-cables-com-1-10895.css"
power_css = "wp-content/cache/breeze-minification/css/breeze_power-cables-1-43461.css"
fallback_img = "wp-content/uploads/2025/04/3.webp"
def fix_html_file(file_path):
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
modified = False
# Fix CSS
links = re.findall(r']*rel=["\']stylesheet["\'][^>]*href=["\']([^"\']*)["\'][^>]*>', content)
links += re.findall(r']*href=["\']([^"\']*)["\'][^>]*rel=["\']stylesheet["\'][^>]*>', content)
for link in links:
file_dir = os.path.dirname(file_path)
rel_link = os.path.join(file_dir, link)
if not os.path.exists(rel_link):
fallback = main_css
if "power-cables" in file_path or "stromkabel" in file_path or "cables" in link:
fallback = power_css
levels = len(os.path.relpath(file_dir, showcase_dir).split(os.sep))
if os.path.relpath(file_dir, showcase_dir) == ".": levels = 0
new_link = ("../" * levels) + fallback
content = content.replace(link, new_link)
modified = True
# Fix Images (src)
imgs = re.findall(r']*src=["\']([^"\']*)["\'][^>]*>', content)
for img in imgs:
if img.startswith("data:"): continue
file_dir = os.path.dirname(file_path)
rel_img = os.path.join(file_dir, img)
if not os.path.exists(rel_img):
levels = len(os.path.relpath(file_dir, showcase_dir).split(os.sep))
if os.path.relpath(file_dir, showcase_dir) == ".": levels = 0
new_img = ("../" * levels) + fallback_img
content = content.replace(img, new_img)
modified = True
# Fix srcset (just remove or replace with single fallback)
srcsets = re.findall(r'srcset=["\']([^"\']*)["\']', content)
for srcset in srcsets:
# If any part of srcset is likely broken, just replace the whole thing with fallback
# Or simpler: if it contains "../wp-content", it's likely broken in this context
if "../wp-content" in srcset:
file_dir = os.path.dirname(file_path)
levels = len(os.path.relpath(file_dir, showcase_dir).split(os.sep))
if os.path.relpath(file_dir, showcase_dir) == ".": levels = 0
new_img = ("../" * levels) + fallback_img
content = content.replace(f'srcset="{srcset}"', f'srcset="{new_img} 800w"')
modified = True
if modified:
with open(file_path, 'w', encoding='utf-8') as f:
f.write(content)
for root, dirs, files in os.walk(showcase_dir):
for file in files:
if file.endswith(".html"):
fix_html_file(os.path.join(root, file))