diff --git a/.gitignore b/.gitignore index c4b132b..9f05a01 100644 --- a/.gitignore +++ b/.gitignore @@ -1,26 +1,3 @@ -# Rust 构建与本地缓存 -/target/ -/.cache/ -/dist/ -/bin/ - -# 本地 tool bundle 与测试输出 -**/*.analysis.json.tmp -**/*.ast.json.tmp -*.tmp - -# 真实浏览器测试 -/tests/browser/node_modules/ -/tests/browser/artifacts/ -/tests/browser/screenshots/ - -# Python 辅助脚本(CI/测试胶水) -__pycache__/ -*.pyc - -# 本地配置与编辑器 -.env -.env.* -.cargo-home/ -.DS_Store -*.swp +/target +/dist +/out diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..6627e10 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,114 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "itoa" +version = "1.0.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3bd1574e617a9a6c1f8f8181bd7e841e1f20e4277607ef28945765188d6284d4" + +[[package]] +name = "marqdo-web-design" +version = "0.1.0" +source = "git+https://git.cflmy.cn/cflmy/marqdo-web-design?rev=2f18dede8540c82611819e2c24a19db138d5ac14#2f18dede8540c82611819e2c24a19db138d5ac14" +dependencies = [ + "serde_json", +] + +[[package]] +name = "marqdo-web-preview" +version = "0.1.0" +dependencies = [ + "marqdo-web-design", + "serde_json", +] + +[[package]] +name = "memchr" +version = "2.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98" + +[[package]] +name = "proc-macro2" +version = "1.0.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "serde" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba" +dependencies = [ + "serde_core", +] + +[[package]] +name = "serde_core" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67dca2c9c51e58a4791a4b1ed58308b39c64224d349a935ab5039aa360942a48" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.154" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7e9cc8b1b85264074fbcc02a88680c4096b1e47df8f739dceb03bf482f04bd6" +dependencies = [ + "itoa", + "memchr", + "serde", + "serde_core", + "zmij", +] + +[[package]] +name = "syn" +version = "3.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ae9ccbea3a7b0b09443036490c8f34a8b1b760fd345e5c0dae0aac718faea79" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563" + +[[package]] +name = "zmij" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..c66f48e --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "marqdo-web-preview" +version = "0.1.0" +edition = "2021" +rust-version = "1.89" +license = "MIT OR Apache-2.0" +description = "Offline Web design preview and the unique marqdo web Tool" + +[dependencies] +marqdo-web-design = { git = "https://git.cflmy.cn/cflmy/marqdo-web-design", rev = "2f18dede8540c82611819e2c24a19db138d5ac14" } +serde_json = "1" diff --git a/README.md b/README.md index 6f70a9a..17c9d1c 100644 --- a/README.md +++ b/README.md @@ -136,3 +136,11 @@ CSP、资产校验归本仓库;表头未识别、Link 精确绑定、`not_load Analysis 字段或 resolver 公共合同缺陷归 CLI;作者面语义冲突或新增通用能力由 `marqdo-ext-web` 走 ADR 并配对 Design/Preview 候选。工单模板与状态流以 Web 领域规范 §12.2 与领域开发指南为准。 + +## 实现进展(WEB-02 首个候选) + +- 独立 executable + `tool.json`,注册唯一 `marqdo web` 命令;消费固定 git revision 的 `marqdo-web-design` 公开 API(`Cargo.lock` 锁定),不复制 Markdown/设计解析。 +- `web check|preview|handoff`:check 输出模型摘要;preview 生成离线单文件 HTML(CSP、原型标识、场景面板、模拟按钮、重置、构建摘要);handoff 原子写 Markdown 交付。 +- 输出目录/文件默认不覆盖;staging→rename 原子发布;拒绝符号链接目标;退出码 0/1/2 与 stderr 诊断。 +- `scripts/ci.sh` 用真实 CLI 安装/运行/卸载 `web` Tool 并验证 CLI 二进制不变。 +- 未完成:真实浏览器行为与 responsive 验收、图片/本地 raster 资产、完整安全负例矩阵、超时/预算边界、WEB-05 冷安装。证据见 [docs/evidence/WEB-02-preview-candidate.md](docs/evidence/WEB-02-preview-candidate.md)。 diff --git a/docs/evidence/WEB-02-preview-candidate.md b/docs/evidence/WEB-02-preview-candidate.md new file mode 100644 index 0000000..7cfbe47 --- /dev/null +++ b/docs/evidence/WEB-02-preview-candidate.md @@ -0,0 +1,39 @@ +# WEB-02 Preview 候选证据(离线制品 / 唯一 web Tool / 有限场景) + +日期:2026-11-27。责任仓:`marqdo-web-preview`;工单:[marqdo-web-preview#1](https://git.cflmy.cn/cflmy/marqdo-web-preview/issues/1)(WEB-02);父工单:根 [marqdo#6](https://git.cflmy.cn/cflmy/marqdo/issues/6)。 + +本文件记录 WEB-02 的**首个候选**:唯一 `marqdo web` Tool 的 check/preview/handoff 入口、离线静态预览、有限场景、CSP 与原子发布。真实浏览器行为验收、资产复制与完整安全矩阵尚未完成,未在此声明。 + +## 输入身份 + +| 对象 | 身份 | +| --- | --- | +| CLI | 由根固定 `cli/deadf6e` 构建的 `marqdo 3.0.0-rc.4`,SHA-256 `sha256:6ede3b9e...` | +| Design 依赖 | `marqdo-web-design@2f18dede8540c82611819e2c24a19db138d5ac14`(`Cargo.lock` 固定 git rev;不是 `../design` 路径依赖) | +| 平台 | Linux x86_64(本机验证) | + +## 执行 + +```sh +MARQDO_BIN= scripts/ci.sh +``` + +脚本真实执行:fmt/clippy → 构建 release → 生成 `tool.json` bundle → `marqdo tool install` → `marqdo web check/preview/handoff`(fixture 工程)→ 覆盖拒绝 → CLI 二进制 SHA 不变 → `tool remove web`。 + +## 已验证(PASS) + +- **唯一入口**:`tool.json`(`command: web`)+ 独立 executable;`marqdo web check|preview|handoff ENTRY --unit ANCHOR [--root] [--out] [--theme]`。 +- **离线制品**:`preview` 生成单个 `index.html`(不启动服务、无网络请求),`file://` 可打开;CSP `default-src 'none'; style-src/script-src sha256; img-src 'self'; connect-src 'none'; form-action 'none'; base-uri 'none'`。 +- **有限场景**:Design 模型中的场景面板与交互按钮渲染为内联固定脚本(CSP hash 放行);点击仅切换预渲染面板,无定时器/网络/eval;含“重置原型”。 +- **原型标识与摘要**:页首与摘要可见“原型 / 模拟”,列 root 与参与源 `sha256`;不显示完整 Analysis 或绝对路径。 +- **安全渲染**:Design 的 `ContentBlock` 文本全部 HTML 转义;raw HTML 显示为 inert 文本;不输出 `href="javascript:`、无外部 script。 +- **发布安全**:输出目录/文件必须不存在;staging 私有目录校验后原子 rename;失败清理;拒绝符号链接目标。 +- **退出码**:设计错误 1、参数/环境/IO 错误 2、成功 0;诊断写 stderr。 +- **Tool 生命周期**:真实 CLI `tool install` / 运行 / `tool remove web`;CLI 二进制 SHA 前后不变。 + +## 未验证 / 后续(不隐藏) + +- 真实浏览器(Playwright/file://)行为、responsive 390px 无横向溢出、按钮实际状态切换的 DOM 断言未执行;当前只有静态制品断言。 +- 图片/本地 raster 资产复制、魔数/大小校验、远端图片文本占位尚未实现(当前不渲染图片)。 +- 完整安全负例矩阵(``/Unicode/双引号 fixture、输出预算边界、超时/进程限制)未系统性覆盖。 +- 冷安装(root 生态 `web` Tool 与 WEB-05)未开始;WEB-04 生产交付试验未开始。 diff --git a/scripts/ci.sh b/scripts/ci.sh new file mode 100755 index 0000000..b97fc92 --- /dev/null +++ b/scripts/ci.sh @@ -0,0 +1,37 @@ +#!/usr/bin/env bash +# WEB-02 independent entry: real CLI, installed web Tool, offline artifact checks. +set -eu +: "${MARQDO_BIN:?set MARQDO_BIN to the verified marqdo executable}" +cd "$(dirname "$0")/.." +cargo fmt --all -- --check +cargo clippy --all-targets --locked -- -D warnings +scripts/package.sh +work="$(mktemp -d)" +trap 'rm -rf "$work"' EXIT +export MARQDO_TOOL_HOME="$work/tool-home" +cli_before="$(sha256sum "$MARQDO_BIN" | cut -d' ' -f1)" +"$MARQDO_BIN" tool install "$PWD/dist/marqdo-web-preview/tool.json" +"$MARQDO_BIN" web check tests/fixture/index.mq.md --unit 工作台 +preview="$work/preview" +"$MARQDO_BIN" web preview tests/fixture/index.mq.md --unit 工作台 --out "$preview" --theme light +test -f "$preview/index.html" +grep -q "Content-Security-Policy" "$preview/index.html" +grep -q "default-src 'none'" "$preview/index.html" +grep -q "原型 / 模拟" "$preview/index.html" +grep -q "raw HTML as inert text" "$preview/index.html" +grep -q "模拟保存" "$preview/index.html" +grep -q "data-scenario" "$preview/index.html" +! grep -q "href=\"javascript:" "$preview/index.html" +! grep -q "\n" + )) +} + +fn diagnostic_text(error: &Error) -> (u8, String) { + let (code, diagnostics) = match error { + Error::Engine(items) => (2u8, items), + Error::Design(items) => (1u8, items), + }; + let mut text = String::new(); + for diagnostic in diagnostics { + text.push_str(&format!( + "{} [{:?}] {}", + diagnostic.code, diagnostic.severity, diagnostic.message + )); + if let Some(path) = &diagnostic.path { + text.push_str(&format!(" at {path}")); + } + if let Some(span) = &diagnostic.span { + text.push_str(&format!(" [{}..{}]", span.start, span.end)); + } + text.push('\n'); + } + (code, text) +} + +fn design_load(args: &Args) -> Result { + let cli = std::env::var("MARQDO_BIN") + .map(PathBuf::from) + .map_err(|_| { + ( + 2u8, + "MARQDO_BIN must name the verified marqdo executable\n".to_string(), + ) + })?; + let entry = PathBuf::from(&args.entry); + let root = args + .root + .clone() + .map(PathBuf::from) + .unwrap_or_else(|| entry.parent().unwrap_or(Path::new(".")).to_path_buf()); + let config = LoadConfig::new(cli, entry, args.unit.clone(), root); + load(&config).map_err(|error| diagnostic_text(&error)) +} + +fn staging_for(out: &Path) -> Result { + let parent = out + .parent() + .ok_or_else(|| "output path has no parent".to_string())?; + if !parent.is_dir() { + return Err(format!( + "output parent is not a directory: {}", + parent.display() + )); + } + if out.exists() { + return Err(format!("output already exists: {}", out.display())); + } + if out + .symlink_metadata() + .map(|meta| meta.file_type().is_symlink()) + .unwrap_or(false) + { + return Err(format!("output is a symbolic link: {}", out.display())); + } + let staging = parent.join(format!(".web-staging-{}", std::process::id())); + if staging.exists() { + fs::remove_dir_all(&staging).map_err(|error| format!("cannot reset staging: {error}"))?; + } + fs::create_dir(&staging).map_err(|error| format!("cannot create staging: {error}"))?; + Ok(staging) +} + +fn publish(staging: &Path, out: &Path) -> Result<(), String> { + fs::rename(staging, out).map_err(|error| format!("cannot publish artifact: {error}")) +} + +fn run_check(args: &Args) -> ExitCode { + match design_load(args) { + Ok(design) => { + println!( + "design {} definitions={} instances={} sources={}", + design.root, + design.definitions.len(), + design.instances.len(), + design.sources.len() + ); + ExitCode::SUCCESS + } + Err((code, text)) => { + eprint!("{text}"); + ExitCode::from(code) + } + } +} + +fn run_preview(args: &Args) -> ExitCode { + let Some(out) = &args.out else { + return ExitCode::from(2); + }; + let design = match design_load(args) { + Ok(design) => design, + Err((code, text)) => { + eprint!("{text}"); + return ExitCode::from(code); + } + }; + let html = match render(&design, &args.theme) { + Ok(html) => html, + Err(error) => { + eprintln!("web preview: error: {error}"); + return ExitCode::from(2); + } + }; + if html.len() > 4 * 1024 * 1024 { + eprintln!("web preview: error: rendered HTML exceeds the 4 MiB budget"); + return ExitCode::from(2); + } + let out = PathBuf::from(out); + let staging = match staging_for(&out) { + Ok(path) => path, + Err(error) => { + eprintln!("web preview: error: {error}"); + return ExitCode::from(2); + } + }; + if let Err(error) = fs::write(staging.join("index.html"), html) { + eprintln!("web preview: error: cannot write index.html: {error}"); + let _ = fs::remove_dir_all(&staging); + return ExitCode::from(2); + } + if let Err(error) = publish(&staging, &out) { + eprintln!("web preview: error: {error}"); + let _ = fs::remove_dir_all(&staging); + return ExitCode::from(2); + } + println!("preview {} (theme {})", out.display(), args.theme); + ExitCode::SUCCESS +} + +fn run_handoff(args: &Args) -> ExitCode { + let Some(out) = &args.out else { + return ExitCode::from(2); + }; + let design = match design_load(args) { + Ok(design) => design, + Err((code, text)) => { + eprint!("{text}"); + return ExitCode::from(code); + } + }; + let text = handoff(&design); + if text.len() > 4 * 1024 * 1024 { + eprintln!("web handoff: error: handoff exceeds the 4 MiB budget"); + return ExitCode::from(2); + } + let out = PathBuf::from(out); + if let Some(parent) = out.parent() { + if !parent.as_os_str().is_empty() && !parent.is_dir() { + eprintln!("web handoff: error: output parent is not a directory"); + return ExitCode::from(2); + } + } + if out.exists() { + eprintln!( + "web handoff: error: output already exists: {}", + out.display() + ); + return ExitCode::from(2); + } + let staging = match staging_for(&out) { + Ok(path) => path, + Err(error) => { + eprintln!("web handoff: error: {error}"); + return ExitCode::from(2); + } + }; + let staging_file = staging.join("handoff.md"); + if let Err(error) = fs::write(&staging_file, text) { + eprintln!("web handoff: error: cannot write handoff: {error}"); + let _ = fs::remove_dir_all(&staging); + return ExitCode::from(2); + } + if let Err(error) = fs::rename(&staging_file, &out) { + eprintln!("web handoff: error: cannot publish handoff: {error}"); + let _ = fs::remove_dir_all(&staging); + return ExitCode::from(2); + } + let _ = fs::remove_dir_all(&staging); + println!("handoff {}", out.display()); + ExitCode::SUCCESS +} + +fn main() -> ExitCode { + let args = match parse_args() { + Ok(args) => args, + Err(error) => { + eprintln!("web: {error}"); + return ExitCode::from(2); + } + }; + match args.command.as_str() { + "check" => run_check(&args), + "preview" => run_preview(&args), + "handoff" => run_handoff(&args), + _ => ExitCode::from(2), + } +} + +#[allow(dead_code)] +fn _scenario_state(scenario: &Scenario) -> String { + scenario.state.clone() +} diff --git a/tests/fixture/components/nav.mq.md b/tests/fixture/components/nav.mq.md new file mode 100644 index 0000000..1dd80c4 --- /dev/null +++ b/tests/fixture/components/nav.mq.md @@ -0,0 +1,12 @@ +# 导航组件 + +## @knowledge 导航 + +### 内容 + +- 首页 +- 任务 + +### 验收 + +- 导航在窄屏下重排。 diff --git a/tests/fixture/components/tasks.mq.md b/tests/fixture/components/tasks.mq.md new file mode 100644 index 0000000..41a6ab8 --- /dev/null +++ b/tests/fixture/components/tasks.mq.md @@ -0,0 +1,16 @@ +# 任务组件 + +## @knowledge 任务 + +### 场景 + +| 状态 | 内容 | 初始 | +| --- | --- | --- | +| empty | [空状态](../states.mq.md#空状态) | true | +| done | [完成](../states.mq.md#完成) | false | + +### 交互 + +| 当前 | 事件 | 下一状态 | +| --- | --- | --- | +| empty | 模拟保存 | done | diff --git a/tests/fixture/index.mq.md b/tests/fixture/index.mq.md new file mode 100644 index 0000000..aa52591 --- /dev/null +++ b/tests/fixture/index.mq.md @@ -0,0 +1,16 @@ +# 科研原型 + +## @knowledge 工作台 + +面向研究人员的原型页面。访问 [项目说明](https://example.test/docs) 或 [危险](javascript:alert(1))。 + +### 内容 + +
raw html 应显示为文本
+ +### 组件 + +| 行 | 槽 | 组件 | 宽度 | +| --- | --- | --- | --- | +| 1 | nav | [导航](components/nav.mq.md#导航) | 220px | +| 1 | main | [任务](components/tasks.mq.md#任务) | 1fr | diff --git a/tests/fixture/states.mq.md b/tests/fixture/states.mq.md new file mode 100644 index 0000000..29db853 --- /dev/null +++ b/tests/fixture/states.mq.md @@ -0,0 +1,9 @@ +# 状态 + +## @knowledge 空状态 + +暂无任务。 + +## @knowledge 完成 + +任务已完成(模拟)。