38 lines
1.9 KiB
Python
38 lines
1.9 KiB
Python
"""Build an offline hierarchy; individual pages embed every runtime dependency."""
|
|
from pathlib import Path
|
|
import hashlib
|
|
import json
|
|
import re
|
|
|
|
root = Path(__file__).resolve().parents[1]
|
|
source = root / 'assets/animejs'
|
|
vendor = root / 'assets/vendor/anime.umd.min.js'
|
|
entries = [entry for entry in json.loads((root / 'vendor-manifest.json').read_text(encoding='utf-8'))
|
|
if entry['path'] == 'assets/vendor/anime.umd.min.js']
|
|
def digest(path):
|
|
return hashlib.sha256(path.read_bytes()).hexdigest()
|
|
if len(entries) != 1 or digest(vendor) != entries[0]['sha256']:
|
|
raise ValueError('Missing, duplicate or mismatched Anime vendor fingerprint')
|
|
out = root / 'dist/animejs-gallery-0.5.0'
|
|
files = {}
|
|
for page in sorted(source.rglob('*.html')):
|
|
text = page.read_text(encoding='utf-8')
|
|
def style(match):
|
|
return '<style>' + (page.parent / match[1]).read_text(encoding='utf-8') + '</style>'
|
|
def script(match):
|
|
js = re.sub(r'//# sourceMappingURL=.*', '', (page.parent / match[1]).read_text(encoding='utf-8'))
|
|
return '<script>' + js.replace('</script', r'<\/script') + '</script>'
|
|
text = re.sub(r'<link rel="stylesheet" href="([^"]+)">', style, text)
|
|
text = re.sub(r'<script src="([^"]+)"></script>', script, text)
|
|
if 'ANIME_LICENSE' in text:
|
|
raise ValueError('Unresolved license placeholder')
|
|
target = out / page.relative_to(source)
|
|
target.parent.mkdir(parents=True, exist_ok=True)
|
|
target.write_text(text, encoding='utf-8')
|
|
files[str(target.relative_to(out))] = digest(target)
|
|
inputs = [*sorted(source.rglob('*')), vendor, root / 'assets/vendor/anime-LICENSE.md']
|
|
report = {'package': out.name, 'entry': 'index.html', 'anime': '4.5.0', 'examples': 5,
|
|
'files': files, 'inputs': {str(p.relative_to(root)): digest(p) for p in inputs if p.is_file()}}
|
|
(root / 'dist/animejs-gallery-manifest.json').write_text(json.dumps(report, indent=2) + '\n')
|
|
print(json.dumps(report, indent=2))
|