Make Web an executable handbook with recursive Markdown layouts

This commit is contained in:
cflmy committed 2026-11-08 21:50:01 +08:00
1 parent cc5277d2b5
commit 5080136d65
84 files changed
+3394 -957

No files matched your search

+46
View File
@@ -84,6 +84,52 @@ fn meta_get<'a>(metadata: &'a [(String, Value)], key: &str) -> Option<&'a Value>
metadata.iter().find(|(k, _)| k == key).map(|(_, v)| v)
}
/// A Web/Endpoint declaration without a runnable local entry has no executable
/// body. Both backends return the same summary instead of inventing bytecode.
pub fn declaration_summary(module: &crate::ast::Module) -> Option<Value> {
if module.functions.iter().any(|f| f.name == "main")
|| module
.functions
.iter()
.filter(|f| f.is_object() && f.params.is_empty())
.count()
== 1
{
return None;
}
let kind = classify(&module.metadata)?;
let kind_name = match kind {
ArtifactKind::Web => "web",
ArtifactKind::Endpoint => "endpoint",
_ => return None,
};
let mut fields = vec![
("kind".into(), Value::Text(kind_name.into())),
("method".into(), Value::Text(http_method(&module.metadata))),
];
let target = if kind == ArtifactKind::Web {
web_route(&module.metadata)
} else {
endpoint_path(&module.metadata)
};
if let Some(target) = target {
fields.push((
if kind == ArtifactKind::Web {
"route"
} else {
"path"
}
.into(),
Value::Text(target),
));
}
Some(Value::Map(vec![
("ok".into(), Value::Bool(true)),
("value".into(), Value::Map(fields)),
("error".into(), Value::None),
]))
}
#[cfg(test)]
mod tests {
use super::*;
+13 -6
View File
@@ -42,14 +42,21 @@ pub const CATALOG: &[ExtPackage] = &[
"web/index.mq.md",
"web/web.mq.md",
"web/网页.mq.md",
"web/_capability.mq.md",
"web/page.mq.md",
"web/route.mq.md",
"web/component.mq.md",
"web/dom.mq.md",
"web/client.mq.md",
"web/_capability/index.mq.md",
"web/page/index.mq.md",
"web/route/index.mq.md",
"web/component/index.mq.md",
"web/dom/index.mq.md",
"web/client/index.mq.md",
"web/layout/index.mq.md",
"web/布局/索引.mq.md",
"web/网页/索引.mq.md",
"web/网页/页面/索引.mq.md",
"web/网页/路由/索引.mq.md",
"web/网页/组件/索引.mq.md",
"web/网页/文档对象模型/索引.mq.md",
"web/网页/客户端/索引.mq.md",
"web/网页/能力/索引.mq.md",
],
native_crate: Some("marqdo_plugin_web"),
},
+2 -37
View File
@@ -622,11 +622,8 @@ impl Interpreter {
}
// Web Artifact entry (ADR 0007): `type: web|endpoint` may be pure document —
// no `# main`. Return a Result-shaped summary for run/check gold paths.
if let Some(kind) = crate::artifact::classify(&module.metadata) {
use crate::artifact::ArtifactKind;
if matches!(kind, ArtifactKind::Web | ArtifactKind::Endpoint) {
return Ok(Self::artifact_run_summary(module, kind));
}
if let Some(value) = crate::artifact::declaration_summary(module) {
return Ok(value);
}
bail!("no `# main` object to run");
})();
@@ -635,38 +632,6 @@ impl Interpreter {
}
fn artifact_run_summary(module: &Module, kind: crate::artifact::ArtifactKind) -> Value {
use crate::artifact::{endpoint_path, http_method, web_route, ArtifactKind};
let kind_s = match kind {
ArtifactKind::Web => "web",
ArtifactKind::Endpoint => "endpoint",
ArtifactKind::Prompt => "prompt",
ArtifactKind::Other => "other",
};
let mut value = vec![
("kind".into(), Value::Text(kind_s.into())),
("method".into(), Value::Text(http_method(&module.metadata))),
];
match kind {
ArtifactKind::Web => {
if let Some(r) = web_route(&module.metadata) {
value.push(("route".into(), Value::Text(r)));
}
}
ArtifactKind::Endpoint => {
if let Some(r) = endpoint_path(&module.metadata) {
value.push(("path".into(), Value::Text(r)));
}
}
_ => {}
}
Value::Map(vec![
("ok".into(), Value::Bool(true)),
("value".into(), Value::Map(value)),
("error".into(), Value::None),
])
}
/// Pin `module` as site and call `lib.member` (isolated worker / CLI `--call-lib-path`).
pub fn run_site_lib_path(
&mut self,
+27
View File
@@ -235,6 +235,13 @@ pub fn run_file(path: &Path, opts: &RunOptions) -> Result<i32> {
println!("=== marqdo: end sema ===");
}
if opts.call_lib_path.is_none() {
if let Some(value) = crate::artifact::declaration_summary(&module) {
emit_result_file(opts, &value)?;
return Ok(0);
}
}
match opts.backend {
Backend::Tree => {
let mut host = HostContext::for_run(Some(path), opts.host_caps(), opts.argv.clone());
@@ -400,6 +407,16 @@ pub fn run_file_capture(path: &Path, opts: &RunOptions) -> Result<RunCapture> {
let stdin_lines = crate::input_feed::effective_stdin(&source, &opts.stdin_lines);
let bind_ctx = BindingContext::from_binds(&opts.binds, cwd_for_path(Some(path)));
let module = load_module_with_ctx(path, &bind_ctx)?;
if let Some(value) = crate::artifact::declaration_summary(&module) {
return Ok(RunCapture {
stdout: String::new(),
value,
plots: Vec::new(),
bindings: Default::default(),
view_trace: Default::default(),
});
}
match opts.backend {
Backend::Tree => {
let mut host = HostContext::for_capture(Some(path), opts.host_caps());
@@ -464,6 +481,16 @@ pub fn run_source(source: &str, opts: &RunOptions) -> Result<RunCapture> {
}
let stdin_lines = crate::input_feed::effective_stdin(source, &opts.stdin_lines);
let module = load_module_from_source(source)?;
if let Some(value) = crate::artifact::declaration_summary(&module) {
return Ok(RunCapture {
stdout: String::new(),
value,
plots: Vec::new(),
bindings: Default::default(),
view_trace: Default::default(),
});
}
match opts.backend {
Backend::Tree => {
let mut host = HostContext::for_capture(None, opts.host_caps());