102 lines
6.3 KiB
Python
102 lines
6.3 KiB
Python
#!/usr/bin/env python3
|
|
"""Package the live Ziran/Canvas Kryon website."""
|
|
import html
|
|
import hashlib
|
|
import os
|
|
from pathlib import Path
|
|
import shutil
|
|
import subprocess
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
SITE = ROOT / 'docs/site'
|
|
NAV = [
|
|
('index.html', 'Overview'), ('getting-started.html', 'Guide'),
|
|
('widgets.html', 'Components'), ('apps.html', 'Apps'), ('docs.html', 'Docs'),
|
|
]
|
|
MAINTAINED = [
|
|
('index.html', 'Kryon — native TSX screens with Ziran logic', 'Build desktop and web interfaces from native TSX screens with Ziran logic. Explore the controls behind Terminal, Explorer, and Workbook.'),
|
|
('getting-started.html', 'Getting started — Kryon', 'Author native TSX screens in a Ziran project, import widgets, and run the maintained local tests.'),
|
|
('widgets.html', 'Components — Kryon', 'Browse native TSX component examples with source, APIs, options and status.'),
|
|
('modules.html', 'Modules — Kryon', 'The maintained TSX gallery builds Kryon widgets, Plot charts, DataViews controls, focused inputs and overlays.'),
|
|
('apps.html', 'Apps — Kryon', 'Explore Terminal, Explorer, Workbook, and other desktop and browser apps built with Kryon modules.'),
|
|
('docs.html', 'Docs — Kryon', 'Kryon ownership, public surface, local commands and integration boundaries.'),
|
|
('renderers.html', 'Platforms — Kryon', 'Kryon runs through Ziran desktop and Canvas hosts; support varies by backend and target.'),
|
|
]
|
|
|
|
def write(path, text):
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
if not path.exists() or path.read_text() != text:
|
|
path.write_text(text)
|
|
|
|
|
|
def package_runtime(page):
|
|
"""Stream compiler payloads into shared files without changing widget code."""
|
|
import base64
|
|
from html.parser import HTMLParser
|
|
runtime = SITE / 'runtime'
|
|
runtime.mkdir(parents=True, exist_ok=True)
|
|
assets = []
|
|
|
|
class Package(HTMLParser):
|
|
def __init__(self):
|
|
super().__init__(convert_charrefs=False)
|
|
self.output, self.mount, self.pending = None, None, ''
|
|
def handle_starttag(self, tag, attrs):
|
|
attrs = dict(attrs)
|
|
if tag != 'script': return
|
|
identity = attrs.get('id', '')
|
|
if identity.startswith('kryon-module-'):
|
|
self.output = (runtime / (identity.removeprefix('kryon-module-') + '.js')).open('w')
|
|
elif attrs.get('class') == 'kryon-file':
|
|
self.mount = attrs['data-mount']
|
|
name = 'asset-' + str(len(assets)) + '.bin'
|
|
assets.append([self.mount, 'runtime/' + name])
|
|
self.output = (runtime / name).open('wb')
|
|
def handle_data(self, data):
|
|
if self.output is None: return
|
|
if self.mount is None: self.output.write(data); return
|
|
self.pending += ''.join(data.split())
|
|
end = len(self.pending) // 4 * 4
|
|
if end:
|
|
self.output.write(base64.b64decode(self.pending[:end], validate=True))
|
|
self.pending = self.pending[end:]
|
|
def handle_endtag(self, tag):
|
|
if tag == 'script' and self.output is not None:
|
|
if self.pending: raise ValueError('Truncated compiler asset')
|
|
self.output.close(); self.output = None; self.mount = None
|
|
parser = Package()
|
|
with page.open() as stream:
|
|
for chunk in iter(lambda: stream.read(65536), ''): parser.feed(chunk)
|
|
parser.close()
|
|
assert (runtime / 'simd128.js').stat().st_size > 1000
|
|
assert (runtime / 'baseline.js').stat().st_size > 1000
|
|
return assets
|
|
|
|
def main():
|
|
from gallery import PROJECT, build_directory, prepare
|
|
modules, entries = prepare()
|
|
mascot_source = ROOT / 'assets/brand/mascot-dragon.png'
|
|
mascot_asset = PROJECT / 'assets/brand/mascot-dragon.png'
|
|
mascot_asset.parent.mkdir(parents=True, exist_ok=True)
|
|
if not mascot_asset.exists() or mascot_asset.read_bytes() != mascot_source.read_bytes():
|
|
shutil.copyfile(mascot_source, mascot_asset)
|
|
shutil.copyfile(mascot_source, SITE / 'assets/brand/mascot-dragon.png')
|
|
subprocess.run(['python3', str(SITE / 'demo/build_preview.py')], cwd=ROOT, check=True)
|
|
environment = os.environ.copy()
|
|
environment['PROJECT_EXTRA_SOURCES'] = 'build/catalog/catalog.zi'
|
|
subprocess.run(['ziran', 'tool', 'kryon', 'build', '--profile', 'web'],
|
|
cwd=PROJECT, env=environment, check=True)
|
|
page = build_directory() / 'gallery-web.html'
|
|
assets = package_runtime(page)
|
|
write(SITE / 'runtime/assets.json', __import__('json').dumps(assets) + '\n')
|
|
write(SITE / 'current.css', 'html,body{margin:0;width:100%;height:100%;overscroll-behavior:none}#canvas-frame{width:100%;height:100%}canvas{display:block;width:100%;height:100%;outline:none}#kryon-ime{position:fixed;left:0;top:0;width:1px;height:1px;border:0;padding:0;opacity:0;pointer-events:none}#result:empty{display:none}#result:not(:empty){position:fixed;inset:auto 20px 20px;margin:0;padding:16px;border:1px solid #d4d4d4;border-radius:10px;background:#fff;color:#171717;font:14px/1.5 system-ui;white-space:pre-wrap}\n')
|
|
with (SITE / 'runtime/simd128.js').open('rb') as stream:
|
|
revision = hashlib.file_digest(stream, 'sha256').hexdigest()[:16]
|
|
for name, title, description in MAINTAINED:
|
|
write(SITE / name, '<!doctype html>\n<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>' + html.escape(title) + '</title><meta name="description" content="' + html.escape(description, quote=True) + '"><link rel="icon" type="image/png" sizes="32x32" href="/favicon-32.png"><link rel="apple-touch-icon" href="/apple-touch-icon.png"><link rel="stylesheet" href="current.css?v=' + revision + '"></head><body><input id="kryon-ime" type="text" tabindex="-1" aria-hidden="true" autocomplete="off" autocapitalize="off" spellcheck="false"><div id="canvas-frame"><canvas id="canvas" tabindex="0" aria-label="Kryon interactive website"></canvas></div><pre id="result" role="status"></pre><noscript>This website runs Ziran through WebAssembly. Enable JavaScript to use the live examples.</noscript><script src="current.js?v=' + revision + '"></script></body></html>\n')
|
|
subprocess.run(['python3', str(ROOT / 'scripts/site-metadata.py')], check=True)
|
|
print(f'Packaged live website: 7 routes, {len(entries)} authored TSX widget examples from {page}')
|
|
|
|
|
|
if __name__ == '__main__': main()
|