Add foreign stdlib with code values and ship v0.0.4.
Bind `lang fences like formulas, run them via local interpreters, polish live-view code cards, and refresh bilingual welcome docs. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
dcd0cdb8f7
commit
b5e9d15391
33 files changed
+1044
-199
No files matched your search
@@ -394,6 +394,87 @@ section.block > h2 {
|
||||
.formula-card .katex-display {
|
||||
margin: 0.2rem 0;
|
||||
}
|
||||
.code-card {
|
||||
padding: 0;
|
||||
overflow: hidden;
|
||||
}
|
||||
.code-head {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: center;
|
||||
gap: 0.45rem 0.65rem;
|
||||
padding: 0.65rem 0.85rem;
|
||||
border-bottom: 1px solid var(--line);
|
||||
background: #fafafa;
|
||||
}
|
||||
.code-lang {
|
||||
font: 0.72rem/1 var(--mono);
|
||||
color: var(--muted);
|
||||
text-transform: lowercase;
|
||||
}
|
||||
.code-actions {
|
||||
margin-left: auto;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.4rem;
|
||||
}
|
||||
.code-cmd {
|
||||
min-width: 9rem;
|
||||
max-width: 16rem;
|
||||
padding: 0.35rem 0.55rem;
|
||||
border: 1px solid var(--line);
|
||||
border-radius: 8px;
|
||||
font: 0.78rem/1.2 var(--mono);
|
||||
color: var(--ink);
|
||||
background: #fff;
|
||||
}
|
||||
.code-cmd:focus {
|
||||
outline: 2px solid rgba(29,29,31,0.18);
|
||||
outline-offset: 1px;
|
||||
}
|
||||
.code-run {
|
||||
padding: 0.35rem 0.75rem;
|
||||
border: none;
|
||||
border-radius: 8px;
|
||||
background: var(--ink);
|
||||
color: #fff;
|
||||
font: 0.78rem/1.2 var(--sans);
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
}
|
||||
.code-run:hover { opacity: 0.9; }
|
||||
.code-run:disabled {
|
||||
opacity: 0.45;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
.code-body {
|
||||
margin: 0;
|
||||
padding: 0.85rem 1rem;
|
||||
overflow: auto;
|
||||
background: #1e1e1e;
|
||||
color: #d4d4d4;
|
||||
font: 0.82rem/1.45 var(--mono);
|
||||
}
|
||||
.code-body code {
|
||||
font: inherit;
|
||||
background: none;
|
||||
padding: 0;
|
||||
color: inherit;
|
||||
}
|
||||
.code-out {
|
||||
margin: 0;
|
||||
padding: 0.75rem 1rem;
|
||||
border-top: 1px solid var(--line);
|
||||
background: #f0f0f2;
|
||||
font: 0.8rem/1.45 var(--mono);
|
||||
white-space: pre-wrap;
|
||||
word-break: break-word;
|
||||
}
|
||||
.code-out.err {
|
||||
background: #fff5f5;
|
||||
color: #9b1c1c;
|
||||
}
|
||||
.code-out:not([hidden]) { display: block; }
|
||||
.plots {
|
||||
display: grid;
|
||||
gap: 1rem;
|
||||
@@ -514,6 +595,7 @@ pub fn layout(title: &str, nav: &str, main: &str) -> String {
|
||||
<link rel="shortcut icon" href="https://s3.cflmy.cn/logo/Logo.ico" type="image/x-icon"/>
|
||||
<link rel="apple-touch-icon" href="https://s3.cflmy.cn/logo/Logo.png"/>
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/katex.min.css" crossorigin="anonymous"/>
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@11.9.0/styles/github-dark.min.css" crossorigin="anonymous"/>
|
||||
<style>{css}</style>
|
||||
</head>
|
||||
<body>
|
||||
@@ -548,6 +630,63 @@ pub fn layout(title: &str, nav: &str, main: &str) -> String {
|
||||
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/katex.min.js" crossorigin="anonymous"></script>
|
||||
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/contrib/auto-render.min.js" crossorigin="anonymous"
|
||||
onload="renderMathInElement(document.body,{{delimiters:[{{left:'$$',right:'$$',display:true}},{{left:'\\(',right:'\\)',display:false}}],ignoredTags:['script','noscript','style','textarea','pre','code','kbd']}});"></script>
|
||||
<script defer src="https://cdn.jsdelivr.net/npm/highlight.js@11.9.0/highlight.min.js" crossorigin="anonymous"
|
||||
onload="document.querySelectorAll('pre.code-body code').forEach(function(el){{hljs.highlightElement(el);}});"></script>
|
||||
<script>
|
||||
(function () {{
|
||||
function initForeignCards() {{
|
||||
var live = !!window.MARQDO_VIEW_LIVE;
|
||||
document.querySelectorAll(".code-card").forEach(function (card) {{
|
||||
var btn = card.querySelector(".code-run");
|
||||
var cmd = card.querySelector(".code-cmd");
|
||||
var out = card.querySelector(".code-out");
|
||||
var body = card.querySelector(".code-body code");
|
||||
if (!btn || !out || !body) return;
|
||||
if (!live) {{
|
||||
btn.disabled = true;
|
||||
btn.title = "Run is available in live marqdo view";
|
||||
if (cmd) cmd.disabled = true;
|
||||
return;
|
||||
}}
|
||||
btn.addEventListener("click", function () {{
|
||||
btn.disabled = true;
|
||||
out.hidden = false;
|
||||
out.classList.remove("err");
|
||||
out.textContent = "Running…";
|
||||
fetch("/api/foreign-run", {{
|
||||
method: "POST",
|
||||
headers: {{ "Content-Type": "application/json" }},
|
||||
body: JSON.stringify({{
|
||||
lang: card.getAttribute("data-lang") || "python",
|
||||
source: body.textContent,
|
||||
cmd: cmd ? cmd.value : ""
|
||||
}})
|
||||
}})
|
||||
.then(function (r) {{ return r.json(); }})
|
||||
.then(function (data) {{
|
||||
if (data.ok) {{
|
||||
out.classList.remove("err");
|
||||
out.textContent = data.stdout === "" ? "(no stdout)" : data.stdout;
|
||||
}} else {{
|
||||
out.classList.add("err");
|
||||
out.textContent = data.error || "foreign run failed";
|
||||
}}
|
||||
}})
|
||||
.catch(function (e) {{
|
||||
out.classList.add("err");
|
||||
out.textContent = String(e);
|
||||
}})
|
||||
.finally(function () {{ btn.disabled = false; }});
|
||||
}});
|
||||
}});
|
||||
}}
|
||||
if (document.readyState === "loading") {{
|
||||
document.addEventListener("DOMContentLoaded", initForeignCards);
|
||||
}} else {{
|
||||
initForeignCards();
|
||||
}}
|
||||
}})();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
"##,
|
||||
@@ -789,8 +928,13 @@ pub fn page_file(files: &[PathBuf], rel: &str, vm: &FileViewModel, links: &LinkM
|
||||
s.push_str("</div>");
|
||||
s
|
||||
};
|
||||
let live_flag = match links {
|
||||
LinkMode::Live => "true",
|
||||
LinkMode::Static { .. } => "false",
|
||||
};
|
||||
let main = format!(
|
||||
r#"
|
||||
<script>window.MARQDO_VIEW_LIVE = {live};</script>
|
||||
<h1 class="page">{title}<span class="status-pill {status}">{status_label}</span></h1>
|
||||
<p class="meta">{rel}</p>
|
||||
<section class="block">
|
||||
@@ -808,6 +952,7 @@ pub fn page_file(files: &[PathBuf], rel: &str, vm: &FileViewModel, links: &LinkM
|
||||
<pre class="source">{source}</pre>
|
||||
</section>
|
||||
"#,
|
||||
live = live_flag,
|
||||
title = escape(title),
|
||||
status = status,
|
||||
status_label = status_label,
|
||||
|
||||
@@ -117,6 +117,17 @@ fn is_mq_md(path: &Path) -> bool {
|
||||
fn handle(root: &ViewRoot, request: Request) -> Result<()> {
|
||||
let url = request.url().to_string();
|
||||
let method = request.method().clone();
|
||||
|
||||
if method == Method::Post {
|
||||
let (path_part, _) = split_url(&url);
|
||||
if path_part == "/api/foreign-run" {
|
||||
return api_foreign_run(root, request);
|
||||
}
|
||||
let resp = Response::from_string("method not allowed").with_status_code(StatusCode(405));
|
||||
let _ = request.respond(resp);
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if method != Method::Get {
|
||||
let resp = Response::from_string("method not allowed").with_status_code(StatusCode(405));
|
||||
let _ = request.respond(resp);
|
||||
@@ -338,6 +349,37 @@ fn escape_json(s: &str) -> String {
|
||||
s.replace('\\', "\\\\").replace('"', "\\\"")
|
||||
}
|
||||
|
||||
fn api_foreign_run(root: &ViewRoot, mut request: Request) -> Result<()> {
|
||||
let mut body = Vec::new();
|
||||
std::io::Read::read_to_end(&mut request.as_reader(), &mut body)?;
|
||||
let body = String::from_utf8_lossy(&body);
|
||||
let v: serde_json::Value = serde_json::from_str(&body).unwrap_or(serde_json::Value::Null);
|
||||
let lang = v
|
||||
.get("lang")
|
||||
.and_then(|x| x.as_str())
|
||||
.unwrap_or("python");
|
||||
let source = v.get("source").and_then(|x| x.as_str()).unwrap_or("");
|
||||
let cmd = v
|
||||
.get("cmd")
|
||||
.and_then(|x| x.as_str())
|
||||
.map(str::trim)
|
||||
.filter(|s| !s.is_empty());
|
||||
let json = if source.is_empty() {
|
||||
serde_json::json!({ "ok": false, "error": "missing source" }).to_string()
|
||||
} else {
|
||||
match crate::host::foreign::run_with_cmd_override(
|
||||
&root.root,
|
||||
lang,
|
||||
source,
|
||||
cmd,
|
||||
) {
|
||||
Ok(stdout) => serde_json::json!({ "ok": true, "stdout": stdout }).to_string(),
|
||||
Err(error) => serde_json::json!({ "ok": false, "error": error }).to_string(),
|
||||
}
|
||||
};
|
||||
respond_json(request, json)
|
||||
}
|
||||
|
||||
fn respond_html(request: Request, body: String) -> Result<()> {
|
||||
let header = Header::from_bytes(&b"Content-Type"[..], &b"text/html; charset=utf-8"[..])
|
||||
.map_err(|_| anyhow::anyhow!("header"))?;
|
||||
|
||||
+41
-1
@@ -108,7 +108,7 @@ fn collect_input_prompts_expr(expr: &Expr, out: &mut Vec<String>) {
|
||||
collect_input_prompts_expr(e, out);
|
||||
}
|
||||
}
|
||||
Expr::Literal(_) | Expr::Var(_) | Expr::Interp(_) | Expr::Formula(_) => {}
|
||||
Expr::Literal(_) | Expr::Var(_) | Expr::Interp(_) | Expr::Formula(_) | Expr::Code(_) => {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -364,6 +364,34 @@ fn render_stmt(stmt: &Stmt, lines: &[ClassifiedLine]) -> (String, u32) {
|
||||
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#"<div class="card code-card" data-lang="{lang}">
|
||||
<div class="code-head">
|
||||
<span class="badge">code</span>
|
||||
<code class="expr">`{name}` =</code>
|
||||
<span class="code-lang">{lang}</span>
|
||||
<div class="code-actions">
|
||||
<input type="text" class="code-cmd" placeholder="{placeholder}" spellcheck="false" autocomplete="off"/>
|
||||
<button type="button" class="code-run">Run</button>
|
||||
</div>
|
||||
</div>
|
||||
<pre class="code-body"><code class="language-{lang}">{src}</code></pre>
|
||||
<pre class="code-out" hidden></pre>
|
||||
</div>"#,
|
||||
lang = lang,
|
||||
name = escape(name),
|
||||
placeholder = placeholder,
|
||||
src = src,
|
||||
)
|
||||
}
|
||||
Stmt::Assign { name, value, .. } => format!(
|
||||
"<div class=\"card\"><span class=\"badge\">bind</span> <code class=\"expr\">`{}` = {}</code></div>",
|
||||
escape(name),
|
||||
@@ -519,6 +547,7 @@ fn expr_prec(expr: &Expr, parent_prec: u8) -> String {
|
||||
format!("[{}]", parts.join(", "))
|
||||
}
|
||||
Expr::Formula(e) => format!("$$ {} $$", e.as_display()),
|
||||
Expr::Code(c) => format!("```{} …```", c.lang),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -708,6 +737,17 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[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");
|
||||
|
||||
Reference in new issue
Block a user