//! Render Marqdo AST as HTML fragments for `view`. //! //! Comments are not in the AST; they are interleaved from classified source lines. //! Expressions are shown in surface syntax, never Rust `Debug`. use crate::ast::{ Arg, BinaryOp, CallExpr, Expr, Function, InterpPart, Literal, Module, Stmt, UnaryOp, }; use crate::lex::{classify_source, ClassifiedLine, LineKind}; use crate::view::html::escape; use pulldown_cmark::{html, Options, Parser}; #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum StructureMode { /// Documentation browse: fun anchors, no breakpoint gutters Browse, /// Debugger: breakpoint gutters on statements Debug, } pub struct FileViewModel { #[allow(dead_code)] pub rel_path: String, pub source: String, pub structure_html: String, pub outline_html: String, pub stdout: String, pub stderr: String, pub ok: bool, /// Text shown in the live-view preset stdin box. pub preset_stdin: String, /// 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, /// SVG plots from math lib (embedded in Execution). pub plots: Vec, } /// Collect prompt strings from every `input` / `输入` call in the module (pre-order). pub fn collect_input_prompts(module: &Module) -> Vec { let mut out = Vec::new(); for fun in &module.functions { collect_input_prompts_fun(fun, &mut out); } out } fn collect_input_prompts_fun(fun: &Function, out: &mut Vec) { for stmt in &fun.body { collect_input_prompts_stmt(stmt, out); } for child in &fun.children { collect_input_prompts_fun(child, out); } } fn collect_input_prompts_stmt(stmt: &Stmt, out: &mut Vec) { match stmt { Stmt::Assign { value, .. } | Stmt::Return { value, .. } => { collect_input_prompts_expr(value, out); } Stmt::Call { call, .. } => { push_input_prompt(call, out); for a in &call.args { match a { Arg::Positional(e) | Arg::Named { value: e, .. } => { collect_input_prompts_expr(e, out); } } } } Stmt::Branch { arms, .. } => { for arm in arms { if let Some(c) = &arm.condition { collect_input_prompts_expr(c, out); } for s in &arm.body { collect_input_prompts_stmt(s, out); } } } Stmt::While { condition, body, .. } => { collect_input_prompts_expr(condition, out); for s in body { collect_input_prompts_stmt(s, out); } } Stmt::ForEach { body, .. } => { for s in body { collect_input_prompts_stmt(s, out); } } } } fn collect_input_prompts_expr(expr: &Expr, out: &mut Vec) { match expr { Expr::Call(call) => { push_input_prompt(call, out); for a in &call.args { match a { Arg::Positional(e) | Arg::Named { value: e, .. } => { collect_input_prompts_expr(e, out); } } } } Expr::Unary { expr, .. } => collect_input_prompts_expr(expr, out), Expr::Binary { left, right, .. } => { collect_input_prompts_expr(left, out); collect_input_prompts_expr(right, out); } Expr::List(items) => { for e in items { collect_input_prompts_expr(e, out); } } Expr::Literal(_) | Expr::Var(_) | Expr::Interp(_) | Expr::Formula(_) | Expr::Code(_) => {} } } fn push_input_prompt(call: &CallExpr, out: &mut Vec) { if crate::aliases::canonical_builtin(&call.callee) != Some("input") { return; } for a in &call.args { if let Arg::Named { name, value } = a { if crate::aliases::canonical_param("input", name) == "prompt" { out.push(prompt_expr_text(value)); return; } } } if let Some(Arg::Positional(value)) = call.args.first() { out.push(prompt_expr_text(value)); return; } out.push(String::new()); } fn prompt_expr_text(expr: &Expr) -> String { match expr { Expr::Literal(Literal::Text(t)) => t.clone(), Expr::Literal(lit) => lit_display(lit), other => expr_display(other), } } pub fn render_module_structure(module: &Module, source: &str) -> String { render_module_structure_mode(module, source, StructureMode::Browse) } pub fn render_module_structure_mode( module: &Module, source: &str, mode: StructureMode, ) -> String { let lines = classify_source(source); let mut out = String::new(); if !module.imports.is_empty() { out.push_str("
import"); for imp in &module.imports { out.push_str(&format!("{}", escape(imp))); } out.push_str("
"); } let body_start = skip_frontmatter_end(&lines).unwrap_or(1); let mut cursor = body_start; if module.functions.is_empty() { out.push_str(&emit_comments(&lines, cursor, u32::MAX)); if out.is_empty() { out.push_str("

(empty module)

"); } return out; } // Leading comments before first function let first = module.functions[0].span.line; out.push_str(&emit_comments(&lines, cursor, first)); cursor = first; for (i, fun) in module.functions.iter().enumerate() { if i > 0 { out.push_str(&emit_comments(&lines, cursor, fun.span.line)); } let (html, next) = render_fun(fun, &lines, 0, "", mode); out.push_str(&html); cursor = next; } // Trailing comments after last function tree out.push_str(&emit_comments(&lines, cursor, u32::MAX)); out } pub fn render_function_outline(module: &Module) -> String { if module.functions.is_empty() { return String::new(); } let mut out = String::from( r#"
    "#, ); for fun in &module.functions { out.push_str(&outline_fun(fun, "")); } out.push_str("
"); out } fn outline_fun(fun: &Function, parent_path: &str) -> String { let fn_path = fn_path(parent_path, &fun.name); let mut s = format!( "
  • \ {} h{}", escape(&fun.name), escape(&fn_path), fun.span.line, escape(&fun.name), fun.level, ); if !fun.children.is_empty() { s.push_str("
      "); for child in &fun.children { s.push_str(&outline_fun(child, &fn_path)); } s.push_str("
    "); } s.push_str("
  • "); s } fn fn_path(parent_path: &str, name: &str) -> String { if parent_path.is_empty() { name.to_string() } else { format!("{parent_path}/{name}") } } /// Line after closing frontmatter `---`, or 1 if none. fn skip_frontmatter_end(lines: &[ClassifiedLine]) -> Option { let first_code = lines.iter().find(|l| l.kind == LineKind::Code)?; if first_code.text.trim() != "---" { return Some(1); } for l in lines.iter().skip_while(|l| l.line_no <= first_code.line_no) { if l.kind == LineKind::Code && l.text.trim() == "---" { return Some(l.line_no + 1); } } Some(1) } fn emit_comments(lines: &[ClassifiedLine], from_line: u32, before_line: u32) -> String { let mut s = String::new(); let mut paragraph: Vec = Vec::new(); let flush = |paragraph: &mut Vec, s: &mut String| { if paragraph.is_empty() { return; } // Soft-wrapped source lines → one Markdown paragraph (spaces between). let text = paragraph.join(" "); paragraph.clear(); s.push_str(&format!( "
    comment
    {}
    ", comment_markdown_to_html(&text) )); }; for l in lines { if l.line_no < from_line { continue; } if l.line_no >= before_line { break; } match l.kind { LineKind::Comment => { let text = l.text.trim(); if !text.is_empty() { paragraph.push(text.to_string()); } } LineKind::Blank => flush(&mut paragraph, &mut s), LineKind::Code => flush(&mut paragraph, &mut s), } } flush(&mut paragraph, &mut s); s } /// Render narrative comments as Markdown (links, emphasis, code, lists, …). fn comment_markdown_to_html(md: &str) -> String { let mut opts = Options::empty(); opts.insert(Options::ENABLE_STRIKETHROUGH); opts.insert(Options::ENABLE_TABLES); opts.insert(Options::ENABLE_TASKLISTS); let parser = Parser::new_ext(md, opts); let mut out = String::new(); html::push_html(&mut out, parser); out } fn render_fun( fun: &Function, lines: &[ClassifiedLine], depth: usize, parent_path: &str, mode: StructureMode, ) -> (String, u32) { let fn_path = fn_path(parent_path, &fun.name); let mut s = String::new(); let nest = if depth > 0 { " nested" } else { "" }; s.push_str(&format!( "
    ", fun.span.line, escape(&fun.name), escape(&fn_path), )); s.push_str(&format!( "
    fn · h{}{}
    ", fun.level, escape(&fun.name) )); if !fun.params.is_empty() { s.push_str("
    "); for p in &fun.params { s.push_str(&format!("{}", escape(p))); } s.push_str("
    "); } let mut cursor = fun.span.line + 1; // Comments / gap before first body stmt or child let first_body = fun.body.first().map(stmt_start); let first_child = fun.children.first().map(|c| c.span.line); let first_content = match (first_body, first_child) { (Some(a), Some(b)) => Some(a.min(b)), (Some(a), None) => Some(a), (None, Some(b)) => Some(b), (None, None) => None, }; if let Some(fc) = first_content { s.push_str(&emit_comments(lines, cursor, fc)); cursor = fc; } // Interleave body stmts and nested functions by source line let mut bi = 0usize; let mut ci = 0usize; while bi < fun.body.len() || ci < fun.children.len() { let stmt_line = fun.body.get(bi).map(stmt_start); let child_line = fun.children.get(ci).map(|c| c.span.line); let take_stmt = match (stmt_line, child_line) { (Some(sl), Some(cl)) => sl <= cl, (Some(_), None) => true, (None, Some(_)) => false, (None, None) => break, }; if take_stmt { 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); 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); s.push_str(&format!("
    {html}
    ")); cursor = end; ci += 1; } } s.push_str("
    "); (s, cursor) } fn stmt_start(stmt: &Stmt) -> u32 { match stmt { Stmt::Assign { span, .. } | Stmt::Return { span, .. } | Stmt::Call { span, .. } | Stmt::Branch { span, .. } | Stmt::While { span, .. } | Stmt::ForEach { span, .. } => span.line, } } fn stmt_end_line(stmt: &Stmt) -> u32 { match stmt { Stmt::Assign { end_line, .. } => end_line + 1, Stmt::Return { span, .. } | Stmt::Call { span, .. } => span.line + 1, Stmt::Branch { arms, span, .. } => { let mut end = span.line + 1; for arm in arms { for st in &arm.body { end = end.max(stmt_end_line(st)); } } end } Stmt::While { body, span, .. } | Stmt::ForEach { body, span, .. } => { let mut end = span.line + 1; for st in body { end = end.max(stmt_end_line(st)); } end } } } fn render_stmt(stmt: &Stmt, lines: &[ClassifiedLine], mode: StructureMode) -> (String, u32) { let end = stmt_end_line(stmt); let line = stmt_start(stmt); let inner = match stmt { Stmt::Assign { name, value: Expr::Formula(e), .. } => { let tex = escape(&formula_ascii_to_tex(&e.as_display())); format!( "
    formula \ `{}` = \
    $${}$$
    ", escape(name), tex ) } Stmt::Assign { name, value: Expr::Code(c), .. } => { let placeholder = escape(&crate::foreign::default_cmd_display(&c.lang)); let lang = escape(&c.lang); let src = escape(&c.source); format!( r#"
    code `{name}` = {lang}
    {src}
    "#, lang = lang, name = escape(name), placeholder = placeholder, src = src, ) } Stmt::Assign { name, value, .. } => format!( "
    bind `{}` = {}
    ", escape(name), escape(&expr_display(value)) ), Stmt::Return { value, .. } => format!( "
    return{}
    ", escape(&expr_display(value)) ), Stmt::Call { call, .. } => format!( "
    call{}
    ", escape(&call_display(call)) ), Stmt::Branch { arms, .. } => { let mut body = String::from("
    branch"); let mut arm_cursor = stmt_start(stmt) + 1; for arm in arms { let label = match &arm.condition { None => "else".to_string(), Some(c) => expr_display(c), }; // comments before first stmt in arm (approx: between arms hard; use body starts) let arm_first = arm.body.first().map(stmt_start); body.push_str(&format!( "
    arm{}", escape(&label) )); if let Some(af) = arm_first { body.push_str(&emit_comments(lines, arm_cursor, af)); arm_cursor = af; } body.push_str("
    "); 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); body.push_str(&h); arm_cursor = e; } body.push_str("
    "); } body.push_str("
    "); body } Stmt::While { condition, body, .. } => { let mut body_html = format!( "
    while{}
    ", escape(&expr_display(condition)) ); let mut cursor = stmt_start(stmt) + 1; 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); body_html.push_str(&h); cursor = e; } body_html.push_str("
    "); body_html } Stmt::ForEach { item, collection, body, .. } => { let mut body_html = format!( "
    foreach[{}]({})
    ", escape(item), escape(collection) ); let mut cursor = stmt_start(stmt) + 1; 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); body_html.push_str(&h); cursor = e; } body_html.push_str("
    "); body_html } }; (stmt_shell(line, &inner, mode), end) } fn stmt_shell(line: u32, inner: &str, mode: StructureMode) -> String { match mode { StructureMode::Browse => format!( r#"
    {inner}
    "#, line = line, inner = inner, ), StructureMode::Debug => format!( r#"
    {inner}
    "#, line = line, inner = inner, ), } } fn call_display(call: &CallExpr) -> String { let mut s = format!("> {}", call.callee); for a in &call.args { match a { Arg::Positional(e) => { s.push(' '); s.push_str(&expr_display(e)); } Arg::Named { name, value } => { s.push(' '); s.push_str(name); s.push('='); s.push_str(&expr_display(value)); } } } s } /// Surface-syntax preview for view (not Debug). pub fn expr_display(expr: &Expr) -> String { expr_prec(expr, 0) } fn expr_prec(expr: &Expr, parent_prec: u8) -> String { match expr { Expr::Literal(lit) => lit_display(lit), Expr::Var(name) => format!("`{name}`"), Expr::Interp(parts) => { let mut s = String::new(); for p in parts { match p { InterpPart::Lit(t) => s.push_str(t), InterpPart::Var(n) => { s.push('`'); s.push_str(n); s.push('`'); } } } s } Expr::Unary { op, expr } => { let prec = 5; let inner = match op { UnaryOp::Not => format!("not {}", expr_prec(expr, prec)), UnaryOp::Neg => format!("-{}", expr_prec(expr, prec)), }; wrap(inner, prec, parent_prec) } Expr::Binary { op, left, right } => { let (sym, prec) = bin_sym_prec(*op); let inner = format!( "{} {} {}", expr_prec(left, prec), sym, expr_prec(right, prec + 1) // left-assoc bias ); wrap(inner, prec, parent_prec) } Expr::Call(call) => call_display(call), Expr::List(items) => { let parts: Vec = items.iter().map(|e| expr_prec(e, 0)).collect(); format!("[{}]", parts.join(", ")) } Expr::Formula(e) => format!("$$ {} $$", e.as_display()), Expr::Code(c) => format!("```{} …```", c.lang), } } /// Best-effort ASCII formula → TeX for KaTeX in view Structure. fn formula_ascii_to_tex(ascii: &str) -> String { let mut out = String::new(); let chars: Vec = ascii.chars().collect(); let mut i = 0usize; while i < chars.len() { let c = chars[i]; if c == '*' { // multiplication: a*b → a \cdot b out.push_str(r" \cdot "); i += 1; continue; } if c == '^' { out.push('^'); i += 1; if i < chars.len() && chars[i] == '(' { out.push('{'); i += 1; let start = i; let mut depth = 1i32; while i < chars.len() && depth > 0 { match chars[i] { '(' => depth += 1, ')' => depth -= 1, _ => {} } if depth > 0 { i += 1; } } out.push_str(&chars[start..i].iter().collect::()); if i < chars.len() && chars[i] == ')' { i += 1; } out.push('}'); } else if i < chars.len() { out.push('{'); out.push(chars[i]); out.push('}'); i += 1; } continue; } // TeX specials that may appear in identifiers rarely match c { '{' | '}' | '%' | '&' | '#' | '$' => { out.push('\\'); out.push(c); } _ => out.push(c), } i += 1; } out } fn wrap(inner: String, prec: u8, parent_prec: u8) -> String { if prec < parent_prec { format!("({inner})") } else { inner } } fn bin_sym_prec(op: BinaryOp) -> (&'static str, u8) { match op { BinaryOp::Or => ("or", 1), BinaryOp::And => ("and", 2), BinaryOp::Eq => ("==", 3), BinaryOp::Ne => ("!=", 3), BinaryOp::Lt => ("<", 3), BinaryOp::Le => ("<=", 3), BinaryOp::Gt => (">", 3), BinaryOp::Ge => (">=", 3), BinaryOp::Add => ("+", 4), BinaryOp::Sub => ("-", 4), BinaryOp::Mul => ("*", 5), BinaryOp::Div => ("/", 5), } } fn lit_display(lit: &Literal) -> String { match lit { Literal::None => "None".into(), Literal::Bool(true) => "True".into(), Literal::Bool(false) => "False".into(), Literal::Int(n) => n.to_string(), Literal::Text(t) => t.clone(), } } #[cfg(test)] mod tests { use super::*; use crate::ast::{BinaryOp, Expr, Literal}; #[test] fn expr_gt_is_surface() { let e = Expr::Binary { op: BinaryOp::Gt, left: Box::new(Expr::Var("x".into())), right: Box::new(Expr::Literal(Literal::Int(0))), }; assert_eq!(expr_display(&e), "`x` > 0"); assert!(!expr_display(&e).contains("Binary")); } #[test] fn structure_keeps_comments_and_surface_expr() { let src = include_str!("../../tests/structure/nested-call.mq.md"); let module = crate::parse::parse_source(src).unwrap(); let html = render_module_structure(&module, src); assert!(html.contains("comment-card")); assert!(html.contains("print")); assert!(!html.contains("Binary {")); assert!(!html.contains("Literal(Int")); } #[test] fn comments_render_as_paragraphs() { let src = "段首说明\n续行仍属同段\n\n下一段\n\n# main\n\n> print text=x\n"; let module = crate::parse::parse_source(src).unwrap(); let html = render_module_structure(&module, src); let cards = html.matches("comment-card").count(); assert_eq!(cards, 2, "blank line should split comment paragraphs: {html}"); assert!(html.contains("段首说明 续行仍属同段") || html.contains("段首说明

    ")); assert!(html.contains("下一段")); } #[test] fn comments_render_markdown_links() { let src = "见[仓库](https://github.com/cflmy/marqdo)与 `code`\n\n# main\n\n> print text=x\n"; let module = crate::parse::parse_source(src).unwrap(); let html = render_module_structure(&module, src); assert!( html.contains("仓库"), "{html}" ); assert!(html.contains("code"), "{html}"); assert!(!html.contains("[仓库](https://github.com/cflmy/marqdo)")); } #[test] fn collect_input_prompts_from_fixture() { let src = include_str!("../../tests/keywords/input.mq.md"); let module = crate::parse::parse_source(src).unwrap(); let prompts = collect_input_prompts(&module); assert_eq!(prompts, vec!["Name:".to_string()]); } #[test] fn collect_input_prompts_absent_without_input() { let src = include_str!("../../tests/structure/hello.mq.md"); let module = crate::parse::parse_source(src).unwrap(); assert!(collect_input_prompts(&module).is_empty()); } #[test] fn structure_omits_imported_lib_bodies() { let src = "---\ntitle: t\n> lib/text.mq.md\n---\n\n# main\n\n> print text=hi\n"; let local = crate::parse::parse_source(src).unwrap(); let html = render_module_structure(&local, src); assert!(html.contains("badge\">import"), "{html}"); assert!(html.contains("lib/text.mq.md"), "{html}"); assert!(html.contains("main"), "{html}"); assert!(!html.contains("trim"), "{html}"); assert!(html.contains("class=\"stmt\""), "{html}"); assert!(html.contains("data-line="), "{html}"); assert_eq!(local.functions.len(), 1); assert_eq!(local.functions[0].name, "main"); } #[test] fn structure_formula_assign_not_comment() { let src = include_str!("../../tests/lib/math-formula.mq.md"); let module = crate::parse::parse_source(src).unwrap(); let html = render_module_structure(&module, src); assert!(html.contains("badge\">formula"), "{html}"); assert!(html.contains("math-block"), "{html}"); assert!(html.contains("$$"), "{html}"); assert!( !html.contains("comment-text\">x^2 - 2") && !html.contains("comment-text\">

    x^2 - 2"), "formula fence leaked into comments: {html}" ); } #[test] fn structure_code_card() { let src = include_str!("../../tests/lib/foreign-python.mq.md"); let module = crate::parse::parse_source(src).unwrap(); let html = render_module_structure(&module, src); assert!(html.contains("code-card"), "{html}"); assert!(html.contains("code-run"), "{html}"); assert!(html.contains("hello-from-python"), "{html}"); assert!(!html.contains("python name=hi"), "{html}"); } #[test] fn formula_ascii_to_tex_pow_and_mul() { assert_eq!(formula_ascii_to_tex("x^2 - 2"), "x^{2} - 2"); assert_eq!(formula_ascii_to_tex("2*x"), r"2 \cdot x"); } #[test] fn structure_mode_browse_vs_debug() { let src = include_str!("../../tests/structure/nested-call.mq.md"); let module = crate::parse::parse_source(src).unwrap(); let browse = render_module_structure_mode(&module, src, StructureMode::Browse); let debug = render_module_structure_mode(&module, src, StructureMode::Debug); assert!(browse.contains("id=\"fn-"), "{browse}"); assert!(!browse.contains("bp-gutter"), "{browse}"); assert!(debug.contains("bp-gutter"), "{debug}"); assert!(debug.contains("id=\"stmt-"), "{debug}"); } #[test] fn function_outline_lists_nested_names() { let src = include_str!("../../tests/structure/nested-call.mq.md"); let module = crate::parse::parse_source(src).unwrap(); let outline = render_function_outline(&module); assert!(outline.contains("outline-item"), "{outline}"); assert!(outline.contains("data-fn=\"main\""), "{outline}"); assert!(outline.contains("data-fn=\"问候\""), "{outline}"); assert!(outline.contains("data-fn-path=\"main/问候\""), "{outline}"); assert!(outline.contains("href=\"#fn-"), "{outline}"); assert!(outline.contains("ol-meta\">h"), "{outline}"); } }