feat(view): render real cell outputs in static exports
Static export opens the foreign document session, runs cells once, and injects stdout/stderr/error into the code cards. Failures are shown as real errors; --no-exec still skips execution.
This commit is contained in:
1 parent
ad3ebf243a
commit
0ba4d3d2d7
4 files changed
+169
-10
No files matched your search
+168
-7
@@ -1,11 +1,14 @@
|
||||
//! Static HTML export: `marqdo view output`.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
|
||||
use crate::view::html::{page_file, page_index, LinkMode};
|
||||
use crate::host::foreign_doc::DocumentSession;
|
||||
use crate::value::Value;
|
||||
use crate::view::html::{escape, page_file, page_index, LinkMode};
|
||||
use crate::view::{build_file_view, build_root, ViewRoot};
|
||||
|
||||
pub struct OutputOptions {
|
||||
@@ -14,6 +17,138 @@ pub struct OutputOptions {
|
||||
pub no_exec: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct StaticCellOutput {
|
||||
ok: bool,
|
||||
text: String,
|
||||
}
|
||||
|
||||
fn value_text(v: &Value) -> Option<String> {
|
||||
match v {
|
||||
Value::Text(s) => Some(s.clone()),
|
||||
Value::None => None,
|
||||
_ => v.reveal_text().map(|s| s.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
fn value_bool(v: &Value) -> Option<bool> {
|
||||
match v {
|
||||
Value::Bool(b) => Some(*b),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn record_field(record: &Value, key: &str) -> Option<Value> {
|
||||
match record {
|
||||
Value::Map(entries) => entries
|
||||
.iter()
|
||||
.find(|(k, _)| k == key)
|
||||
.map(|(_, v)| v.clone()),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// 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(root: &ViewRoot, abs: &std::path::Path) -> HashMap<String, StaticCellOutput> {
|
||||
let mut out = HashMap::new();
|
||||
let Ok(mut session) = DocumentSession::open(abs, &root.root) else {
|
||||
return out;
|
||||
};
|
||||
for cell in session.order.clone() {
|
||||
let record = match session.run_cell(&cell, None) {
|
||||
Ok(record) => record,
|
||||
Err(error) => {
|
||||
out.insert(
|
||||
cell,
|
||||
StaticCellOutput {
|
||||
ok: false,
|
||||
text: error,
|
||||
},
|
||||
);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
let ok = record_field(&record, "ok")
|
||||
.and_then(|v| value_bool(&v))
|
||||
.unwrap_or(false);
|
||||
let mut parts = Vec::new();
|
||||
if let Some(stdout) = record_field(&record, "stdout").and_then(|v| value_text(&v)) {
|
||||
if !stdout.is_empty() {
|
||||
parts.push(stdout);
|
||||
}
|
||||
}
|
||||
if let Some(stderr) = record_field(&record, "stderr").and_then(|v| value_text(&v)) {
|
||||
if !stderr.is_empty() {
|
||||
parts.push(format!("[stderr]\n{stderr}"));
|
||||
}
|
||||
}
|
||||
if let Some(error) = record_field(&record, "error").and_then(|v| value_text(&v)) {
|
||||
if !error.is_empty() {
|
||||
parts.push(error);
|
||||
}
|
||||
}
|
||||
let mut meta = Vec::new();
|
||||
if let Some(code) =
|
||||
record_field(&record, "code").and_then(|v| match v {
|
||||
Value::Int(n) => Some(n),
|
||||
_ => None,
|
||||
})
|
||||
{
|
||||
if code != 0 {
|
||||
meta.push(format!("exit {code}"));
|
||||
}
|
||||
}
|
||||
if let Some(ms) =
|
||||
record_field(&record, "ms").and_then(|v| match v {
|
||||
Value::Int(n) => Some(n),
|
||||
_ => None,
|
||||
})
|
||||
{
|
||||
if ms > 0 {
|
||||
meta.push(format!("{ms}ms"));
|
||||
}
|
||||
}
|
||||
if !meta.is_empty() {
|
||||
parts.push(format!("— {}", meta.join(" · ")));
|
||||
}
|
||||
let text = if parts.is_empty() {
|
||||
"(no output)".to_string()
|
||||
} else {
|
||||
parts.join("\n\n")
|
||||
};
|
||||
out.insert(cell, StaticCellOutput { ok, text });
|
||||
}
|
||||
session.close();
|
||||
out
|
||||
}
|
||||
|
||||
/// Inject static cell outputs into code cards without running client JS.
|
||||
fn inject_cell_outputs(html: &str, outputs: &HashMap<String, StaticCellOutput>) -> String {
|
||||
let mut html = html.to_string();
|
||||
for (name, out) in outputs {
|
||||
let marker = format!(r#"data-code-name="{name}""#);
|
||||
let Some(pos) = html.find(&marker) else {
|
||||
continue;
|
||||
};
|
||||
let tail = &html[pos..];
|
||||
let Some(out_pos) = tail.find(r#"<pre class="code-out" hidden></pre>"#) else {
|
||||
continue;
|
||||
};
|
||||
let abs_pos = pos + out_pos;
|
||||
let replacement = if out.ok {
|
||||
format!(r#"<pre class="code-out">{}</pre>"#, escape(&out.text))
|
||||
} else {
|
||||
format!(r#"<pre class="code-out err">{}</pre>"#, escape(&out.text))
|
||||
};
|
||||
html.replace_range(
|
||||
abs_pos..abs_pos + r#"<pre class="code-out" hidden></pre>"#.len(),
|
||||
&replacement,
|
||||
);
|
||||
}
|
||||
html
|
||||
}
|
||||
|
||||
pub fn write_static(opts: OutputOptions) -> Result<()> {
|
||||
let root = build_root(&opts.path)?;
|
||||
fs::create_dir_all(&opts.out_dir)
|
||||
@@ -48,7 +183,15 @@ pub fn write_static(opts: OutputOptions) -> Result<()> {
|
||||
let links = LinkMode::Static {
|
||||
from: Some(rel.clone()),
|
||||
};
|
||||
let html = page_file(&root.files, &rel, &vm, &links);
|
||||
let cell_outputs = if opts.no_exec {
|
||||
HashMap::new()
|
||||
} else {
|
||||
static_cell_outputs(&root, &abs)
|
||||
};
|
||||
let html = inject_cell_outputs(
|
||||
&page_file(&root.files, &rel, &vm, &links),
|
||||
&cell_outputs,
|
||||
);
|
||||
let out_file = pages_root.join(format!("{rel}.html"));
|
||||
if let Some(parent) = out_file.parent() {
|
||||
fs::create_dir_all(parent)?;
|
||||
@@ -57,11 +200,14 @@ 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(page_file(
|
||||
&root.files,
|
||||
&rel,
|
||||
&vm,
|
||||
&LinkMode::Static { from: None },
|
||||
first_page_html = Some(inject_cell_outputs(
|
||||
&page_file(
|
||||
&root.files,
|
||||
&rel,
|
||||
&vm,
|
||||
&LinkMode::Static { from: None },
|
||||
),
|
||||
&cell_outputs,
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -85,3 +231,18 @@ fn resolve_abs(root: &ViewRoot, rel: &str) -> Result<PathBuf> {
|
||||
let abs = root.root.join(rel);
|
||||
Ok(abs.canonicalize().unwrap_or(abs))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn injects_visible_static_cell_outputs() {
|
||||
let html = r#"<div class="card" data-code-name="a"></div><pre class="code-out" hidden></pre>"#;
|
||||
let mut outputs = HashMap::new();
|
||||
outputs.insert("a".to_string(), StaticCellOutput { ok: true, text: "42".into() });
|
||||
let html = inject_cell_outputs(html, &outputs);
|
||||
assert!(html.contains(r#"<pre class="code-out">42</pre>"#), "{html}");
|
||||
assert!(!html.contains("hidden"), "{html}");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user