feat(markup): code-as-docs narration surface (G1–G4) and drop --- body end

Treat --- as Markdown HR everywhere, end side-effect helpers with empty returns,
and accept soft emphasis, float literals, and dynamic footnote keys so LLM-friendly
.mq.md stays runnable. Ship the weekend coffee example plus gold fixtures.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
cflmyandCursor committed 2026-09-14 10:16:48 +08:00
1 parent b5c07385e9
commit 5e74ff39f2
28 files changed
+799 -99

No files matched your search

+106 -13
View File
@@ -1,4 +1,7 @@
//! Inline prose scan for Markup v0.3 (叙述中的 `` `名` `` / `**代码**` / `*返回*`).
//!
//! Soft Markdown emphasis that does not look like code/return is skipped (G2 —
//! [code-as-docs-gaps.md](../../../doc/design/code-as-docs-gaps.md)).
use crate::lex::{matching_bold_close_inline, matching_italic_close_inline};
@@ -36,13 +39,12 @@ pub fn scan_prose_line(line: &str) -> Vec<ProseBit> {
if c == b'*' && i + 1 < bytes.len() && bytes[i + 1] == b'*' {
let rest = &line[i..];
if let Some(close_rel) = matching_bold_close_inline(rest) {
// close_rel is index within `rest` of first `*` of closing `**`
let inner = rest[2..close_rel].to_string();
out.push(ProseBit::BoldCode {
inner,
at: i,
});
i += close_rel + 2;
let next = i + close_rel + 2;
if looks_like_bold_code(&inner) {
out.push(ProseBit::BoldCode { inner, at: i });
}
i = next;
continue;
}
i += 1;
@@ -56,11 +58,11 @@ pub fn scan_prose_line(line: &str) -> Vec<ProseBit> {
}
if let Some(close_rel) = matching_italic_close_inline(rest) {
let inner = rest[1..close_rel].to_string();
out.push(ProseBit::ItalicReturn {
inner,
at: i,
});
i += close_rel + 1;
let next = i + close_rel + 1;
if looks_like_italic_return(line, i, next, &inner) {
out.push(ProseBit::ItalicReturn { inner, at: i });
}
i = next;
continue;
}
}
@@ -69,6 +71,79 @@ pub fn scan_prose_line(line: &str) -> Vec<ProseBit> {
out
}
/// Whether `**inner**` should execute (assign / call / expr), not soft emphasis.
pub fn looks_like_bold_code(inner: &str) -> bool {
let t = inner.trim();
if t.is_empty() {
return false;
}
if t.contains('=') || t.starts_with('>') {
return true;
}
if t.chars().any(|c| matches!(c, '+' | '-' | '*' | '/' | '(' | ')' | '[' | ']')) {
return true;
}
let parts: Vec<&str> = t.split_whitespace().collect();
if parts.len() >= 2 {
return parts[1..].iter().any(|p| {
p.contains('=')
|| p.starts_with('"')
|| p.starts_with('\'')
|| p.starts_with('`')
|| p.chars().all(|c| c.is_ascii_digit())
|| (p.contains('.')
&& p.chars()
.filter(|c| *c != '.')
.all(|c| c.is_ascii_digit()))
});
}
false
}
/// Whether `*inner*` is a return (vs mid-prose English/CJK emphasis).
fn looks_like_italic_return(line: &str, at: usize, after: usize, inner: &str) -> bool {
let t = inner.trim();
if t.is_empty()
|| matches!(
t,
"None" | "无" | "空" | "True" | "False" | "真" | "假" | "true" | "false"
)
{
return true;
}
if t.starts_with('>')
|| t.contains('=')
|| t.chars()
.any(|c| matches!(c, '+' | '-' | '*' | '/' | '(' | ')' | '[' | ']'))
{
return true;
}
// Whole-line return: only ws outside the italic span.
let before = line[..at].trim();
let after_s = line.get(after..).unwrap_or("").trim();
if before.is_empty() && after_s.is_empty() {
return true;
}
// Narration "返回*n*" / "return *n*"
if before.ends_with("返回") || before.ends_with("return") || before.ends_with("Return") {
return true;
}
// Mid-prose simple word → soft emphasis (G2).
if is_simple_emphasis_word(t) {
return false;
}
// Multi-token or dotted id mid-line: keep as return (rare).
true
}
fn is_simple_emphasis_word(t: &str) -> bool {
if t.chars().any(|c| c.is_whitespace()) {
return false;
}
t.chars()
.all(|c| c.is_alphanumeric() || c == '_' || !c.is_ascii())
}
fn scan_backtick_decl(line: &str, start: usize) -> Option<(ProseBit, usize)> {
let rest = &line[start + 1..];
let end = rest.find('`')?;
@@ -78,9 +153,7 @@ fn scan_backtick_decl(line: &str, start: usize) -> Option<(ProseBit, usize)> {
}
let after_tick = start + 1 + end + 1;
let after = line.get(after_tick..)?;
// `` `名`=默认 `` — optional default glued after closing tick
if let Some(stripped) = after.strip_prefix('=') {
// default runs until whitespace or CJK punctuation-ish break for prose
let def_end = stripped
.find(|c: char| {
c.is_whitespace()
@@ -147,4 +220,24 @@ mod tests {
_ => panic!("{:?}", bits[0]),
}
}
#[test]
fn soft_bold_emphasis_skipped() {
let bits = scan_prose_line("本文件既是**说明**也是**程序**。");
assert!(bits.is_empty(), "{bits:?}");
assert!(!looks_like_bold_code("说明"));
assert!(looks_like_bold_code("n=n+1"));
assert!(looks_like_bold_code("加一 37"));
}
#[test]
fn soft_italic_emphasis_skipped() {
let bits = scan_prose_line("When soft=True, *only* FIND continues.");
assert!(bits.is_empty(), "{bits:?}");
let bits = scan_prose_line("*总额*");
assert!(
matches!(&bits[..], [ProseBit::ItalicReturn { inner, .. }] if inner == "总额"),
"{bits:?}"
);
}
}