Expose route-D pages through a preview endpoint

This commit is contained in:
gloamxun committed 2026-10-20 21:15:35 +08:00
1 parent 12288b51ee
commit 875f8a7600
8 files changed
+151 -5

No files matched your search

+22
View File
@@ -9,6 +9,7 @@ use std::{
use tiny_http::{Header, Method, Request, Response, Server, StatusCode};
mod projects;
mod route_d;
use texrra_engine::rust_typeset;
@@ -134,6 +135,27 @@ fn handle_request(
None => respond(request, 404, "text/plain; charset=utf-8", "not found"),
}
}
(&Method::Post, "/api/route-d/preview") => {
let bytes = match read_body_bytes(&mut request, MAX_SOURCE_BYTES) {
Ok(bytes) => bytes,
Err(error) => return respond(request, 413, "text/plain; charset=utf-8", &error),
};
let source = match String::from_utf8(bytes) {
Ok(source) => source,
Err(_) => {
return respond(
request,
400,
"text/plain; charset=utf-8",
"source is not valid UTF-8",
);
}
};
match route_d::preview_json(&source) {
Ok(json) => respond(request, 200, "application/json; charset=utf-8", &json),
Err(error) => respond(request, 422, "application/json; charset=utf-8", &error),
}
}
(&Method::Post, "/preview") => {
let source = match read_and_save(&mut request, input_path) {
Ok(source) => source,
Executable
+91
View File
@@ -0,0 +1,91 @@
//! Route-D preview endpoint: a small, independent TeX-style page pipeline.
//!
//! This is intentionally separate from the v1 `DocumentModel` preview. It
//! demonstrates checkpointed, page-hashed output from the route-D core. The
//! v1 preview remains the default and the fallback.
use std::fmt::Write as _;
use texrra_tex_core::{Document, Engine, ParagraphStyle, Unit};
const SCALE: i64 = 65_536;
const PAGE_HEIGHT_PT: i64 = 792;
/// Build a JSON preview from blank-line-separated plain-text paragraphs.
///
/// Each paragraph is a route-D unit, so `Engine` produces per-page checkpoints
/// and hashes. The response shape deliberately mirrors the useful parts of the
/// v1 preview without pretending to parse LaTeX yet.
pub fn preview_json(source: &str) -> Result<String, String> {
let paragraphs: Vec<&str> = source
.split("\n\n")
.map(str::trim)
.filter(|paragraph| !paragraph.is_empty())
.collect();
if paragraphs.is_empty() {
return Err("route-D preview needs at least one paragraph".to_string());
}
let style = ParagraphStyle {
line_width: 480 * SCALE,
left_skip: 0,
right_skip: 0,
par_indent: 0,
line_height: 12 * SCALE,
char_width: 10 * SCALE,
space_width: 4 * SCALE,
space_stretch: 6 * SCALE,
space_shrink: 2 * SCALE,
};
let units = paragraphs
.iter()
.enumerate()
.map(|(index, paragraph)| {
Unit::paragraph_with_style(index as u64, (*paragraph).to_string(), style.clone())
})
.collect();
let document = Document::new(700 * SCALE, units);
let engine = Engine::layout(&document);
let mut json = String::new();
json.push_str("{\"status\":\"ok\",\"pageWidthMm\":215.90,\"pageHeightMm\":279.40,\"pages\":[");
let mut page_count = 0usize;
for page in engine.pages() {
let svg = page.display_list().to_svg();
if page_count > 0 {
json.push(',');
}
write!(
json,
"{{\"index\":{page_count},\"hash\":\"{:016x}\",\"svg\":\"{}\"}}",
page.hash,
escape_json(&svg)
)
.map_err(|error| error.to_string())?;
page_count += 1;
}
write!(
json,
"],\"pageCount\":{page_count},\"pageHeightPt\":{PAGE_HEIGHT_PT}}}"
)
.map_err(|error| error.to_string())?;
Ok(json)
}
fn escape_json(value: &str) -> String {
let mut escaped = String::with_capacity(value.len());
for character in value.chars() {
match character {
'"' => escaped.push_str("\\\""),
'\\' => escaped.push_str("\\\\"),
'\n' => escaped.push_str("\\n"),
'\r' => escaped.push_str("\\r"),
'\t' => escaped.push_str("\\t"),
character if character < '\u{20}' => {
let _ = write!(escaped, "\\u{:04x}", character as u32);
}
character => escaped.push(character),
}
}
escaped
}