1"""Shared documentation inputs for Python-only build tools."""2import hashlib,json,re,posixpath3from pathlib import Path4ROOT=Path(__file__).resolve().parents[1]5def examples():6 catalog=json.loads((ROOT/'examples/gallery/catalog.json').read_text());english=json.loads((ROOT/'examples/gallery/catalog.en.json').read_text());out=[]7 for category in catalog['categories']:8 for item in category['items']:9 source=item.get('source',f'examples/gallery/{category["slug"]}/{item["slug"]}.lay')10 out.append({'id':source.removeprefix('examples/').removesuffix('.lay').replace('/','-'),'source':source,'category':'plots' if category['slug']=='plots' else 'layout','group':category['slug'],'title':[item['title'],english[category['slug']]['items'][item['slug']][0]],'description':[item['description'],english[category['slug']]['items'][item['slug']][1]]})11 seen={e['source'] for e in out}12 for p in sorted((ROOT/'examples').rglob('*.lay')):13 source=p.relative_to(ROOT).as_posix()14 if 'output' in p.relative_to(ROOT).parts:continue15 if source in seen or 'canvas(' not in p.read_text():continue16 title=p.stem.replace('-',' ').title();out.append({'id':source.removeprefix('examples/').removesuffix('.lay').replace('/','-'),'source':source,'category':'plots' if '/plot/' in source else 'notebook' if '/notebook/' in source else 'applications','group':'plot' if '/plot/' in source else 'applications','title':[title,title],'description':['完整可复现的源码与输出。','Complete reproducible source and output.']})17 for p in sorted((ROOT/'examples').rglob('*.laydoc')):18 source=p.relative_to(ROOT).as_posix();title=p.stem.replace('-',' ').title()19 out.append({'id':source.removeprefix('examples/').removesuffix('.laydoc').replace('/','-'),'source':source,'category':'applications','group':'applications','title':[title,title],'description':['使用 Lay 语法管理独立页面。','Manage independent pages with the Lay DSL.']})20 return out2122def dependencies(source,seen=None):23 if seen is None:seen=set()24 if source in seen:return sorted(seen)25 seen.add(source);p=ROOT/source26 if p.suffix.lower() not in ('.lay','.laydoc','.lcss','.css','.py','.ipynb','.svg'):return sorted(seen)27 text=p.read_text();text=re.sub(r'url\(\s*([^\'"()\s]+)\s*\)',r'url("\1")',text)28 for value in re.findall(r'''["']([^"'\n]+\.(?:lay|laydoc|lcss|css|png|jpg|jpeg|tif|tiff|bmp|webp|gif|ico|pnm|pbm|pgm|ppm|pam|tga|svg|mp4|m4a|mp3|wav|json|csv|ttf|otf|ttc|otc)(?:#[^"'\n]*)?)["']''',text):29 value=re.sub(r'(\.(?:ttf|otf|ttc|otc))#[^/]*$',r'\1',value);target=posixpath.normpath(posixpath.join(posixpath.dirname(source),value))30 if target.startswith('../') or value.startswith('/'):continue31 if (ROOT/target).is_file():dependencies(target,seen)32 return sorted(seen)33def fingerprint(files):34 h=hashlib.sha256()35 for f in sorted(set(files)):36 h.update(str(f).encode());h.update((ROOT/f).read_bytes())37 return h.hexdigest()38def engine_files():39 # Renderer provenance covers runtime crate sources and build settings. Test40 # fixtures and catalog/benchmark example executables do not ship in the engine.41 # Standalone test42 # fixtures do not change rendered media when their platform paths are corrected.43 return ['.cargo/config.toml','Cargo.lock','Cargo.toml','rust-toolchain.toml']+[p.relative_to(ROOT).as_posix() for p in sorted((ROOT/'crates').rglob('*')) if p.is_file() and p.suffix in ('.rs','.toml','.json') and 'laymesh-wasm' not in p.parts and 'tests' not in p.relative_to(ROOT/'crates').parts and 'examples' not in p.relative_to(ROOT/'crates').parts]