sonjasrasselbande/scripts/check.py
torsten 2c7cae11a5 Add static website for Sonjas Rasselbande
- src/: layout, page fragments, CSS design system, vanilla JS, .htaccess,
  service worker, manifest, SVG artwork
- scripts/: python build (no npm), HTML/link checks, image + photo pipeline,
  FTP deploy with size verification
- docs/: analysis of the old Wix site, architecture notes, deployment
  learnings (FollowSymLinks 500, FTPS data channel, Let's Encrypt),
  open items, source material
- site/ is generated (make build) and gitignored
2026-11-29 19:26:25 +01:00

110 lines
3.7 KiB
Python
Executable file
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""Prüft die gebaute Website: HTML-Struktur, interne Links, Pflichtdateien.
Aufruf: python3 scripts/check.py (nach scripts/build.py)
Exit-Code 1, wenn Probleme gefunden wurden – so auch in CI nutzbar.
"""
from __future__ import annotations
import pathlib
import re
import sys
from html.parser import HTMLParser
ROOT = pathlib.Path(__file__).resolve().parent.parent
SITE = ROOT / "site"
VOID = {"br", "img", "meta", "link", "input", "hr", "source", "area", "base",
"col", "embed", "track", "wbr"}
REQUIRED = ["index.html", "impressum.html", "datenschutz.html", "404.html",
"robots.txt", "sitemap.xml", "manifest.webmanifest", "sw.js",
".htaccess", "assets/css/style.css", "assets/js/main.js"]
class Balance(HTMLParser):
def __init__(self) -> None:
super().__init__(convert_charrefs=True)
self.stack: list[tuple[str, tuple[int, int]]] = []
self.errors: list[str] = []
def handle_starttag(self, tag, attrs):
if tag in VOID:
return
self.stack.append((tag, self.getpos()))
def handle_endtag(self, tag):
if not self.stack:
self.errors.append(f"zusätzliches </{tag}> in Zeile {self.getpos()[0]}")
return
if self.stack[-1][0] == tag:
self.stack.pop()
else:
self.errors.append(
f"</{tag}> in Zeile {self.getpos()[0]} passt nicht zu <{self.stack[-1][0]}> "
f"aus Zeile {self.stack[-1][1][0]}"
)
for i in range(len(self.stack) - 1, -1, -1):
if self.stack[i][0] == tag:
del self.stack[i:]
return
def main() -> int:
problems: list[str] = []
if not SITE.exists():
print("site/ fehlt – bitte zuerst 'python3 scripts/build.py' ausführen.")
return 1
for name in REQUIRED:
if not (SITE / name).exists():
problems.append(f"Pflichtdatei fehlt: {name}")
pages = sorted(SITE.glob("*.html"))
if not pages:
problems.append("Keine HTML-Seiten gefunden")
for page in pages:
html = page.read_text(encoding="utf-8")
parser = Balance()
parser.feed(html)
if parser.errors:
problems.extend(f"{page.name}: {e}" for e in parser.errors[:3])
if parser.stack:
problems.append(f"{page.name}: nicht geschlossen {[t for t, _ in parser.stack][:4]}")
if "{{" in html:
problems.append(f"{page.name}: unersetzter Platzhalter")
if not re.search(r'<html[^>]*\blang="de"', html):
problems.append(f"{page.name}: lang=\"de\" fehlt")
if html.count("<h1") != 1:
problems.append(f"{page.name}: {html.count('<h1')}× <h1> (erwartet: 1)")
for match in re.finditer(r'(?:href|src)="([^"#][^"]*)"', html):
target = match.group(1)
if target.startswith(("http://", "https://", "mailto:", "tel:", "data:")):
continue
if not (SITE / target.split("#")[0]).exists():
problems.append(f"{page.name}: toter interner Link {target}")
for match in re.finditer(r'<img[^>]*>', html):
tag = match.group(0)
if "alt=" not in tag:
problems.append(f"{page.name}: <img> ohne alt – {tag[:60]}")
total_kb = sum(f.stat().st_size for f in SITE.rglob("*") if f.is_file()) / 1024
print(f"{len(pages)} Seiten geprüft, {total_kb:.0f} KB gesamt")
if problems:
print(f"\n{len(problems)} Problem(e):")
for p in problems:
print(f" ✗ {p}")
return 1
print("✓ HTML-Struktur, Links, Bilder und Pflichtdateien in Ordnung")
return 0
if __name__ == "__main__":
sys.exit(main())