Files
RuView/scripts/verify-readme-visuals.py

30 lines
1.9 KiB
Python

#!/usr/bin/env python3
"""Verify the generated SVGs and GitHub-compatible image link wrappers."""
from pathlib import Path
import json,re
from urllib.parse import urlparse
from readme_visuals_xml import read_svg,resolve_repo_file
ROOT=Path(__file__).resolve().parents[1];NS='{http://www.w3.org/2000/svg}'
manifest=json.loads((ROOT/'assets/readme/manifest.json').read_text(encoding='utf-8'));readme=(ROOT/'README.md').read_text(encoding='utf-8');total=0
for asset in manifest['assets']:
file=resolve_repo_file(ROOT,asset['file'],asset=True);data,root=read_svg(file);raw=data.decode('utf-8');total+=len(data)
assert root.tag==NS+'svg' and root.get('viewBox'),asset['file']
assert root.find(NS+'title') is not None and root.find(NS+'desc') is not None
ids=[e.get('id') for e in root.iter() if e.get('id')];assert len(ids)==len(set(ids))
assert 'prefers-reduced-motion:reduce' in raw and '@keyframes' in raw
for e in root.iter():
assert e.tag not in [NS+'script',NS+'foreignObject',NS+'image'],asset['file']
assert all(not k.lower().startswith('on') for k in e.attrib)
assert not any(k.endswith('href') for k in e.attrib),'Links belong to README wrappers'
assert not re.search(r'url\((?!#)',raw),'Remote dependency'
target=asset['target'];parsed=urlparse(target)
if parsed.scheme:
assert parsed.scheme=='https' and parsed.hostname in ['cognitum.one','huggingface.co']
else:resolve_repo_file(ROOT,target.split('#')[0])
escaped=re.escape(asset['file']);destination=re.escape(target)
linked=bool(re.search(r'\[!\[[^\]]*\]\('+escaped+r'\)\]\('+destination+r'\)',readme))
linked=linked or bool(re.search(r'<a href="'+destination+r'">\s*<img src="'+escaped+r'"',readme))
assert linked,('Missing clickable wrapper',asset['file'])
assert total<100000
print(f'PASS: {len(manifest["assets"])} accessible animated SVGs, link targets and README wrappers; {total:,} bytes. No scripts, remote resources or SVG-internal links.')