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
@@ -60,6 +60,11 @@ pub fn canonical_param(builtin: &str, param: &str) -> String {
|
||||
}
|
||||
("host_plot", "steps" | "步数") => "steps".into(),
|
||||
("host_plot", "derivative" | "导数") => "derivative".into(),
|
||||
("host_foreign_run", "code" | "代码") => "code".into(),
|
||||
("host_foreign_run" | "host_foreign_run_lang", "stdin" | "标准输入") => "stdin".into(),
|
||||
("host_foreign_set_cmd" | "host_foreign_run_lang", "lang" | "语言") => "lang".into(),
|
||||
("host_foreign_set_cmd", "cmd" | "命令") => "cmd".into(),
|
||||
("host_foreign_run_lang", "source" | "源码") => "source".into(),
|
||||
_ => param.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
use crate::diagnostics::Span;
|
||||
use crate::formula::Expr as FormulaExpr;
|
||||
use crate::value::CodeBlock;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Module {
|
||||
@@ -91,6 +92,8 @@ pub enum Expr {
|
||||
List(Vec<Expr>),
|
||||
/// Parsed `$$…$$` formula tree (from assignment RHS).
|
||||
Formula(FormulaExpr),
|
||||
/// Bound ```lang … ``` fence (`code` value).
|
||||
Code(CodeBlock),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
|
||||
@@ -44,6 +44,7 @@ pub fn builtin_type(v: &Value) -> Value {
|
||||
Value::List(_) => "list",
|
||||
Value::Map(_) => "map",
|
||||
Value::Formula(_) => "formula",
|
||||
Value::Code(_) => "code",
|
||||
}
|
||||
.into(),
|
||||
)
|
||||
|
||||
@@ -641,6 +641,10 @@ impl<'a> FnCompiler<'a> {
|
||||
let i = self.add_const(Value::Formula(e.clone()));
|
||||
self.emit(Op::Constant(i));
|
||||
}
|
||||
Expr::Code(c) => {
|
||||
let i = self.add_const(Value::Code(c.clone()));
|
||||
self.emit(Op::Constant(i));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
//! Foreign / code-block helpers (外联).
|
||||
|
||||
/// Default interpreter argv for a language tag (before env / set_cmd).
|
||||
pub fn default_argv(lang: &str) -> Option<Vec<String>> {
|
||||
match lang.to_ascii_lowercase().as_str() {
|
||||
"python" | "python3" | "py" => Some(default_python_argv()),
|
||||
"javascript" | "js" | "node" => Some(vec!["node".into()]),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn default_python_argv() -> Vec<String> {
|
||||
#[cfg(windows)]
|
||||
{
|
||||
vec!["python".into()]
|
||||
}
|
||||
#[cfg(not(windows))]
|
||||
{
|
||||
vec!["python3".into()]
|
||||
}
|
||||
}
|
||||
|
||||
/// Placeholder shown in the view command box.
|
||||
pub fn default_cmd_display(lang: &str) -> String {
|
||||
default_argv(lang)
|
||||
.map(|a| a.join(" "))
|
||||
.unwrap_or_else(|| lang.to_string())
|
||||
}
|
||||
|
||||
/// Parse ```lang … opener → language id (first token).
|
||||
pub fn fence_lang(opener_trimmed: &str) -> Option<String> {
|
||||
let rest = opener_trimmed.strip_prefix("```")?;
|
||||
if rest.is_empty() || rest.chars().all(|c| c == '`') {
|
||||
return None;
|
||||
}
|
||||
let lang = rest.split_whitespace().next()?.to_string();
|
||||
if lang.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(lang)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_fence_opener(trimmed: &str) -> bool {
|
||||
fence_lang(trimmed).is_some()
|
||||
}
|
||||
|
||||
pub fn is_fence_closer(trimmed: &str) -> bool {
|
||||
trimmed.starts_with("```")
|
||||
&& trimmed
|
||||
.chars()
|
||||
.skip_while(|c| *c == '`')
|
||||
.all(|c| c.is_whitespace())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn fence_lang_python() {
|
||||
assert_eq!(fence_lang("```python").as_deref(), Some("python"));
|
||||
assert_eq!(fence_lang("```python name=x").as_deref(), Some("python"));
|
||||
assert!(fence_lang("```").is_none());
|
||||
}
|
||||
}
|
||||
+37
-1
@@ -2,7 +2,7 @@
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::host::{fs, json, math, net, sys, time, HostContext};
|
||||
use crate::host::{foreign, fs, json, math, net, sys, time, HostContext};
|
||||
use crate::value::Value;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
@@ -70,6 +70,10 @@ pub enum HostFn {
|
||||
Plot = 59,
|
||||
PlotPoints = 60,
|
||||
PlotConic = 61,
|
||||
ForeignSetCmd = 62,
|
||||
ForeignRun = 63,
|
||||
ForeignRunLang = 64,
|
||||
ForeignLangs = 65,
|
||||
}
|
||||
|
||||
impl HostFn {
|
||||
@@ -136,6 +140,10 @@ impl HostFn {
|
||||
59 => Self::Plot,
|
||||
60 => Self::PlotPoints,
|
||||
61 => Self::PlotConic,
|
||||
62 => Self::ForeignSetCmd,
|
||||
63 => Self::ForeignRun,
|
||||
64 => Self::ForeignRunLang,
|
||||
65 => Self::ForeignLangs,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
@@ -205,6 +213,10 @@ impl HostFn {
|
||||
"host_plot" | "plot" | "绘图" => Self::Plot,
|
||||
"host_plot_points" | "plot_points" | "绘点列" => Self::PlotPoints,
|
||||
"host_plot_conic" | "plot_conic" | "绘圆锥" => Self::PlotConic,
|
||||
"host_foreign_set_cmd" => Self::ForeignSetCmd,
|
||||
"host_foreign_run" => Self::ForeignRun,
|
||||
"host_foreign_run_lang" => Self::ForeignRunLang,
|
||||
"host_foreign_langs" => Self::ForeignLangs,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
@@ -276,6 +288,10 @@ impl HostFn {
|
||||
Self::Plot => "host_plot",
|
||||
Self::PlotPoints => "host_plot_points",
|
||||
Self::PlotConic => "host_plot_conic",
|
||||
Self::ForeignSetCmd => "host_foreign_set_cmd",
|
||||
Self::ForeignRun => "host_foreign_run",
|
||||
Self::ForeignRunLang => "host_foreign_run_lang",
|
||||
Self::ForeignLangs => "host_foreign_langs",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -312,6 +328,10 @@ impl HostFn {
|
||||
Self::Plot => &["formula", "var", "min", "max"],
|
||||
Self::PlotPoints => &["xs", "ys"],
|
||||
Self::PlotConic => &["kind", "a"],
|
||||
Self::ForeignSetCmd => &["lang", "cmd"],
|
||||
Self::ForeignRun => &["code"],
|
||||
Self::ForeignRunLang => &["lang", "source"],
|
||||
Self::ForeignLangs => &[],
|
||||
}
|
||||
}
|
||||
|
||||
@@ -324,6 +344,8 @@ impl HostFn {
|
||||
Self::Plot => &["steps", "path", "derivative", "grid"],
|
||||
Self::PlotPoints => &["path", "grid"],
|
||||
Self::PlotConic => &["b", "h", "k", "path", "grid"],
|
||||
Self::ForeignSetCmd => &["args"],
|
||||
Self::ForeignRun | Self::ForeignRunLang => &["stdin"],
|
||||
_ => &[],
|
||||
}
|
||||
}
|
||||
@@ -457,6 +479,20 @@ pub fn call_host(
|
||||
bound.get("path"),
|
||||
bound.get("grid"),
|
||||
),
|
||||
HostFn::ForeignSetCmd => foreign::set_cmd(
|
||||
ctx,
|
||||
require(bound, "lang")?,
|
||||
require(bound, "cmd")?,
|
||||
bound.get("args"),
|
||||
),
|
||||
HostFn::ForeignRun => foreign::run(ctx, require(bound, "code")?, bound.get("stdin")),
|
||||
HostFn::ForeignRunLang => foreign::run_lang(
|
||||
ctx,
|
||||
require(bound, "lang")?,
|
||||
require(bound, "source")?,
|
||||
bound.get("stdin"),
|
||||
),
|
||||
HostFn::ForeignLangs => foreign::langs(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,326 @@
|
||||
//! Foreign language runners (subprocess). Import L1 `lib/foreign` to use.
|
||||
|
||||
use std::io::{Read, Write};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::{Command, Stdio};
|
||||
use std::thread;
|
||||
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
|
||||
|
||||
use crate::foreign;
|
||||
use crate::host::HostContext;
|
||||
use crate::value::{CodeBlock, Value};
|
||||
|
||||
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(10);
|
||||
const MAX_OUTPUT: usize = 1_048_576;
|
||||
|
||||
fn as_text<'a>(v: &'a Value, what: &str) -> Result<&'a str, String> {
|
||||
match v {
|
||||
Value::Text(s) => Ok(s.as_str()),
|
||||
_ => Err(format!("{what} must be text")),
|
||||
}
|
||||
}
|
||||
|
||||
fn opt_text(v: Option<&Value>) -> Result<Option<&str>, String> {
|
||||
match v {
|
||||
None | Some(Value::None) => Ok(None),
|
||||
Some(v) => Ok(Some(as_text(v, "value")?)),
|
||||
}
|
||||
}
|
||||
|
||||
fn as_code(v: &Value) -> Result<&CodeBlock, String> {
|
||||
match v {
|
||||
Value::Code(c) => Ok(c),
|
||||
_ => Err("run needs a code value (bind with `name` = then a ```lang fence)".into()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Register interpreter argv for `lang` (e.g. venv python).
|
||||
pub fn set_cmd(
|
||||
ctx: &mut HostContext,
|
||||
lang: &Value,
|
||||
cmd: &Value,
|
||||
args: Option<&Value>,
|
||||
) -> Result<Value, String> {
|
||||
let lang = as_text(lang, "lang")?.to_ascii_lowercase();
|
||||
let cmd = as_text(cmd, "cmd")?;
|
||||
let mut argv = vec![cmd.to_string()];
|
||||
if let Some(a) = args {
|
||||
match a {
|
||||
Value::List(xs) => {
|
||||
for x in xs {
|
||||
argv.push(x.as_display());
|
||||
}
|
||||
}
|
||||
Value::Text(s) => argv.push(s.clone()),
|
||||
Value::None => {}
|
||||
_ => return Err("set_cmd args must be list or text".into()),
|
||||
}
|
||||
}
|
||||
ctx.foreign_cmds.insert(lang, argv);
|
||||
Ok(Value::None)
|
||||
}
|
||||
|
||||
pub fn run(
|
||||
ctx: &mut HostContext,
|
||||
code: &Value,
|
||||
stdin: Option<&Value>,
|
||||
) -> Result<Value, String> {
|
||||
let block = as_code(code)?;
|
||||
let stdin = opt_text(stdin)?;
|
||||
run_source(ctx, &block.lang, &block.source, stdin)
|
||||
}
|
||||
|
||||
pub fn run_lang(
|
||||
ctx: &mut HostContext,
|
||||
lang: &Value,
|
||||
source: &Value,
|
||||
stdin: Option<&Value>,
|
||||
) -> Result<Value, String> {
|
||||
let lang = as_text(lang, "lang")?;
|
||||
let source = as_text(source, "source")?;
|
||||
let stdin = opt_text(stdin)?;
|
||||
run_source(ctx, lang, source, stdin)
|
||||
}
|
||||
|
||||
pub fn langs(ctx: &HostContext) -> Result<Value, String> {
|
||||
let mut names: Vec<String> = ctx.foreign_cmds.keys().cloned().collect();
|
||||
if !names.iter().any(|n| n == "python" || n == "python3") {
|
||||
names.push("python".into());
|
||||
}
|
||||
names.sort();
|
||||
names.dedup();
|
||||
Ok(Value::List(names.into_iter().map(Value::Text).collect()))
|
||||
}
|
||||
|
||||
/// Shared entry for CLI host and live view `/api/foreign-run`.
|
||||
pub fn run_source(
|
||||
ctx: &HostContext,
|
||||
lang: &str,
|
||||
source: &str,
|
||||
stdin: Option<&str>,
|
||||
) -> Result<Value, String> {
|
||||
let lang_key = lang.to_ascii_lowercase();
|
||||
let argv = resolve_argv(ctx, &lang_key)?;
|
||||
let script_path = write_temp_script(&lang_key, source)?;
|
||||
let result = run_argv(ctx, &argv, &script_path, stdin, &lang_key);
|
||||
let _ = std::fs::remove_file(&script_path);
|
||||
result
|
||||
}
|
||||
|
||||
/// Resolve argv with optional one-shot cmd override (view command box).
|
||||
pub fn resolve_argv_with_override(
|
||||
ctx: &HostContext,
|
||||
lang: &str,
|
||||
cmd_override: Option<&str>,
|
||||
) -> Result<Vec<String>, String> {
|
||||
if let Some(cmd) = cmd_override.map(str::trim).filter(|s| !s.is_empty()) {
|
||||
return Ok(split_cmd_line(cmd));
|
||||
}
|
||||
resolve_argv(ctx, lang)
|
||||
}
|
||||
|
||||
fn resolve_argv(ctx: &HostContext, lang: &str) -> Result<Vec<String>, String> {
|
||||
if let Some(argv) = ctx.foreign_cmds.get(lang) {
|
||||
return Ok(argv.clone());
|
||||
}
|
||||
if lang == "python3" {
|
||||
if let Some(argv) = ctx.foreign_cmds.get("python") {
|
||||
return Ok(argv.clone());
|
||||
}
|
||||
}
|
||||
if lang == "python" {
|
||||
if let Some(argv) = ctx.foreign_cmds.get("python3") {
|
||||
return Ok(argv.clone());
|
||||
}
|
||||
}
|
||||
|
||||
let env_key = format!("MARQDO_FOREIGN_{}", lang.to_ascii_uppercase());
|
||||
if let Ok(v) = std::env::var(&env_key) {
|
||||
let v = v.trim();
|
||||
if !v.is_empty() {
|
||||
return Ok(split_cmd_line(v));
|
||||
}
|
||||
}
|
||||
if lang == "python" || lang == "python3" || lang == "py" {
|
||||
if let Ok(v) = std::env::var("MARQDO_FOREIGN_PYTHON") {
|
||||
let v = v.trim();
|
||||
if !v.is_empty() {
|
||||
return Ok(split_cmd_line(v));
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(argv) = foreign::default_argv(lang) {
|
||||
return Ok(argv);
|
||||
}
|
||||
|
||||
Err(format!(
|
||||
"no interpreter configured for lang=`{lang}` — set_cmd lang={lang} cmd=… \
|
||||
or env {env_key} (check your environment)"
|
||||
))
|
||||
}
|
||||
|
||||
fn split_cmd_line(s: &str) -> Vec<String> {
|
||||
s.split_whitespace().map(|p| p.to_string()).collect()
|
||||
}
|
||||
|
||||
fn write_temp_script(lang: &str, source: &str) -> Result<PathBuf, String> {
|
||||
let ext = match lang {
|
||||
"python" | "python3" | "py" => "py",
|
||||
"javascript" | "js" | "node" => "js",
|
||||
other => {
|
||||
if other.len() <= 8 && other.chars().all(|c| c.is_ascii_alphanumeric()) {
|
||||
other
|
||||
} else {
|
||||
"txt"
|
||||
}
|
||||
}
|
||||
};
|
||||
let body = if matches!(lang, "python" | "python3" | "py") {
|
||||
format!("# -*- coding: utf-8 -*-\n{source}")
|
||||
} else {
|
||||
source.to_string()
|
||||
};
|
||||
let name = format!(
|
||||
"marqdo-foreign-{}-{}.{}",
|
||||
std::process::id(),
|
||||
SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.map(|d| d.as_nanos())
|
||||
.unwrap_or(0),
|
||||
ext
|
||||
);
|
||||
let path = std::env::temp_dir().join(name);
|
||||
std::fs::write(&path, body.as_bytes())
|
||||
.map_err(|e| format!("failed to write temp foreign script: {e}"))?;
|
||||
Ok(path)
|
||||
}
|
||||
|
||||
fn run_argv(
|
||||
ctx: &HostContext,
|
||||
argv: &[String],
|
||||
script: &Path,
|
||||
stdin: Option<&str>,
|
||||
lang: &str,
|
||||
) -> Result<Value, String> {
|
||||
if argv.is_empty() {
|
||||
return Err("foreign cmd argv is empty — check set_cmd / env".into());
|
||||
}
|
||||
let mut command = Command::new(&argv[0]);
|
||||
for a in &argv[1..] {
|
||||
command.arg(a);
|
||||
}
|
||||
command.arg(script);
|
||||
command.current_dir(&ctx.cwd);
|
||||
command.stdin(Stdio::piped());
|
||||
command.stdout(Stdio::piped());
|
||||
command.stderr(Stdio::piped());
|
||||
command.env("PYTHONIOENCODING", "utf-8");
|
||||
command.env("PYTHONUTF8", "1");
|
||||
|
||||
let mut child = command.spawn().map_err(|e| {
|
||||
format!(
|
||||
"foreign run failed (lang={lang}, cmd={}): {e} — check your interpreter / \
|
||||
MARQDO_FOREIGN_* / set_cmd",
|
||||
argv.join(" ")
|
||||
)
|
||||
})?;
|
||||
|
||||
if let Some(data) = stdin {
|
||||
if let Some(mut pipe) = child.stdin.take() {
|
||||
let _ = pipe.write_all(data.as_bytes());
|
||||
}
|
||||
} else {
|
||||
drop(child.stdin.take());
|
||||
}
|
||||
|
||||
let mut stdout_pipe = child.stdout.take();
|
||||
let mut stderr_pipe = child.stderr.take();
|
||||
let t_out = thread::spawn(move || {
|
||||
let mut buf = Vec::new();
|
||||
if let Some(ref mut r) = stdout_pipe {
|
||||
let _ = r.read_to_end(&mut buf);
|
||||
}
|
||||
String::from_utf8_lossy(&buf).into_owned()
|
||||
});
|
||||
let t_err = thread::spawn(move || {
|
||||
let mut buf = Vec::new();
|
||||
if let Some(ref mut r) = stderr_pipe {
|
||||
let _ = r.read_to_end(&mut buf);
|
||||
}
|
||||
String::from_utf8_lossy(&buf).into_owned()
|
||||
});
|
||||
|
||||
let start = Instant::now();
|
||||
let status = loop {
|
||||
match child.try_wait() {
|
||||
Ok(Some(st)) => break st,
|
||||
Ok(None) if start.elapsed() > DEFAULT_TIMEOUT => {
|
||||
let _ = child.kill();
|
||||
let _ = child.wait();
|
||||
return Err(format!(
|
||||
"foreign run timed out after {}s (lang={lang}, cmd={}) — check your script / environment",
|
||||
DEFAULT_TIMEOUT.as_secs(),
|
||||
argv.join(" ")
|
||||
));
|
||||
}
|
||||
Ok(None) => thread::sleep(Duration::from_millis(20)),
|
||||
Err(e) => {
|
||||
return Err(format!(
|
||||
"foreign run failed (lang={lang}, cmd={}): {e}",
|
||||
argv.join(" ")
|
||||
));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let stdout = t_out.join().unwrap_or_default();
|
||||
let stderr = t_err.join().unwrap_or_default();
|
||||
if stdout.len() > MAX_OUTPUT || stderr.len() > MAX_OUTPUT {
|
||||
return Err(format!(
|
||||
"foreign output too large (lang={lang}) — truncate script output"
|
||||
));
|
||||
}
|
||||
|
||||
if !status.success() {
|
||||
let code = status.code().unwrap_or(1);
|
||||
let mut msg = format!(
|
||||
"foreign run failed (lang={lang}, cmd={}, exit={code})",
|
||||
argv.join(" ")
|
||||
);
|
||||
if !stderr.trim().is_empty() {
|
||||
msg.push_str(": ");
|
||||
msg.push_str(stderr.trim());
|
||||
}
|
||||
msg.push_str(" — check your interpreter / MARQDO_FOREIGN_* / set_cmd / script");
|
||||
return Err(msg);
|
||||
}
|
||||
|
||||
let mut out = stdout;
|
||||
if out.ends_with('\n') {
|
||||
out.pop();
|
||||
if out.ends_with('\r') {
|
||||
out.pop();
|
||||
}
|
||||
}
|
||||
Ok(Value::Text(out))
|
||||
}
|
||||
|
||||
/// View/API helper: run with optional command override string.
|
||||
pub fn run_with_cmd_override(
|
||||
cwd: &Path,
|
||||
lang: &str,
|
||||
source: &str,
|
||||
cmd_override: Option<&str>,
|
||||
) -> Result<String, String> {
|
||||
let mut ctx = HostContext::default();
|
||||
ctx.cwd = cwd.to_path_buf();
|
||||
let argv = resolve_argv_with_override(&ctx, lang, cmd_override)?;
|
||||
let script_path = write_temp_script(&lang.to_ascii_lowercase(), source)?;
|
||||
let result = run_argv(&ctx, &argv, &script_path, None, lang);
|
||||
let _ = std::fs::remove_file(&script_path);
|
||||
match result {
|
||||
Ok(Value::Text(s)) => Ok(s),
|
||||
Ok(v) => Ok(v.as_display()),
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
@@ -108,5 +108,6 @@ fn value_to_json(v: &Value) -> Result<serde_json::Value, String> {
|
||||
serde_json::Value::Object(map)
|
||||
}
|
||||
Value::Formula(e) => serde_json::Value::String(e.as_display()),
|
||||
Value::Code(c) => serde_json::Value::String(c.source.clone()),
|
||||
})
|
||||
}
|
||||
@@ -5,6 +5,7 @@
|
||||
//! Soft side-effects (no process::exit; sleep clamp) apply under view / capture only.
|
||||
|
||||
mod dispatch;
|
||||
pub mod foreign;
|
||||
mod fs;
|
||||
mod json;
|
||||
pub mod math;
|
||||
@@ -15,6 +16,7 @@ mod time;
|
||||
pub use dispatch::{call_host, HostFn};
|
||||
pub use fs::path_under_root;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
/// Host capability flags. Default: all enabled.
|
||||
@@ -55,6 +57,8 @@ pub struct HostContext {
|
||||
/// LCG state for `random` / `random_int`.
|
||||
pub rng: u64,
|
||||
pub plots: Vec<PlotArtifact>,
|
||||
/// lang → interpreter argv (from `set_cmd`).
|
||||
pub foreign_cmds: HashMap<String, Vec<String>>,
|
||||
}
|
||||
|
||||
impl Default for HostContext {
|
||||
@@ -68,6 +72,7 @@ impl Default for HostContext {
|
||||
sleep_limit_ms: Some(30_000),
|
||||
rng: 0xC0FF_EE42_CAFE_BABE,
|
||||
plots: Vec::new(),
|
||||
foreign_cmds: HashMap::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -395,6 +395,7 @@ impl Interpreter {
|
||||
Ok(Value::List(out))
|
||||
}
|
||||
Expr::Formula(e) => Ok(Value::Formula(e.clone())),
|
||||
Expr::Code(c) => Ok(Value::Code(c.clone())),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -73,6 +73,7 @@ pub fn classify_source(source: &str) -> Vec<ClassifiedLine> {
|
||||
let mut out = Vec::with_capacity(lines.len());
|
||||
let mut in_comment_paragraph = false;
|
||||
let mut in_math_fence = false;
|
||||
let mut in_md_fence = false;
|
||||
|
||||
for (i, text) in lines.into_iter().enumerate() {
|
||||
let base = classify_line(text);
|
||||
@@ -83,6 +84,16 @@ pub fn classify_source(source: &str) -> Vec<ClassifiedLine> {
|
||||
in_math_fence = false;
|
||||
}
|
||||
LineKind::Code
|
||||
} else if in_md_fence {
|
||||
// Markdown ``` fences are narrative / foreign sources — not Marqdo stmts.
|
||||
if is_md_fence_line(trimmed) {
|
||||
in_md_fence = false;
|
||||
}
|
||||
LineKind::Comment
|
||||
} else if is_md_fence_opener(trimmed) {
|
||||
in_md_fence = true;
|
||||
in_comment_paragraph = false;
|
||||
LineKind::Comment
|
||||
} else {
|
||||
match base {
|
||||
LineKind::Blank => {
|
||||
@@ -116,6 +127,18 @@ pub fn classify_source(source: &str) -> Vec<ClassifiedLine> {
|
||||
out
|
||||
}
|
||||
|
||||
fn is_md_fence_opener(trimmed: &str) -> bool {
|
||||
trimmed.starts_with("```")
|
||||
&& !trimmed
|
||||
.chars()
|
||||
.skip_while(|c| *c == '`')
|
||||
.all(|c| c.is_whitespace())
|
||||
}
|
||||
|
||||
fn is_md_fence_line(trimmed: &str) -> bool {
|
||||
trimmed.starts_with("```")
|
||||
}
|
||||
|
||||
/// Function-end / frontmatter HR or empty bold return — never swallowed by paragraphs.
|
||||
fn is_structural_code_line(trimmed: &str) -> bool {
|
||||
if trimmed.len() >= 3
|
||||
|
||||
@@ -11,6 +11,7 @@ pub mod capture;
|
||||
pub mod catalog;
|
||||
pub mod debug;
|
||||
pub mod diagnostics;
|
||||
pub mod foreign;
|
||||
pub mod formula;
|
||||
pub mod host;
|
||||
pub mod input_feed;
|
||||
|
||||
+66
-1
@@ -288,7 +288,7 @@ impl<'a> Cursor<'a> {
|
||||
}))
|
||||
}
|
||||
|
||||
/// RHS on the same line, or empty RHS → following `$$` fence or table.
|
||||
/// RHS on the same line, or empty RHS → following `$$` / ```lang fence or table.
|
||||
/// Returns `(expr, inclusive end_line)`.
|
||||
fn resolve_assign_rhs(&mut self, rhs: &str, span: Span) -> Result<(Expr, u32)> {
|
||||
let rhs = rhs.trim();
|
||||
@@ -297,6 +297,9 @@ impl<'a> Cursor<'a> {
|
||||
if self.peek_is_formula_fence() {
|
||||
return self.consume_formula_fence(span);
|
||||
}
|
||||
if self.peek_is_code_fence() {
|
||||
return self.consume_code_fence(span);
|
||||
}
|
||||
let start_i = self.i;
|
||||
let list = self.consume_table()?;
|
||||
let end_line = if self.i > start_i {
|
||||
@@ -334,6 +337,50 @@ impl<'a> Cursor<'a> {
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
fn peek_is_code_fence(&self) -> bool {
|
||||
self.peek()
|
||||
.map(|l| crate::foreign::is_fence_opener(l.text.trim()))
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
fn consume_code_fence(&mut self, span: Span) -> Result<(Expr, u32)> {
|
||||
let open = self
|
||||
.bump()
|
||||
.ok_or_else(|| anyhow::anyhow!("{span}: expected ```lang code fence"))?;
|
||||
let open_line = open.line_no;
|
||||
let trimmed = open.text.trim();
|
||||
let lang = crate::foreign::fence_lang(trimmed).ok_or_else(|| {
|
||||
anyhow::anyhow!("{open_line}:1: expected ```lang fence after empty assignment")
|
||||
})?;
|
||||
if trimmed.contains("name=") {
|
||||
bail!(
|
||||
"{open_line}:1: `name=` on fences was removed; \
|
||||
use `name` = then a ```{lang} fence (same shape as formulas)"
|
||||
);
|
||||
}
|
||||
let mut body_lines: Vec<String> = Vec::new();
|
||||
let mut closed = false;
|
||||
let mut end_line = open_line;
|
||||
while let Some(l) = self.peek() {
|
||||
if crate::foreign::is_fence_closer(l.text.trim()) {
|
||||
end_line = l.line_no;
|
||||
self.bump();
|
||||
closed = true;
|
||||
break;
|
||||
}
|
||||
body_lines.push(l.text.clone());
|
||||
self.bump();
|
||||
}
|
||||
if !closed {
|
||||
bail!("{open_line}:1: unclosed code fence ```{lang}");
|
||||
}
|
||||
let source = body_lines.join("\n");
|
||||
Ok((
|
||||
Expr::Code(crate::value::CodeBlock { lang, source }),
|
||||
end_line,
|
||||
))
|
||||
}
|
||||
|
||||
fn consume_formula_fence(&mut self, span: Span) -> Result<(Expr, u32)> {
|
||||
let open = self
|
||||
.bump()
|
||||
@@ -725,4 +772,22 @@ mod tests {
|
||||
other => panic!("expected formula assign, got {other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_code_assign_fence() {
|
||||
let src = "# main\n\n`hi` =\n```python\nprint(1)\n```\n\n> print text=ok\n";
|
||||
let m = parse_source(src).unwrap();
|
||||
match &m.functions[0].body[0] {
|
||||
Stmt::Assign {
|
||||
name,
|
||||
value: Expr::Code(c),
|
||||
..
|
||||
} => {
|
||||
assert_eq!(name, "hi");
|
||||
assert_eq!(c.lang, "python");
|
||||
assert_eq!(c.source, "print(1)");
|
||||
}
|
||||
other => panic!("expected code assign, got {other:?}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,13 @@ use std::fmt;
|
||||
|
||||
use crate::formula::Expr as FormulaExpr;
|
||||
|
||||
/// Bound Markdown code fence (`type` → `code`).
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct CodeBlock {
|
||||
pub lang: String,
|
||||
pub source: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum Value {
|
||||
None,
|
||||
@@ -18,6 +25,8 @@ pub enum Value {
|
||||
Map(Vec<(String, Value)>),
|
||||
/// Parsed symbolic expression (`type` → `formula`).
|
||||
Formula(FormulaExpr),
|
||||
/// External language source from ```lang fence (`type` → `code`).
|
||||
Code(CodeBlock),
|
||||
}
|
||||
|
||||
impl Value {
|
||||
@@ -31,6 +40,7 @@ impl Value {
|
||||
Value::List(xs) => !xs.is_empty(),
|
||||
Value::Map(xs) => !xs.is_empty(),
|
||||
Value::Formula(_) => true,
|
||||
Value::Code(c) => !c.source.is_empty(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,6 +58,7 @@ impl Value {
|
||||
}
|
||||
Value::Map(_) => "<map>".into(),
|
||||
Value::Formula(e) => e.as_display(),
|
||||
Value::Code(c) => format!("```{}\n{}\n```", c.lang, c.source),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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