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
@@ -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"))?;
|
||||
|
||||
Reference in new issue
Block a user