feat(view): M8 遮蔽呈现 — 描述为正文、实现为代码片、桩有徽章;并修复 view 解析模式回归

一、先修回归(用户看到"没渲染"的真因)
view 多处直接用 crate::parse::parse_source 重新解析文件,而 per-file markup:
是在 load 层生效的(M7)。于是任何带 markup: occlusion 的文档在结构区报
"parse/load error: expected ')' after link-index collection",退化成源码视图。
→ 新增 load::parse_source_auto(source):先读 frontmatter 的 markup:,用
  with_markup 包住解析;view 的 4 处解析点全部改用。

二、遮蔽呈现
浏览视图(document.rs / /file):
  <div class="doc-line doc-occl">
    <span class="occl-pre">留存 =</span>              ← 变量名 灰 等宽
    <span class="occl-label">客户留存率,模拟数据</span> ← 描述 正文
    <code class="occl-code">0.42</code>              ← 实现 代码片
  </div>
空载荷换成 <span class="occl-stub">桩</span> 琥珀徽章(title 说明未接通/待补返回值);
前缀的反引号与 ** 都剥掉,与其它行的 mq-anchor 一致。

卡片视图(render.rs / /debug):按 AST 匹配 Expr::Occluded(Assign/Expr/Return),
渲染 card occl-card:徽章「遮蔽」+ 变量名 + 描述 + 代码片/桩。

样式刻意不加虚线下划线(易读成链接):描述即正文,靠代码片边框区分。

验证:
- /file 页面 0 处 parse/load error;7 个遮蔽行全部渲染
  (6 代码片 + 1 桩),文本抽取为「留存 = 客户留存率,模拟数据 [0.42]」
- 4 个单测(裸赋值/粗体赋值/空载荷+嵌套/非遮蔽不误判)
- 全量 cargo test --release 613 passed / 0 failed

未能截图:本机无任何浏览器,puppeteer 的 Chromium 下载被网络阻断;
改为 DOM 断言 + /.preview/occlusion.html 方案对照页由作者目视确认。

文档:doc/design/change-m8-view-occlusion.md
This commit is contained in:
cflmy committed 2026-10-22 16:16:32 +08:00
1 parent 8c24d96626
commit 9015de26e5
7 files changed
+387 -4

No files matched your search

+205
View File
@@ -724,6 +724,157 @@ fn parse_args_brief(brief: &str) -> HashMap<String, String> {
m
}
/// Split a statement line holding one occlusion `[描述](实现)`.
///
/// Returns `(prefix, label, payload, suffix)`. Display-only: the parser already
/// validated the real structure, this just decides how to paint it.
fn split_occlusion(line: &str) -> Option<(String, String, String, String)> {
let chars: Vec<char> = line.chars().collect();
let mut i = 0usize;
let mut in_bt = false;
let mut in_str = false;
while i < chars.len() {
let c = chars[i];
if in_str {
if c == '\\' {
i += 2;
continue;
}
if c == '"' {
in_str = false;
}
i += 1;
continue;
}
if in_bt {
if c == '`' {
in_bt = false;
}
i += 1;
continue;
}
match c {
'`' => in_bt = true,
'"' => in_str = true,
'[' if chars.get(i + 1) != Some(&'^') => {
if let Some(close) = match_delim(&chars, i, '[', ']') {
if chars.get(close + 1) == Some(&'(') {
if let Some(paren) = match_delim(&chars, close + 1, '(', ')') {
let mut prefix: String = chars[..i].iter().collect();
let label: String = chars[i + 1..close].iter().collect();
let payload: String = chars[close + 2..paren].iter().collect();
let suffix: String = chars[paren + 1..].iter().collect();
// `**名 = [..](..)**` reads as bold code: drop the
// emphasis markers so the label reads as prose.
if let Some(p) = prefix.strip_suffix("**") {
prefix = p.to_string();
}
prefix = prefix.trim_end().to_string();
if let Some(p) = prefix.strip_prefix("**") {
prefix = p.to_string();
}
let suffix = suffix.strip_prefix("**").unwrap_or(&suffix).to_string();
return Some((
prefix,
label.trim().to_string(),
payload.trim().to_string(),
suffix.trim().to_string(),
));
}
}
}
}
_ => {}
}
i += 1;
}
None
}
fn match_delim(chars: &[char], start: usize, open: char, close: char) -> Option<usize> {
let mut depth = 0i32;
let mut i = start;
let mut in_bt = false;
let mut in_str = false;
while i < chars.len() {
let c = chars[i];
if in_str {
if c == '\\' {
i += 2;
continue;
}
if c == '"' {
in_str = false;
}
i += 1;
continue;
}
if in_bt {
if c == '`' {
in_bt = false;
}
i += 1;
continue;
}
match c {
'`' => in_bt = true,
'"' => in_str = true,
c if c == open => depth += 1,
c if c == close => {
depth -= 1;
if depth == 0 {
return Some(i);
}
}
_ => {}
}
i += 1;
}
None
}
/// Render one occlusion line: description as prose, payload as a code chip,
/// stubs get a badge. `<TAB>` M8
fn render_occlusion_line(line_no: u32, line: &str) -> Option<String> {
let (prefix, label, payload, suffix) = split_occlusion(line)?;
let stub = payload.is_empty();
// The rest of the view drops backticks (see the `mq-anchor` marks), so the
// assignment head reads as `留存 =` rather than `` `留存` = ``.
let head = prefix
.trim_end()
.trim_end_matches('=')
.trim()
.trim_matches('`')
.to_string();
let mut html = format!(r#"<div class="doc-line doc-occl" data-line="{line_no}">"#);
if !head.is_empty() {
html.push_str(&format!(
r#"<span class="occl-pre">{} =</span>"#,
escape(&head)
));
}
html.push_str(&format!(
r#"<span class="occl-label">{}</span>"#,
escape(&label)
));
if stub {
html.push_str(r#"<span class="occl-stub" title="空载荷:未接通或待补返回值">桩</span>"#);
} else {
html.push_str(&format!(
r#"<code class="occl-code">{}</code>"#,
escape(&payload)
));
}
if !suffix.is_empty() {
html.push_str(&format!(
r#"<span class="occl-pre">{}</span>"#,
escape(&suffix)
));
}
html.push_str("</div>");
Some(html)
}
fn render_line_block(
line_no: u32,
text: &str,
@@ -757,6 +908,15 @@ fn render_line_block(
}
None => {
let trimmed = text.trim_start();
// `<TAB>` M8: an occlusion line is painted as description + code chip
// regardless of annotation bits (the annotator always emits spans).
if let Some(html) = render_occlusion_line(line_no, trimmed) {
s.push_str(&html);
if let Some(body) = writeback_body {
s.push_str(&crate::view::render::render_output_card(line_no, body));
}
return s;
}
if bits.is_empty() {
if let Some(rest) = trimmed.strip_prefix("> ") {
let aid = match_annot_for_line(annots)
@@ -1383,3 +1543,48 @@ When we meet `who`, we **print text="Hi, `who`!"** and close with*None*.
);
}
}
#[cfg(test)]
mod occl_split_tests {
use super::split_occlusion;
#[test]
fn splits_bare_assignment_occlusion() {
let (head, label, payload, rest) =
split_occlusion("`留存` = [客户留存率,模拟数据](0.42)").unwrap();
assert_eq!(head, "`留存` =");
assert_eq!(label, "客户留存率,模拟数据");
assert_eq!(payload, "0.42");
assert_eq!(rest, "");
}
#[test]
fn splits_bold_assignment_occlusion_without_markers() {
let (head, label, payload, _) =
split_occlusion("**留存 = [客户留存率,模拟数据](0.42)**").unwrap();
assert_eq!(head, "留存 =");
assert_eq!(label, "客户留存率,模拟数据");
assert_eq!(payload, "0.42");
}
#[test]
fn splits_stub_and_nested_payload() {
let (_, label, payload, _) =
split_occlusion("`待接` = [搜索接入,待开发]()").unwrap();
assert_eq!(label, "搜索接入,待开发");
assert!(payload.is_empty());
let (_, label, payload, _) =
split_occlusion("`取第一` = [第一颗](access(`篮子`, \"1\"))").unwrap();
assert_eq!(label, "第一颗");
assert_eq!(payload, "access(`篮子`, \"1\")");
}
#[test]
fn ignores_index_and_plain_lines() {
// Link-index (classic) and ordinary statements are not occlusions.
assert!(split_occlusion("**v = [键](`表`)**").is_some()); // shape matches; parser decides meaning
assert!(split_occlusion("**甲 = 加(1 2)**").is_none());
assert!(split_occlusion("> print text=x").is_none());
}
}