Initial commit
This commit is contained in:
91
fix_links.py
Normal file
91
fix_links.py
Normal file
@@ -0,0 +1,91 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Zet absolute URLs (https://correctvloerverwarming.nl/...) om naar relatieve paden
|
||||
in alle HTML en CSS bestanden.
|
||||
"""
|
||||
|
||||
import os
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
BASE_DIR = Path("/home/anisy/projects/websites/correctvloerverwarming/correctvloerverwarming.nl")
|
||||
|
||||
DOMAINS = [
|
||||
"https://www.correctvloerverwarming.nl",
|
||||
"http://www.correctvloerverwarming.nl",
|
||||
"https://correctvloerverwarming.nl",
|
||||
"http://correctvloerverwarming.nl",
|
||||
]
|
||||
|
||||
|
||||
def get_relative_prefix(html_file: Path) -> str:
|
||||
"""Bereken het relatieve pad terug naar de root."""
|
||||
depth = len(html_file.relative_to(BASE_DIR).parts) - 1
|
||||
if depth == 0:
|
||||
return "./"
|
||||
return "../" * depth
|
||||
|
||||
|
||||
def fix_html_file(html_path: Path) -> bool:
|
||||
"""Vervang absolute URLs door relatieve paden in een HTML bestand."""
|
||||
try:
|
||||
content = html_path.read_text(encoding="utf-8", errors="ignore")
|
||||
original = content
|
||||
prefix = get_relative_prefix(html_path)
|
||||
|
||||
for domain in DOMAINS:
|
||||
# href="https://domain.nl/pad" -> href="./pad"
|
||||
content = content.replace(f'href="{domain}/', f'href="{prefix}')
|
||||
content = content.replace(f"href='{domain}/", f"href='{prefix}")
|
||||
# src="https://domain.nl/pad" -> src="./pad"
|
||||
content = content.replace(f'src="{domain}/', f'src="{prefix}')
|
||||
content = content.replace(f"src='{domain}/", f"src='{prefix}")
|
||||
# action="https://domain.nl/pad" -> action="./pad"
|
||||
content = content.replace(f'action="{domain}/', f'action="{prefix}')
|
||||
# content="https://domain.nl" (meta canonical etc.)
|
||||
content = content.replace(f'content="{domain}/', f'content="{prefix}')
|
||||
|
||||
if content != original:
|
||||
html_path.write_text(content, encoding="utf-8")
|
||||
return True
|
||||
except Exception as e:
|
||||
print(f" Fout bij {html_path}: {e}")
|
||||
return False
|
||||
|
||||
|
||||
def fix_css_file(css_path: Path) -> bool:
|
||||
"""Vervang absolute URLs in CSS bestanden."""
|
||||
try:
|
||||
content = css_path.read_text(encoding="utf-8", errors="ignore")
|
||||
original = content
|
||||
|
||||
for domain in DOMAINS:
|
||||
content = content.replace(f"url('{domain}/", "url('../")
|
||||
content = content.replace(f'url("{domain}/', 'url("../')
|
||||
content = content.replace(f"url({domain}/", "url(../")
|
||||
|
||||
if content != original:
|
||||
css_path.write_text(content, encoding="utf-8")
|
||||
return True
|
||||
except Exception as e:
|
||||
print(f" Fout bij {css_path}: {e}")
|
||||
return False
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("=== Absolute links omzetten naar relatief ===\n")
|
||||
|
||||
html_updated = 0
|
||||
for html_path in BASE_DIR.rglob("*.html"):
|
||||
if fix_html_file(html_path):
|
||||
html_updated += 1
|
||||
|
||||
print(f"HTML: {html_updated} bestanden bijgewerkt.")
|
||||
|
||||
css_updated = 0
|
||||
for css_path in BASE_DIR.rglob("*.css"):
|
||||
if fix_css_file(css_path):
|
||||
css_updated += 1
|
||||
|
||||
print(f"CSS: {css_updated} bestanden bijgewerkt.")
|
||||
print("\nKlaar!")
|
||||
Reference in New Issue
Block a user