Add bilingual public docs, keyword aliases, and frontmatter stdin for view.

English/Chinese user pages sit side by side; L0 builtins and lib/ text|表 dual APIs land with gold coverage. Capture/view input demos use stdin:/输入: so static export and first page load succeed.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
cflmyandCursor committed 2026-08-05 09:14:26 +08:00
1 parent fd36b92224
commit a5d30c00b3
68 files changed
+1812 -241

No files matched your search

+74 -2
View File
@@ -1,6 +1,7 @@
//! Load `.mq.md` files and merge frontmatter imports.
use std::collections::HashSet;
use std::env;
use std::path::{Path, PathBuf};
use anyhow::{bail, Context, Result};
@@ -30,7 +31,7 @@ fn load_module_inner(path: &Path, visited: &mut HashSet<PathBuf>) -> Result<Modu
let base = path.parent().unwrap_or_else(|| Path::new("."));
let imports = module.imports.clone();
for rel in imports {
let dep_path = base.join(&rel);
let dep_path = resolve_import(base, &rel)?;
let dep = load_module_inner(&dep_path, visited)?;
for fun in dep.functions {
merge_top_level(&mut module.functions, fun);
@@ -40,6 +41,60 @@ fn load_module_inner(path: &Path, visited: &mut HashSet<PathBuf>) -> Result<Modu
Ok(module)
}
/// Resolve an import path: relative to the importer first; `lib/…` and `std/…`
/// also search official library roots (`MARQDO_LIB`, `./lib`, near the binary).
pub fn resolve_import(from_dir: &Path, rel: &str) -> Result<PathBuf> {
let rel = rel.replace('\\', "/");
let direct = from_dir.join(&rel);
if direct.is_file() {
return Ok(direct);
}
let normalized = if let Some(rest) = rel.strip_prefix("std/") {
format!("lib/{rest}")
} else {
rel.clone()
};
if let Some(remainder) = normalized.strip_prefix("lib/") {
for root in lib_search_roots() {
let as_lib_dir = root.join(remainder);
if as_lib_dir.is_file() {
return Ok(as_lib_dir);
}
let as_repo_root = root.join("lib").join(remainder);
if as_repo_root.is_file() {
return Ok(as_repo_root);
}
}
bail!(
"cannot resolve library import `{rel}` (set MARQDO_LIB or keep a lib/ next to the project)"
);
}
Ok(direct)
}
fn lib_search_roots() -> Vec<PathBuf> {
let mut roots = Vec::new();
if let Ok(h) = env::var("MARQDO_LIB") {
roots.push(PathBuf::from(h));
}
if let Ok(cwd) = env::current_dir() {
roots.push(cwd.join("lib"));
roots.push(cwd);
}
if let Ok(exe) = env::current_exe() {
if let Some(dir) = exe.parent() {
roots.push(dir.join("lib"));
roots.push(dir.join("../lib"));
roots.push(dir.join("../../lib"));
roots.push(dir.join("../../../lib"));
}
}
roots
}
fn attach_path(path: &Path, err: anyhow::Error) -> anyhow::Error {
if let Some(d) = err.downcast_ref::<Diagnostic>() {
if d.path.is_none() {
@@ -47,7 +102,6 @@ fn attach_path(path: &Path, err: anyhow::Error) -> anyhow::Error {
}
return err;
}
// Legacy `line:col: message` from parse bail!
let msg = err.to_string();
if let Some((loc, rest)) = msg.split_once(": ") {
if let Some((line_s, col_s)) = loc.split_once(':') {
@@ -66,3 +120,21 @@ fn merge_top_level(into: &mut Vec<Function>, fun: Function) {
into.push(fun);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn resolve_lib_from_cwd() {
let cwd = env::current_dir().unwrap();
let lib_text = cwd.join("lib").join("text.mq.md");
if !lib_text.is_file() {
return;
}
let p = resolve_import(Path::new("tests/keywords"), "lib/text.mq.md").unwrap();
assert!(p.ends_with("text.mq.md"));
let p2 = resolve_import(Path::new("tests/keywords"), "std/text.mq.md").unwrap();
assert!(p2.ends_with("text.mq.md"));
}
}