Reuse route-D page checkpoints in project previews

This commit is contained in:
gloamxun committed 2026-10-20 23:13:49 +08:00
1 parent fe7fd68321
commit ea69301804
5 files changed
+139 -12

No files matched your search

+1 -1
View File
@@ -500,7 +500,7 @@ fn handle_project_request(
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) {
return match route_d::project_preview_json(id, &source) {
Ok(json) => respond_json(request, 200, &json),
Err(error) => respond_api_error(request, 422, &error),
};
+86 -5
View File
@@ -5,7 +5,9 @@
//! core. The v1 preview remains the default and the fallback.
use std::cell::RefCell;
use std::collections::HashMap;
use std::fmt::Write as _;
use std::sync::{Mutex, OnceLock};
use texrra_tex_core::cmr10::Cmr10Metrics;
use texrra_tex_core::math::parse_math;
@@ -20,17 +22,96 @@ thread_local! {
static ROUTE_D_SESSION: RefCell<Option<(Document, Engine)>> = const { RefCell::new(None) };
}
struct ProjectSession {
paragraphs: Vec<String>,
engine: Engine,
}
fn project_sessions() -> &'static Mutex<HashMap<String, ProjectSession>> {
static SESSIONS: OnceLock<Mutex<HashMap<String, ProjectSession>>> = OnceLock::new();
SESSIONS.get_or_init(|| Mutex::new(HashMap::new()))
}
fn latex_body(source: &str) -> &str {
let body = source
.split_once("\\begin{document}")
.map_or(source, |(_, body)| body);
body.split("\\end{document}").next().unwrap_or(body)
}
/// Project-scoped route-D preview. Reuses the previous engine when the
/// paragraph list is unchanged and applies exactly one paragraph edit through
/// `Engine::edit`; multi-paragraph or structural edits fall back to a full
/// layout. The v1 project preview remains the default.
pub fn project_preview_json(project_id: &str, source: &str) -> Result<String, String> {
let paragraphs = paragraphs(latex_body(source))?;
let texts: Vec<String> = paragraphs.iter().map(|entry| entry.text.clone()).collect();
let document = build_document(&paragraphs);
let mut sessions = project_sessions()
.lock()
.map_err(|_| "route-D session lock".to_string())?;
let previous = sessions.remove(project_id);
let (mut engine, changed, recomputed) = match previous {
Some(session)
if session.paragraphs.len() == texts.len()
&& session
.paragraphs
.iter()
.zip(&texts)
.filter(|(a, b)| a != b)
.count()
== 0 =>
{
(session.engine, Vec::new(), 0)
}
Some(mut session)
if session.paragraphs.len() == texts.len()
&& session
.paragraphs
.iter()
.zip(&texts)
.filter(|(a, b)| a != b)
.count()
== 1 =>
{
let index = session
.paragraphs
.iter()
.zip(&texts)
.position(|(a, b)| a != b)
.expect("one changed paragraph");
session
.engine
.edit(&document, index as u64, texts[index].clone());
let changed = session.engine.changed_pages().to_vec();
let recomputed = session.engine.recomputed_pages();
(session.engine, changed, recomputed)
}
_ => {
let engine = Engine::layout(&document);
let changed: Vec<usize> = (0..engine.pages().len()).collect();
let recomputed = changed.len();
(engine, changed, recomputed)
}
};
let json = render_json(&engine, Some((&changed, recomputed)));
sessions.insert(
project_id.to_string(),
ProjectSession {
paragraphs: texts,
engine,
},
);
Ok(json)
}
/// 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<String, String> {
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)
preview_json(latex_body(source))
}
/// Build a JSON preview from blank-line-separated plain-text paragraphs.