Release 2.7.0: executable handbooks and native user-site workflows

This commit is contained in:
cflmy committed 2026-11-10 17:03:06 +08:00
1 parent aa0c4d0f55
commit d9aef957f7
562 files changed
+21064 -20387

No files matched your search

+97
View File
@@ -157,6 +157,46 @@ fn tables(text: &str) -> Vec<Table> {
}
out
}
fn literal_value(expression: crate::ast::Expr) -> Result<Value, String> {
use crate::ast::{Expr, Literal};
match expression {
Expr::Literal(literal) => Ok(match literal {
Literal::None => Value::None,
Literal::Bool(value) => Value::Bool(value),
Literal::Int(value) => Value::Int(value),
Literal::Num(value) => Value::Num(value),
Literal::Text(value) => Value::Text(value),
}),
Expr::Unary { op: crate::ast::UnaryOp::Neg, expr } => match literal_value(*expr)? {
Value::Int(value) => value.checked_neg().map(Value::Int).ok_or_else(|| "negative table literal overflows an integer".into()),
Value::Num(value) => Ok(Value::Num(-value)),
_ => Err("negative table literal requires a number".into()),
},
Expr::List(values) => values.into_iter().map(literal_value)
.collect::<Result<Vec<_>, _>>().map(Value::List),
Expr::Map(fields) => fields.into_iter()
.map(|(key, value)| literal_value(value).map(|value| (key, value)))
.collect::<Result<Vec<_>, _>>().map(Value::Map),
_ => Err("document.table accepts literal data cells; variable references, interpolation and calls are not evaluated".into()),
}
}
fn table_value(source: &str, title: Option<&str>, index: usize) -> Result<Value, String> {
let section_list;
let selected = if let Some(title) = title {
section_list = sections(source);
let section = unique_section(&section_list, title)?;
&source[section.body..section.end]
} else {
source
};
let found = tables(selected);
let table = found.get(index).ok_or_else(|| format!(
"document.table index {index} is out of range ({} tables)", found.len()))?;
let expression = crate::parse::parse_gfm_table(&selected[table.start..table.end])
.map_err(|error| format!("document.table: {error:#}"))?;
literal_value(expression)
}
fn check_digest(b: &Args, text: &str) -> Result<(), String> {
if let Some(Value::Text(expected)) = b.get("expected_sha256") {
if format!("{:x}", Sha256::digest(text.as_bytes())) != *expected {
@@ -232,6 +272,19 @@ pub(super) fn call(_ctx: &mut HostContext, id: HostFn, b: &Args) -> Result<Value
})
.collect(),
)),
HostFn::DocumentTable => {
let title = match b.get("title") {
None | Some(Value::None) => None,
Some(value) => Some(as_text(value, "title")?),
};
let index = match b.get("index") {
None | Some(Value::None) => 0,
Some(Value::Int(value)) if *value >= 0 =>
usize::try_from(*value).map_err(|_| "table index too large")?,
_ => return Err("table index must be a zero-based nonnegative integer".into()),
};
table_value(source, title, index)
}
HostFn::DocumentReplaceTable => {
let change = (|| {
check_digest(b, source)?;
@@ -356,3 +409,47 @@ pub(super) fn call(_ctx: &mut HostContext, id: HostFn, b: &Args) -> Result<Value
_ => Err("not a document operation".into()),
}
}
#[cfg(test)]
mod literal_table_tests {
use super::*;
#[test]
fn literal_values_retain_native_record_column_and_list_shapes() {
let source="# 数据\n\n| @ | name | enabled | count |\n|---|---|---|---|\n| 1 | 中国 | True | 3 |\n";
let actual=table_value(source,Some("数据"),0).unwrap();
assert_eq!(actual,Value::List(vec![Value::Map(vec![
("name".into(),Value::Text("中国".into())),
("enabled".into(),Value::Bool(true)),
("count".into(),Value::Int(3)),
])]));
assert_eq!(table_value("| number |\n|---|\n| 1 |\n| 2 |\n",None,0).unwrap(),Value::List(vec![Value::Int(1),Value::Int(2)]));
let columns=table_value("| key | value |\n|---|---|\n| a | 1 |\n| b | 2 |\n",None,0).unwrap();
assert!(matches!(columns,Value::Map(fields) if fields.iter().all(|(_, value)|matches!(value,Value::List(_)))));
}
#[test]
fn negative_numbers_none_and_false_keep_their_literal_types() {
let source="| values |\n|---|\n| -3 |\n| -0.5 |\n| None |\n| False |\n";
assert_eq!(table_value(source,None,0).unwrap(),Value::List(vec![
Value::Int(-3),Value::Num(-0.5),Value::None,Value::Bool(false),
]));
}
#[test]
fn unique_sections_and_indices_skip_fenced_examples() {
let source="```text\n| code |\n|---|\n| hidden |\n```\n\n# first\n\n| x | y |\n|---|---|\n| 1 | 2 |\n\n# second\n\n| x | y |\n|---|---|\n| 3 | 4 |\n";
let second=Value::Map(vec![("x".into(),Value::Int(3)),("y".into(),Value::Int(4))]);
assert_eq!(table_value(source,Some("second"),0).unwrap(),second);
assert_eq!(table_value(source,None,1).unwrap(),second);
assert!(table_value("# same\n\n# same\n",Some("same"),0).unwrap_err().contains("ambiguous"));
assert!(table_value(source,None,2).unwrap_err().contains("out of range"));
}
#[test]
fn rejects_dynamic_cells_duplicate_headers_and_silent_truncation() {
for source in ["| value |\n|---|\n| `outside` |\n","| x | x |\n|---|---|\n| a | b |\n","| x | y |\n|---|---|\n| a | b | extra |\n"] {
assert!(table_value(source,None,0).is_err(),"accepted {source}");
}
let escaped="| @ | text | flag |\n|---|---|---|\n| 1 | left\\|right | False |\n";
assert_eq!(table_value(escaped,None,0).unwrap(),Value::List(vec![Value::Map(vec![
("text".into(),Value::Text("left|right".into())),("flag".into(),Value::Bool(false)),
])]));
}
}