feat(foreign): pass Marqdo values into multi-kernel view sessions
foreign.session accepts a values table and initializes each kernel from ordinary Marqdo values. The static walkthrough now uses two kernels and a shared 订单 value instead of redefining orders inside Python. Static pages embed the session summary so kernel controls and Clean stay visible.
This commit is contained in:
1 parent
15362cb048
commit
9b269f6447
7 files changed
+164
-61
No files matched your search
+37
-7
@@ -165,14 +165,21 @@ fn table_rows(v: &Value) -> Result<Vec<HashMap<String, Value>>, String> {
|
||||
|
||||
fn plan_tables(
|
||||
bindings: &HashMap<String, Value>,
|
||||
) -> Result<(Option<Vec<HashMap<String, Value>>>, Option<Vec<HashMap<String, Value>>>), String> {
|
||||
) -> Result<
|
||||
(
|
||||
Option<Vec<HashMap<String, Value>>>,
|
||||
Option<Vec<HashMap<String, Value>>>,
|
||||
Option<Vec<HashMap<String, Value>>>,
|
||||
),
|
||||
String,
|
||||
> {
|
||||
let Some(plan) = bindings.get("会话") else {
|
||||
return Ok((None, None));
|
||||
return Ok((None, None, None));
|
||||
};
|
||||
let Value::Map(fields) = plan else {
|
||||
// `会话` is also a common low-level session id binding (e.g. from
|
||||
// foreign.session_open). It is not a view plan; leave cells stateless.
|
||||
return Ok((None, None));
|
||||
return Ok((None, None, None));
|
||||
};
|
||||
let field = |name: &str| {
|
||||
fields
|
||||
@@ -192,7 +199,11 @@ fn plan_tables(
|
||||
Some(Value::None) | None => None,
|
||||
Some(v) => Some(table_rows(&v)?),
|
||||
};
|
||||
Ok((kernels, cells))
|
||||
let values = match field("values") {
|
||||
Some(Value::None) | None => None,
|
||||
Some(v) => Some(table_rows(&v)?),
|
||||
};
|
||||
Ok((kernels, cells, values))
|
||||
}
|
||||
|
||||
fn col_value<'a>(row: &'a HashMap<String, Value>, names: &[&str]) -> Option<&'a Value> {
|
||||
@@ -311,7 +322,7 @@ impl DocumentSession {
|
||||
collect_cells(&entry.body, &mut cells, &mut seen)?;
|
||||
let declarations = capture_bindings(path, root, &module, &source)?;
|
||||
|
||||
let (plan_kernels, plan_cells) = plan_tables(&declarations)?;
|
||||
let (plan_kernels, plan_cells, plan_values) = plan_tables(&declarations)?;
|
||||
let persistent = matches!(
|
||||
declarations.get("会话"),
|
||||
Some(Value::Map(fields))
|
||||
@@ -411,7 +422,7 @@ impl DocumentSession {
|
||||
host.entry_path = Some(path.to_path_buf());
|
||||
host.entry_source = Some(source);
|
||||
|
||||
Ok(Self {
|
||||
let mut session = Self {
|
||||
path: path.to_path_buf(),
|
||||
root: root.to_path_buf(),
|
||||
persistent,
|
||||
@@ -425,7 +436,26 @@ impl DocumentSession {
|
||||
host,
|
||||
kernel_sessions: HashMap::new(),
|
||||
seq: 0,
|
||||
})
|
||||
};
|
||||
if let Some(rows) = plan_values {
|
||||
for row in rows {
|
||||
let kernel = col_text(&row, &["内核", "Kernel", "kernel"])
|
||||
.ok_or_else(|| "foreign.session values: missing 内核/Kernel".to_string())?;
|
||||
let name = col_text(&row, &["名称", "Name", "name"])
|
||||
.ok_or_else(|| "foreign.session values: missing 名称/Name".to_string())?;
|
||||
let value = col_value(&row, &["值", "Value", "value", "数据"])
|
||||
.ok_or_else(|| format!("foreign.session values[{name}]: missing 值/Value"))?
|
||||
.clone();
|
||||
let id = session.ensure_kernel_session(&kernel)?;
|
||||
foreign_session::set(
|
||||
&mut session.host,
|
||||
&Value::Text(id),
|
||||
&Value::Text(name),
|
||||
&value,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
Ok(session)
|
||||
}
|
||||
|
||||
/// Re-read the document, preserving kernels/state/journal. Changed cells become dirty.
|
||||
|
||||
+36
-4
@@ -3692,6 +3692,34 @@ body.notebook-page .stdin-form button {
|
||||
});
|
||||
});
|
||||
}
|
||||
function renderStaticSummary(s) {
|
||||
if (!s) return;
|
||||
state.persistent = !!s.persistent;
|
||||
state.kernels = s.kernels || [];
|
||||
state.cells = {};
|
||||
(s.cells || []).forEach(function (c) { state.cells[c.name] = c; });
|
||||
(s.cells || []).forEach(function (c) {
|
||||
var el = card(c.name);
|
||||
if (!el) return;
|
||||
badge(el, c.status, c.status === "clean");
|
||||
if (!state.persistent) {
|
||||
ensureKernel(el, false);
|
||||
return;
|
||||
}
|
||||
var sel = ensureKernel(el, true);
|
||||
var opts = state.kernels.filter(function (k) { return k.lang === c.lang; }).map(function (k) { return k.name; });
|
||||
if (!opts.length) opts = [c.kernel];
|
||||
sel.innerHTML = "";
|
||||
opts.forEach(function (n) {
|
||||
var o = document.createElement("option");
|
||||
o.value = n; o.textContent = n;
|
||||
if (n === c.kernel) o.selected = true;
|
||||
sel.appendChild(o);
|
||||
});
|
||||
sel.disabled = true;
|
||||
sel.title = "Static export: kernel fixed to " + c.kernel;
|
||||
});
|
||||
}
|
||||
function openSession() {
|
||||
var bar = document.getElementById("notebook-bar");
|
||||
if (bar) bar.hidden = false;
|
||||
@@ -3804,10 +3832,14 @@ body.notebook-page .stdin-form button {
|
||||
staticNotice();
|
||||
});
|
||||
});
|
||||
document.querySelectorAll(".code-card").forEach(function (el) {
|
||||
ensureKernel(el, false);
|
||||
bindClean(el);
|
||||
});
|
||||
if (window.MARQDO_STATIC_SESSION) {
|
||||
renderStaticSummary(window.MARQDO_STATIC_SESSION);
|
||||
} else {
|
||||
document.querySelectorAll(".code-card").forEach(function (el) {
|
||||
ensureKernel(el, false);
|
||||
});
|
||||
}
|
||||
document.querySelectorAll(".code-card").forEach(bindClean);
|
||||
status("static export · preview only");
|
||||
return;
|
||||
}
|
||||
|
||||
+41
-18
@@ -50,18 +50,18 @@ fn record_field(record: &Value, key: &str) -> Option<Value> {
|
||||
|
||||
/// Run the document's cells at export time and keep their real outputs. This
|
||||
/// is what makes a static page show foreign results instead of just source.
|
||||
fn static_cell_outputs(
|
||||
fn static_page_data(
|
||||
root: &ViewRoot,
|
||||
abs: &std::path::Path,
|
||||
stdin: &str,
|
||||
) -> HashMap<String, StaticCellOutput> {
|
||||
) -> (HashMap<String, StaticCellOutput>, Option<Value>) {
|
||||
let mut out = HashMap::new();
|
||||
let Ok(mut session) = DocumentSession::open(abs, &root.root) else {
|
||||
return out;
|
||||
return (out, None);
|
||||
};
|
||||
let Ok(summary) = session.run_all_with_stdin(false, Some(stdin)) else {
|
||||
session.close();
|
||||
return out;
|
||||
return (out, None);
|
||||
};
|
||||
let records = match &summary {
|
||||
Value::Map(entries) => entries
|
||||
@@ -123,8 +123,9 @@ fn static_cell_outputs(
|
||||
out.insert(cell, StaticCellOutput { ok, text });
|
||||
}
|
||||
}
|
||||
let view_summary = session.summary();
|
||||
session.close();
|
||||
out
|
||||
(out, Some(view_summary))
|
||||
}
|
||||
|
||||
/// Inject static cell outputs into code cards without running client JS.
|
||||
@@ -153,6 +154,22 @@ fn inject_cell_outputs(html: &str, outputs: &HashMap<String, StaticCellOutput>)
|
||||
html
|
||||
}
|
||||
|
||||
fn inject_static_session(html: &str, summary: Option<&Value>) -> String {
|
||||
let Some(summary) = summary else {
|
||||
return html.to_string();
|
||||
};
|
||||
let Ok(json) = crate::host::json::value_to_json(summary) else {
|
||||
return html.to_string();
|
||||
};
|
||||
let Ok(json) = serde_json::to_string(&json) else {
|
||||
return html.to_string();
|
||||
};
|
||||
let line = format!(
|
||||
"window.MARQDO_VIEW_LIVE = false;\nwindow.MARQDO_STATIC_SESSION = {json};"
|
||||
);
|
||||
html.replace("window.MARQDO_VIEW_LIVE = false;", &line)
|
||||
}
|
||||
|
||||
pub fn write_static(opts: OutputOptions) -> Result<()> {
|
||||
let root = build_root(&opts.path)?;
|
||||
fs::create_dir_all(&opts.out_dir)
|
||||
@@ -187,14 +204,17 @@ pub fn write_static(opts: OutputOptions) -> Result<()> {
|
||||
let links = LinkMode::Static {
|
||||
from: Some(rel.clone()),
|
||||
};
|
||||
let cell_outputs = if opts.no_exec {
|
||||
HashMap::new()
|
||||
let (cell_outputs, static_summary) = if opts.no_exec {
|
||||
(HashMap::new(), None)
|
||||
} else {
|
||||
static_cell_outputs(&root, &abs, &vm.preset_stdin)
|
||||
static_page_data(&root, &abs, &vm.preset_stdin)
|
||||
};
|
||||
let html = inject_cell_outputs(
|
||||
&page_file(&root.files, &rel, &vm, &links),
|
||||
&cell_outputs,
|
||||
let html = inject_static_session(
|
||||
&inject_cell_outputs(
|
||||
&page_file(&root.files, &rel, &vm, &links),
|
||||
&cell_outputs,
|
||||
),
|
||||
static_summary.as_ref(),
|
||||
);
|
||||
let out_file = pages_root.join(format!("{rel}.html"));
|
||||
if let Some(parent) = out_file.parent() {
|
||||
@@ -204,14 +224,17 @@ pub fn write_static(opts: OutputOptions) -> Result<()> {
|
||||
|
||||
if first_page_html.is_none() {
|
||||
// index.html = first file (same content, links rooted at OUT_DIR)
|
||||
first_page_html = Some(inject_cell_outputs(
|
||||
&page_file(
|
||||
&root.files,
|
||||
&rel,
|
||||
&vm,
|
||||
&LinkMode::Static { from: None },
|
||||
first_page_html = Some(inject_static_session(
|
||||
&inject_cell_outputs(
|
||||
&page_file(
|
||||
&root.files,
|
||||
&rel,
|
||||
&vm,
|
||||
&LinkMode::Static { from: None },
|
||||
),
|
||||
&cell_outputs,
|
||||
),
|
||||
&cell_outputs,
|
||||
static_summary.as_ref(),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user