"""Make a fully offline industrial-theme homepage preview from the real static builder.""" import os,sys,json,html,re,tempfile from pathlib import Path ROOT=Path(__file__).resolve().parents[1] sys.path[:0]=[str(ROOT),str(ROOT/'tests')] from tests.legacy import LegacyTestClient from app import db,theme_store from app.main import app def generate(): target=ROOT/'examples'; target.mkdir(exist_ok=True) with tempfile.TemporaryDirectory() as temporary: db.DATA=Path(temporary);db.DB_FILE=db.DATA/'data.sqlite3';theme_store.THEME_ROOT=db.DATA/'theme_packages' os.environ['SITEFORGE_ALLOW_LEGACY_DIRECT_DESIGN_WRITE']='1' os.environ['SITEFORGE_ADMIN_PASSWORD']='demo-test-admin-secret-2026' os.environ['SITEFORGE_SESSION_SECRET']='demo-secret-not-for-use-outside-test-2026' with LegacyTestClient(app) as c: assert c.post('/api/auth/login',json={'username':'admin','password':os.environ['SITEFORGE_ADMIN_PASSWORD']}).status_code==200 c.headers.update({'X-SiteForge-CSRF':c.get('/api/auth/me').json()['csrf']}) r=c.post('/api/sites',json={'name':'HESU PIPE · 排版验证样站','theme':'industrial','domain':'https://example.test','mode':'migration','tagline':'工业管道与工程材料 · 完整产品方案'}) assert r.status_code==201,r.text; sid=r.json()['id'] home=next(e for e in c.get(f'/api/sites/{sid}/entries').json() if e['route']=='/') assert c.put(f'/api/factory/sites/{sid}/theme',json={'theme':'industrial','version':'1.3.1'}).status_code==200 products=[('HDPE 给水管','适用于市政给水及工业供水管网。'),('双壁波纹排水管','适用于市政雨污水管网和地下排水工程。'),('PVC-U 排水管','耐腐蚀、规格丰富的塑料管材系统。'),('PPR 冷热水管','建筑冷热水输送常用管道系列。'),('钢丝网骨架复合管','适用于承压输水与工程管线系统。'),('HDPE 管道连接件','用于热熔及电熔连接的各类管件。')] for i,(title,summary) in enumerate(products,1): res=c.post(f'/api/sites/{sid}/entries',json={'type':'product','title':title,'route':f'/products/pipe-{i}/','summary':summary}) assert res.status_code==201,res.text blocks=[{'id':'features-hp-v8331','type':'features','title':'核心优势','items':[ {'title':'管道产品供应','text':'覆盖多种工程管材和配件,便于项目集中采购。'}, {'title':'规格与质量管理','text':'根据实际应用提供产品选型和规格资料。'}, {'title':'工程应用支持','text':'面向供水、排水及工业管网项目的应用场景。'}, {'title':'全球交付服务','text':'从订单沟通到物流交付,提供清晰的合作流程。'}, ]}, {'id':'collection-hp-v8331','type':'collection','title':'动态内容列表 · 精选产品', 'subtitle':'PRODUCT COLLECTION','source':'product','limit':6,'columns':3, 'href':'/products/','button':'查看全部产品'}] saved=c.put(f'/api/sites/{sid}/pages/{home["id"]}/blueprint',json={'blocks':blocks,'page_template':'home'}) assert saved.status_code==200,saved.text built=c.post(f'/api/sites/{sid}/builds',json={}) assert built.status_code==200,built.text bid=built.json()['version']; dist=db.DATA/'builds'/sid/bid/'dist' assert (dist/'index.html').exists(),dist source=(dist/'index.html').read_text() # Inline exact packaged CSS/JS, so the demo is readable without a server or external requests. def stylesheet(m): path=m.group(1) local=dist/path return '' if local.is_file() else '' source=re.sub(r'',stylesheet,source) def script(m): local=dist/m.group(1) if local.is_file(): return '' return '' source=re.sub(r'',script,source) # These paths are demonstrative and intentionally not published. isolated=target/'V8_33_1_HesuPipe_首页布局真实构建预览.html' snippet=json.dumps(source,ensure_ascii=False).replace('<','\\u003c') wrapper='''