Add v0.2 surface syntax, writeback/subtask stdlib, and view input deferral.

Use + for parameters, ordered-list branches, backtick identifiers, and quoted strings with escapes. Add writeback and subtask host libs, optional parameter defaults, narrative-bold comment fix, and defer view execution until preset stdin is submitted.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
cflmyandCursor committed 2026-08-06 17:52:33 +08:00
1 parent 92d88fe761
commit 63e5be80ab
116 files changed
+2226 -559

No files matched your search

+30 -10
View File
@@ -250,6 +250,7 @@ h1.page {
vertical-align: middle;
}
.status-pill.ok { background: var(--surface); color: var(--muted); }
.status-pill.pending { background: var(--surface); color: var(--ink); border-color: var(--ink); }
.status-pill.fail { background: var(--ink); color: #fff; border-color: var(--ink); }
section.block { margin: 2rem 0; }
section.block > h2 {
@@ -469,6 +470,17 @@ section.block > h2 {
padding: 0;
overflow: hidden;
}
.output-card {
border-left: 3px solid #2563eb;
background: #f8fafc;
}
.output-card .output-body {
margin: 0.35rem 0 0;
padding: 0.5rem 0.65rem;
font: 0.85rem/1.45 var(--mono);
white-space: pre-wrap;
color: #0f172a;
}
.code-head {
display: flex;
flex-wrap: wrap;
@@ -968,18 +980,26 @@ pub fn page_index(files: &[PathBuf], only: Option<&Path>, links: &LinkMode) -> S
pub fn page_file(files: &[PathBuf], rel: &str, vm: &FileViewModel, links: &LinkMode) -> String {
let nav = nav_html(files, Some(rel), links);
let status = if vm.ok { "ok" } else { "fail" };
let status_label = if vm.ok { "ok" } else { "fail" };
let out_text = if vm.ok {
if vm.stdout.is_empty() {
"(no stdout)".into()
} else {
escape(&vm.stdout)
}
let (status, status_label, out_text) = if vm.awaiting_input {
(
"pending",
"input",
"Fill preset input below, then click <strong>Run with input</strong>.".to_string(),
)
} else if vm.ok {
(
"ok",
"ok",
if vm.stdout.is_empty() {
"(no stdout)".into()
} else {
escape(&vm.stdout)
},
)
} else if vm.stderr.is_empty() {
"(skipped)".into()
("fail", "fail", "(skipped)".into())
} else {
escape(&vm.stderr)
("fail", "fail", escape(&vm.stderr))
};
let stdin_panel = if vm.input_prompts.is_empty() {
String::new()
+49 -14
View File
@@ -17,7 +17,7 @@ use std::process::Command;
use anyhow::{bail, Context, Result};
use tiny_http::{Header, Method, Request, Response, Server, StatusCode};
use crate::input_feed::{effective_stdin, split_stdin_text};
use crate::input_feed::{effective_stdin, extract_frontmatter_stdin, split_stdin_text};
use crate::view::debug_page::{build_debug_model, page_debug};
use crate::view::html::{escape, page_file, page_index, LinkMode};
use crate::view::render::{collect_input_prompts, render_module_structure, FileViewModel};
@@ -309,25 +309,30 @@ pub(crate) fn build_file_view(
),
};
let effective = effective_stdin(&source, stdin_lines);
let awaiting_input = awaiting_preset_input(&input_prompts, stdin_lines, &source);
// Same host caps as `marqdo run`. Soft exit + sleep clamp only.
let mut opts = RunOptions::default();
opts.stdin_lines = effective.clone();
opts.sleep_limit_ms = if live { Some(30_000) } else { Some(0) };
// Optional: sandbox to the whole view tree so sibling folders are reachable.
opts.fs_root = Some(root.root.clone());
let (stdout, stderr, ok, plots) = match run_file_capture(abs, &opts) {
Ok(cap) => (
cap.stdout,
String::new(),
true,
cap.plots.into_iter().map(|p| p.svg).collect(),
),
Err(e) => (
String::new(),
tidy_user_error(&format!("{e:#}"), abs, rel),
false,
Vec::new(),
),
let (stdout, stderr, ok, plots) = if awaiting_input {
(String::new(), String::new(), true, Vec::new())
} else {
match run_file_capture(abs, &opts) {
Ok(cap) => (
cap.stdout,
String::new(),
true,
cap.plots.into_iter().map(|p| p.svg).collect(),
),
Err(e) => (
String::new(),
tidy_user_error(&format!("{e:#}"), abs, rel),
false,
Vec::new(),
),
}
};
Ok(FileViewModel {
rel_path: rel.to_string(),
@@ -339,10 +344,22 @@ pub(crate) fn build_file_view(
ok,
preset_stdin: effective.join("\n"),
input_prompts,
awaiting_input,
plots,
})
}
/// Defer execution until the view preset-input form supplies lines (no query stdin, no FM stdin).
pub(crate) fn awaiting_preset_input(
input_prompts: &[String],
stdin_lines: &[String],
source: &str,
) -> bool {
!input_prompts.is_empty()
&& stdin_lines.is_empty()
&& extract_frontmatter_stdin(source).is_empty()
}
/// Prefer view-relative paths and strip Windows `\\?\` noise in error text.
fn tidy_user_error(msg: &str, abs: &Path, rel: &str) -> String {
use crate::diagnostics::display_path;
@@ -544,3 +561,21 @@ fn open_url(url: &str) -> Result<()> {
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::awaiting_preset_input;
#[test]
fn awaiting_input_when_no_stdin_source() {
let src = include_str!("../../tests/keywords/input.mq.md");
assert!(awaiting_preset_input(&["Name: ".into()], &[], src));
assert!(!awaiting_preset_input(&["Name: ".into()], &["Ada".into()], src));
}
#[test]
fn not_awaiting_when_frontmatter_stdin() {
let src = "---\nstdin: Ada\n---\n\n# main\n\n*`n` = > input*\n";
assert!(!awaiting_preset_input(&["p".into()], &[], src));
}
}
+32 -10
View File
@@ -3,9 +3,12 @@
//! Comments are not in the AST; they are interleaved from classified source lines.
//! Expressions are shown in surface syntax, never Rust `Debug`.
use std::collections::HashMap;
use crate::ast::{
Arg, BinaryOp, CallExpr, Expr, Function, InterpPart, Literal, Module, Stmt, UnaryOp,
};
use crate::host::writeback;
use crate::lex::{classify_source, ClassifiedLine, LineKind};
use crate::view::html::escape;
use pulldown_cmark::{html, Options, Parser};
@@ -32,6 +35,8 @@ pub struct FileViewModel {
/// One entry per `input` / `输入` call (prompt text; may be empty).
/// Empty vec → page does not use input; hide the preset form.
pub input_prompts: Vec<String>,
/// Live view: program uses `input` but no stdin yet — execution deferred until form submit.
pub awaiting_input: bool,
/// SVG plots from math lib (embedded in Execution).
pub plots: Vec<String>,
}
@@ -163,6 +168,7 @@ pub fn render_module_structure_mode(
mode: StructureMode,
) -> String {
let lines = classify_source(source);
let writebacks = writeback::writeback_map(source);
let mut out = String::new();
if !module.imports.is_empty() {
@@ -193,7 +199,7 @@ pub fn render_module_structure_mode(
if i > 0 {
out.push_str(&emit_comments(&lines, cursor, fun.span.line));
}
let (html, next) = render_fun(fun, &lines, 0, "", mode);
let (html, next) = render_fun(fun, &lines, 0, "", mode, &writebacks);
out.push_str(&html);
cursor = next;
}
@@ -294,8 +300,7 @@ fn emit_comments(lines: &[ClassifiedLine], from_line: u32, before_line: u32) ->
paragraph.push(text.to_string());
}
}
LineKind::Blank => flush(&mut paragraph, &mut s),
LineKind::Code => flush(&mut paragraph, &mut s),
LineKind::Blank | LineKind::Writeback | LineKind::Code => flush(&mut paragraph, &mut s),
}
}
flush(&mut paragraph, &mut s);
@@ -320,6 +325,7 @@ fn render_fun(
depth: usize,
parent_path: &str,
mode: StructureMode,
writebacks: &HashMap<u32, String>,
) -> (String, u32) {
let fn_path = fn_path(parent_path, &fun.name);
let mut s = String::new();
@@ -338,7 +344,7 @@ fn render_fun(
if !fun.params.is_empty() {
s.push_str("<div class=\"params\">");
for p in &fun.params {
s.push_str(&format!("<span class=\"chip\">{}</span>", escape(p)));
s.push_str(&format!("<span class=\"chip\">{}</span>", escape(&p.name)));
}
s.push_str("</div>");
}
@@ -375,14 +381,14 @@ fn render_fun(
let stmt = &fun.body[bi];
let start = stmt_start(stmt);
s.push_str(&emit_comments(lines, cursor, start));
let (html, end) = render_stmt(stmt, lines, mode);
let (html, end) = render_stmt(stmt, lines, mode, writebacks);
s.push_str(&html);
cursor = end;
bi += 1;
} else {
let child = &fun.children[ci];
s.push_str(&emit_comments(lines, cursor, child.span.line));
let (html, end) = render_fun(child, lines, depth + 1, &fn_path, mode);
let (html, end) = render_fun(child, lines, depth + 1, &fn_path, mode, writebacks);
s.push_str(&format!("<div class=\"nested\">{html}</div>"));
cursor = end;
ci += 1;
@@ -427,7 +433,22 @@ fn stmt_end_line(stmt: &Stmt) -> u32 {
}
}
fn render_stmt(stmt: &Stmt, lines: &[ClassifiedLine], mode: StructureMode) -> (String, u32) {
fn output_card(line: u32, writebacks: &HashMap<u32, String>) -> String {
match writebacks.get(&line) {
Some(body) => format!(
r#"<div class="card output-card"><span class="badge">output</span><pre class="output-body">{}</pre></div>"#,
escape(body)
),
None => String::new(),
}
}
fn render_stmt(
stmt: &Stmt,
lines: &[ClassifiedLine],
mode: StructureMode,
writebacks: &HashMap<u32, String>,
) -> (String, u32) {
let end = stmt_end_line(stmt);
let line = stmt_start(stmt);
let inner = match stmt {
@@ -509,7 +530,7 @@ fn render_stmt(stmt: &Stmt, lines: &[ClassifiedLine], mode: StructureMode) -> (S
for st in &arm.body {
let start = stmt_start(st);
body.push_str(&emit_comments(lines, arm_cursor, start));
let (h, e) = render_stmt(st, lines, mode);
let (h, e) = render_stmt(st, lines, mode, writebacks);
body.push_str(&h);
arm_cursor = e;
}
@@ -529,7 +550,7 @@ fn render_stmt(stmt: &Stmt, lines: &[ClassifiedLine], mode: StructureMode) -> (S
for st in body {
let start = stmt_start(st);
body_html.push_str(&emit_comments(lines, cursor, start));
let (h, e) = render_stmt(st, lines, mode);
let (h, e) = render_stmt(st, lines, mode, writebacks);
body_html.push_str(&h);
cursor = e;
}
@@ -551,7 +572,7 @@ fn render_stmt(stmt: &Stmt, lines: &[ClassifiedLine], mode: StructureMode) -> (S
for st in body {
let start = stmt_start(st);
body_html.push_str(&emit_comments(lines, cursor, start));
let (h, e) = render_stmt(st, lines, mode);
let (h, e) = render_stmt(st, lines, mode, writebacks);
body_html.push_str(&h);
cursor = e;
}
@@ -559,6 +580,7 @@ fn render_stmt(stmt: &Stmt, lines: &[ClassifiedLine], mode: StructureMode) -> (S
body_html
}
};
let inner = format!("{}{}", inner, output_card(line, writebacks));
(stmt_shell(line, &inner, mode), end)
}