refactor: scope markup compatibility per invocation
This commit is contained in:
1 parent
eac9b45263
commit
939e23ee88
9 files changed
+94
-67
No files matched your search
+3
-2
@@ -290,8 +290,9 @@ fn label_error(label: &str, e: anyhow::Error) -> anyhow::Error {
|
||||
/// Effective markup mode declared by the file itself. `<TAB>` M7
|
||||
///
|
||||
/// Looks for `markup: occlusion` (or `标记: 遮蔽`) inside the leading frontmatter
|
||||
/// block. Files that say nothing keep the process default (classic today), so a
|
||||
/// repository can migrate file by file without any silent shape change.
|
||||
/// block. Files that say nothing retain the compatibility default (`classic`)
|
||||
/// until the full library migration lands. There is no environment/process
|
||||
/// default: an explicit file declaration always decides its own grammar.
|
||||
/// Parse a standalone source the way the file itself declares (`markup:` in
|
||||
/// frontmatter). The viewer must use this — otherwise an occluded document is
|
||||
/// re-parsed in classic mode and fails. `<TAB>` M7/M8
|
||||
|
||||
+25
-15
@@ -108,6 +108,11 @@ enum Commands {
|
||||
/// Emit diagnostics as JSON on stderr (machine-readable; AI/MLSP 面).
|
||||
#[arg(long)]
|
||||
json: bool,
|
||||
|
||||
/// Parse unmarked files using the legacy `[key](collection)` grammar.
|
||||
/// Explicit per-file `markup:` still wins; this flag is transitional.
|
||||
#[arg(long)]
|
||||
legacy_markup: bool,
|
||||
},
|
||||
/// Static contract cross-check (渐进式契约): drift, unknown types, misplaced tables
|
||||
Check {
|
||||
@@ -122,6 +127,10 @@ enum Commands {
|
||||
/// Also check prose above `>` calls (implicit occlusion; fuzzy, off by default)
|
||||
#[arg(long)]
|
||||
implicit_labels: bool,
|
||||
|
||||
/// Parse an unmarked file with the legacy grammar for migration review.
|
||||
#[arg(long)]
|
||||
legacy_markup: bool,
|
||||
},
|
||||
/// Occlusion label queries: pending stubs (built-in words) or keyword search
|
||||
Labels {
|
||||
@@ -483,20 +492,7 @@ enum ViewAction {
|
||||
},
|
||||
}
|
||||
|
||||
/// `<TAB>` M2: `MARQDO_MARKUP=occlusion` enables `[描述](实现)` (purity mode).
|
||||
/// Default stays `classic` so legacy `[键](集合)` keeps its meaning until M7.
|
||||
fn init_markup_from_env() {
|
||||
if let Ok(v) = std::env::var("MARQDO_MARKUP") {
|
||||
let m = match v.trim().to_ascii_lowercase().as_str() {
|
||||
"occlusion" | "purity" | "1" | "on" | "true" => marqdo::parse::expr::Markup::Occlusion,
|
||||
_ => marqdo::parse::expr::Markup::Classic,
|
||||
};
|
||||
marqdo::parse::expr::set_markup_default(m);
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> ExitCode {
|
||||
init_markup_from_env();
|
||||
let cli = Cli::parse();
|
||||
let json_errors = matches!(
|
||||
&cli.command,
|
||||
@@ -633,6 +629,7 @@ fn try_main(cli: Cli) -> Result<i32> {
|
||||
binds,
|
||||
fs_root,
|
||||
json: _,
|
||||
legacy_markup,
|
||||
} => {
|
||||
let path = file.unwrap_or_else(|| PathBuf::from("index.mq.md"));
|
||||
let stdin_lines = match stdin_file {
|
||||
@@ -672,15 +669,28 @@ fn try_main(cli: Cli) -> Result<i32> {
|
||||
if dump_all {
|
||||
opts.backend = backend.into();
|
||||
}
|
||||
marqdo::run_file(&path, &opts)?;
|
||||
if legacy_markup {
|
||||
marqdo::parse::expr::with_markup(marqdo::parse::expr::Markup::Classic, || {
|
||||
marqdo::run_file(&path, &opts)
|
||||
})?;
|
||||
} else {
|
||||
marqdo::run_file(&path, &opts)?;
|
||||
}
|
||||
Ok(0)
|
||||
}
|
||||
Commands::Check {
|
||||
file,
|
||||
json,
|
||||
implicit_labels,
|
||||
legacy_markup,
|
||||
} => {
|
||||
let diags = marqdo::check::check_path_opts(&file, implicit_labels)?;
|
||||
let diags = if legacy_markup {
|
||||
marqdo::parse::expr::with_markup(marqdo::parse::expr::Markup::Classic, || {
|
||||
marqdo::check::check_path_opts(&file, implicit_labels)
|
||||
})?
|
||||
} else {
|
||||
marqdo::check::check_path_opts(&file, implicit_labels)?
|
||||
};
|
||||
let mut errors = 0i32;
|
||||
for d in &diags {
|
||||
if matches!(d.severity, marqdo::diagnostics::Severity::Error) {
|
||||
|
||||
+16
-1
@@ -33,7 +33,7 @@ fn has_marker(source: &str) -> bool {
|
||||
}
|
||||
let lower = t.to_ascii_lowercase();
|
||||
if lower.starts_with("markup:") || t.starts_with("标记:") {
|
||||
return true;
|
||||
return lower.contains("occlusion") || lower.contains("purity") || t.contains("遮蔽") || t.contains("纯度");
|
||||
}
|
||||
}
|
||||
false
|
||||
@@ -43,6 +43,21 @@ fn add_marker(source: &str) -> String {
|
||||
if has_marker(source) {
|
||||
return source.to_string();
|
||||
}
|
||||
// A classic marker is an explicit request to migrate; replace it rather
|
||||
// than adding conflicting keys to the same frontmatter block.
|
||||
if source.starts_with("---\n") {
|
||||
let mut lines: Vec<String> = source.lines().map(str::to_string).collect();
|
||||
for (index, line) in lines.iter_mut().enumerate() {
|
||||
let lower = line.trim().to_ascii_lowercase();
|
||||
if lower.starts_with("markup:") || line.trim().starts_with("标记:") {
|
||||
*line = "markup: occlusion".to_string();
|
||||
let mut out = lines.join("\n");
|
||||
if source.ends_with('\n') { out.push('\n'); }
|
||||
return out;
|
||||
}
|
||||
if line.trim() == "---" && index != 0 { break; }
|
||||
}
|
||||
}
|
||||
if let Some(rest) = source.strip_prefix("---\n") {
|
||||
format!("---\nmarkup: occlusion\n{rest}")
|
||||
} else {
|
||||
|
||||
+15
-28
@@ -10,9 +10,10 @@ pub fn parse_expr(input: &str) -> Result<Expr> {
|
||||
|
||||
/// Markup mode for `[描述](实现)` vs the legacy `[键](集合)` index. `<TAB>` M2
|
||||
///
|
||||
/// `Classic` keeps today's index semantics; `Occlusion` reads the same shape as
|
||||
/// an occlusion. The default is `Classic` until M7 migrates the corpus and flips
|
||||
/// the gate (otherwise every legacy index would silently change meaning).
|
||||
/// `Classic` keeps the legacy index semantics; `Occlusion` reads the same shape
|
||||
/// as an occlusion. The compatibility default remains `Classic` until every
|
||||
/// shipped library has an explicit per-file declaration; new syntax opts in
|
||||
/// with `markup: occlusion`. No environment/process switch exists.
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
|
||||
pub enum Markup {
|
||||
#[default]
|
||||
@@ -20,28 +21,17 @@ pub enum Markup {
|
||||
Occlusion,
|
||||
}
|
||||
|
||||
static MARKUP: std::sync::atomic::AtomicU8 = std::sync::atomic::AtomicU8::new(0);
|
||||
|
||||
/// Set the process-wide default markup mode (CLI / tests).
|
||||
pub fn set_markup_default(m: Markup) {
|
||||
MARKUP.store(
|
||||
match m {
|
||||
Markup::Classic => 0,
|
||||
Markup::Occlusion => 1,
|
||||
},
|
||||
std::sync::atomic::Ordering::Relaxed,
|
||||
);
|
||||
}
|
||||
|
||||
thread_local! {
|
||||
/// Per-module override (`markup:` in frontmatter) — wins over the process default.
|
||||
/// Scoped parser context for loader/migration calls. It is never mutated
|
||||
/// from environment/process configuration, so two project files cannot
|
||||
/// silently change each other's grammar.
|
||||
static MARKUP_OVERRIDE: std::cell::Cell<Option<Markup>> = const { std::cell::Cell::new(None) };
|
||||
}
|
||||
|
||||
/// Run `f` with `markup` as the effective mode for everything parsed inside.
|
||||
///
|
||||
/// Frontmatter `markup: occlusion` uses this so that one process can load files
|
||||
/// in different modes (imports!). `<TAB>` M7
|
||||
/// Used by the loader and migration verifier to give one parse an explicit
|
||||
/// mode. Imported files establish their own nested scope from frontmatter.
|
||||
pub fn with_markup<T>(markup: Markup, f: impl FnOnce() -> T) -> T {
|
||||
let saved = MARKUP_OVERRIDE.with(|c| c.replace(Some(markup)));
|
||||
let out = f();
|
||||
@@ -53,10 +43,7 @@ pub fn markup_default() -> Markup {
|
||||
if let Some(m) = MARKUP_OVERRIDE.with(|c| c.get()) {
|
||||
return m;
|
||||
}
|
||||
match MARKUP.load(std::sync::atomic::Ordering::Relaxed) {
|
||||
1 => Markup::Occlusion,
|
||||
_ => Markup::Classic,
|
||||
}
|
||||
Markup::Classic
|
||||
}
|
||||
|
||||
/// Italic / bold value syntax: bare words are variables; text needs `"…"` / `'…'`.
|
||||
@@ -1921,7 +1908,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn link_shaped_index() {
|
||||
let e = parse_expr_prefer_var("[拿铁](菜单)").unwrap();
|
||||
let e = parse_expr_markup("[拿铁](菜单)", true, Markup::Classic).unwrap();
|
||||
match e {
|
||||
Expr::Index { base, label } => {
|
||||
assert!(matches!(base.as_ref(), Expr::Var(n) if n == "菜单"));
|
||||
@@ -1929,7 +1916,7 @@ mod tests {
|
||||
}
|
||||
other => panic!("expected link Index, got {other:?}"),
|
||||
}
|
||||
let e = parse_expr_prefer_var("[`名`](菜单)").unwrap();
|
||||
let e = parse_expr_markup("[`名`](菜单)", true, Markup::Classic).unwrap();
|
||||
match e {
|
||||
Expr::Index { base, label } => {
|
||||
assert!(matches!(base.as_ref(), Expr::Var(n) if n == "菜单"));
|
||||
@@ -1937,7 +1924,7 @@ mod tests {
|
||||
}
|
||||
other => panic!("expected dynamic link Index, got {other:?}"),
|
||||
}
|
||||
let e = parse_expr_prefer_var("[1](篮子)").unwrap();
|
||||
let e = parse_expr_markup("[1](篮子)", true, Markup::Classic).unwrap();
|
||||
assert!(matches!(
|
||||
e,
|
||||
Expr::Index {
|
||||
@@ -1945,7 +1932,7 @@ mod tests {
|
||||
..
|
||||
} if s == "1"
|
||||
));
|
||||
let e = parse_expr_prefer_var("[1]([苹果](货架))").unwrap();
|
||||
let e = parse_expr_markup("[1]([苹果](货架))", true, Markup::Classic).unwrap();
|
||||
match e {
|
||||
Expr::Index { base, label } => {
|
||||
assert_eq!(label, IndexKey::Lit("1".into()));
|
||||
@@ -2015,7 +2002,7 @@ mod bracket_call_tests {
|
||||
|
||||
#[test]
|
||||
fn link_index_not_bracket_call() {
|
||||
let e = parse_expr_prefer_var("[拿铁](菜单)").unwrap();
|
||||
let e = parse_expr_markup("[拿铁](菜单)", true, Markup::Classic).unwrap();
|
||||
assert!(matches!(e, Expr::Index { .. }));
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user