From fe7fd6832182f826adb63d1f45d78822bee62fa8 Mon Sep 17 00:00:00 2001 From: GloamXun Date: Tue, 20 Oct 2026 23:09:32 +0800 Subject: [PATCH] Let project previews opt into the route-D core --- README.md | 2 +- docs/TEX_CORE.md | 15 +++++++++------ src/main.rs | 6 ++++++ src/route_d.rs | 13 +++++++++++++ tests/server.rs | 30 ++++++++++++++++++++++++++++++ 5 files changed, 59 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 24aaf54..fc2208e 100644 --- a/README.md +++ b/README.md @@ -568,6 +568,6 @@ It accepts blank-line-separated source with a small `\section`/`\textbf`/`\emph` builder, and returns the v1-compatible preview shape plus per-page hashes and SVG. Route-D paragraphs are laid out with CMR10 advances, kerns and ligatures, and the PDF `/W` array reports the same advances, so extracted text keeps word boundaries. `Document::insertion_classes` caps each insertion class to a page fraction and defers overflow to the next page. The v1 `/preview` and project APIs are unchanged and remain the default. -`POST /api/route-d/edit/{unit}` applies one paragraph edit to the last route-D +Project previews can opt into the new core with `POST /api/projects/{id}/preview?engine=route-d`; the same project preview without the query keeps the v1 pipeline. `POST /api/route-d/edit/{unit}` applies one paragraph edit to the last route-D document and returns the same page list plus `changedPages` and `recomputedPages`, exposing the checkpoint/page-hash diff from the core. diff --git a/docs/TEX_CORE.md b/docs/TEX_CORE.md index 6161bf8..9e8d6bc 100755 --- a/docs/TEX_CORE.md +++ b/docs/TEX_CORE.md @@ -237,7 +237,7 @@ for a later overlaid slash. | M4 math | partial | atoms, scripts, fractions, roots, delimiters, accents, operators, classes, braces, boxed, negated; no math painter/shrink wrapping | | M5 NFSS/TFM | partial | runtime `cmr10.tfm` widths/heights/depths/kern pairs and ff/fi/fl/ffi/ffl ligatures; text faces embed Liberation Serif/Mono as Type0 CID fonts with Identity-H, ToUnicode and width arrays; only faces used on a page are embedded, and ASCII documents use the 110-glyph minimal tier (one-page Roman PDF is about 21 KB); no Base-14 dependency; SVG carries family/weight/style | | M6 specials/XObject | partial | raw specials, RGB image XObjects, URI link annotations; class/package execution absent | -| M7 incremental integration | partial | route-D preview/PDF/edit endpoints with content-based page hashes and section/emphasis/display-math/itemize/enumerate/description/quote/center/flushright mapping; editor integration and inline math pending | +| M7 incremental integration | partial | route-D preview/PDF/edit endpoints plus opt-in `?engine=route-d` project preview, content-based page hashes and section/emphasis/display-math/itemize/enumerate/description/quote/center/flushright mapping; editor toggle UI and inline math pending | v1 remains the production renderer; the route-D core is additive and tested independently. @@ -267,7 +267,7 @@ M3 (partial): `Unit::halign` normalizes column widths, intercolumn gaps, row hei Math coverage includes fractions/roots, scripts, `\left...\right` with body-scaled delimiters, `\middle`, `\big`/`\Big`/`\bigg`/`\Bigg` and their `l`/`r` forms, control-sequence delimiters, accents (dot/check/breve/acute/grave/ring), `\text`/`\mathrm`, `\overbrace`/`\underbrace`, `\overset`/`\underset`, `\binom`, `\cancel`/`\bcancel`/`\xcancel`, `\bmod`/`\pmod`, fence symbols, `\operatorname*` and `\mathop` limits. -The crate currently has 142 unit tests plus one route-D integration test. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 237 tests (142 route-D unit, 1 route-D integration, 71 v1 integration, 18 server, 4 v1 unit, 1 doctest). v1 remains the live server path. +The crate currently has 142 unit tests plus one route-D integration test. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 238 tests (142 route-D unit, 1 route-D integration, 71 v1 integration, 18 server, 4 v1 unit, 1 doctest). v1 remains the live server path. ### M0 incremental demo @@ -301,9 +301,12 @@ deliberately open rather than approximated: metadata, so replaying an earlier page cannot change its hash merely by re-attaching source hashes. - **M7**: `POST /api/route-d/preview`, `/api/route-d/pdf` and - `/api/route-d/edit/{unit}` expose route-D pages, PDF export and edit diff; the preview path lays out - blank-line-separated plain text and returns route-D pages with SVG and page - hashes. It is not yet the editor preview: LaTeX-to-route-D semantic mapping and - edit-diff reuse inside the project preview remain open. + `/api/route-d/edit/{unit}` expose route-D pages, PDF export and edit diff. The + project preview can opt in with `POST /api/projects/{id}/preview?engine=route-d`; + `route_d::latex_preview_json` strips the preamble/body wrapper and hands the + document body to the same route-D paragraph parser. The v1 semantic pipeline + remains the default fallback. LaTeX-to-route-D semantic mapping is still a + subset, the project preview recomputes rather than reusing edit checkpoints, + and there is no editor toggle or pixel comparison yet. - `\middle` does not scale with its enclosing delimiter; math delimiters are scaled glyphs rather than TeX delimiter variants. diff --git a/src/main.rs b/src/main.rs index 5fa3142..9bd0168 100644 --- a/src/main.rs +++ b/src/main.rs @@ -499,6 +499,12 @@ fn handle_project_request( Ok(source) => source, Err(error) => return respond_api_error(request, 422, &error), }; + if query_param(url, "engine").as_deref() == Some("route-d") { + return match route_d::latex_preview_json(&source) { + Ok(json) => respond_json(request, 200, &json), + Err(error) => respond_api_error(request, 422, &error), + }; + } match compiler.preview_json(&source, &file_name) { Ok(json) => respond_json(request, 200, &json), Err(error) => respond( diff --git a/src/route_d.rs b/src/route_d.rs index f2c1b46..f864209 100755 --- a/src/route_d.rs +++ b/src/route_d.rs @@ -20,6 +20,19 @@ thread_local! { static ROUTE_D_SESSION: RefCell> = const { RefCell::new(None) }; } +/// Build a JSON preview from the document body of a small LaTeX file. +/// +/// This is the opt-in M7 bridge for v1 projects: the preamble is discarded and +/// the body is handed to the same route-D paragraph parser used by +/// `/api/route-d/preview`. The v1 semantic pipeline remains the default. +pub fn latex_preview_json(source: &str) -> Result { + let body = source + .split_once("\\begin{document}") + .map_or(source, |(_, body)| body); + let body = body.split("\\end{document}").next().unwrap_or(body); + preview_json(body) +} + /// Build a JSON preview from blank-line-separated plain-text paragraphs. pub fn preview_json(source: &str) -> Result { let paragraphs = paragraphs(source)?; diff --git a/tests/server.rs b/tests/server.rs index 0e832af..172df1b 100644 --- a/tests/server.rs +++ b/tests/server.rs @@ -262,6 +262,36 @@ fn serves_project_api_preview_and_pdf() { assert!(pdf.body.starts_with(b"%PDF-"), "missing PDF header"); } +#[test] +fn project_preview_can_switch_to_route_d() { + let server = TestServer::start("route-d-toggle"); + let preview = server.request( + "POST", + "/api/projects/hello/preview?engine=route-d", + &[], + "application/json", + ); + assert_eq!(preview.status, 200, "{}", preview.text()); + let value: Value = serde_json::from_slice(&preview.body).expect("preview JSON"); + assert_eq!(value["status"], "ok"); + assert!( + value["pages"] + .as_array() + .is_some_and(|pages| !pages.is_empty()) + ); + assert_eq!(value["pageHeightPt"], 792); + + let v1 = server.request( + "POST", + "/api/projects/hello/preview", + &[], + "application/json", + ); + assert_eq!(v1.status, 200); + let v1: Value = serde_json::from_slice(&v1.body).expect("v1 preview JSON"); + assert!((v1["pageWidthMm"].as_f64().unwrap() - 210.0).abs() < 0.1); +} + #[test] fn rejects_static_path_traversal() { let server = TestServer::start("traversal");