Fix heading/float block boundaries and record the web trial

The curated trial exposed two parser losses: a heading followed on the next
line by body text dropped the body, and consecutive figure/table/equation
blocks without blank lines swallowed each other. Headings are now separated
from following text and later headings, and the block splitter understands
display environments plus headings, so article-basic keeps its body, figure,
table and equation while arxiv-template moves from 2 to the expected 3 pages.

WEB_ROUTE_D_EXPERIMENT.md now records the three-project results (article-basic,
unicode-mixed, arxiv-template), the Edge smoke test, the go/no-go and the
updated P0 backlog: SVG preview image embedding, .bib data, vector figures and
table-cell math. Test counts move to 1065 (431 engine, 442 core, 64 server).
This commit is contained in:
gloamxun committed 2026-10-27 12:46:48 +08:00
1 parent 8d70868781
commit 254fd4a9a1
5 files changed
+250 -30

No files matched your search

+200 -9
View File
@@ -6756,12 +6756,27 @@ fn expand_par_breaks(body: &str) -> String {
/// Remove a `thebibliography` environment from the body and return its
/// `\bibitem{key}` entries in order.
/// Start a new block when a display environment follows text on the same
/// chunk, e.g. "The documentation is at\n\begin{center}...".
/// chunk, or when display blocks follow each other without a blank line.
fn split_block_environments(chunk: &str) -> Vec<&str> {
const OPENINGS: [&str; 3] = [
const OPENINGS: [&str; 18] = [
"\\begin{center}",
"\\begin{flushleft}",
"\\begin{flushright}",
"\\begin{figure}",
"\\begin{figure*}",
"\\begin{table}",
"\\begin{table*}",
"\\begin{equation}",
"\\begin{equation*}",
"\\begin{align}",
"\\begin{gather}",
"\\begin{multline}",
"\\chapter",
"\\section",
"\\subsection",
"\\subsubsection",
"\\paragraph",
"\\subparagraph",
];
let mut pieces = Vec::new();
let mut rest = chunk;
@@ -6770,15 +6785,50 @@ fn split_block_environments(chunk: &str) -> Vec<&str> {
.iter()
.filter_map(|opening| rest.find(opening).map(|position| (position, *opening)))
.min_by_key(|(position, _)| *position);
match next {
Some((position, _)) if position > 0 && !rest[..position].trim().is_empty() => {
pieces.push(&rest[..position]);
rest = &rest[position..];
let Some((position, opening)) = next else {
if !rest.trim().is_empty() {
pieces.push(rest);
}
_ => break,
break;
};
if position > 0 {
pieces.push(&rest[..position]);
rest = &rest[position..];
continue;
}
if !opening.starts_with("\\begin") {
// A heading has no closing delimiter: split before the next
// heading or display environment, keeping the body with it.
let after = &rest[opening.len()..];
let next = OPENINGS
.iter()
.filter_map(|candidate| {
after
.find(candidate)
.map(|position| (position + opening.len(), *candidate))
})
.min_by_key(|(position, _)| *position);
if let Some((cut, _)) = next {
pieces.push(&rest[..cut]);
rest = rest[cut..].trim_start();
} else {
pieces.push(rest);
break;
}
continue;
}
let closing = opening.replacen("\\begin", "\\end", 1);
let Some(close) = rest.find(&closing) else {
pieces.push(rest);
break;
};
let end = close + closing.len();
pieces.push(&rest[..end]);
rest = rest[end..].trim_start();
while rest.starts_with('\n') {
rest = rest[1..].trim_start();
}
}
pieces.push(rest);
pieces
}
@@ -7939,8 +7989,9 @@ fn paragraphs_with_spacing(
equation_start: u32,
spacing: ListSpacing,
) -> Result<Vec<SourceParagraph>, String> {
let source = separate_heading_lines(source);
let mut paragraphs: Vec<&str> = Vec::new();
for chunk in split_display_chunks(source) {
for chunk in split_display_chunks(&source) {
for piece in split_block_environments(chunk) {
let piece = piece.trim();
if !piece.is_empty() {
@@ -11498,6 +11549,89 @@ fn parse_runs(
}
/// Recognize a `\section`-family command: level, starred, and title.
/// Byte span of a heading command at the start of a line, including any
/// immediately following `\label{...}` groups.
fn heading_span(raw: &str) -> Option<usize> {
let trimmed = raw.trim_start();
for command in [
"\\chapter",
"\\section",
"\\subsection",
"\\subsubsection",
"\\paragraph",
"\\subparagraph",
] {
if !trimmed.starts_with(command) {
continue;
}
let mut position = command.len();
if trimmed[position..].starts_with('*') {
position += 1;
}
while trimmed[position..]
.chars()
.next()
.is_some_and(|character| character.is_whitespace())
{
position += 1;
}
if trimmed[position..].starts_with('[') {
let close = trimmed[position..].find(']')?;
position += close + 1;
}
let rest = &trimmed[position..];
let trimmed_rest = rest.trim_start();
let skipped = rest.len() - trimmed_rest.len();
position += skipped;
let (_, used) = braced_group(&trimmed[position..])?;
position += used;
loop {
let tail = &trimmed[position..];
let trimmed_tail = tail.trim_start();
let Some(after) = trimmed_tail.strip_prefix("\\label") else {
break;
};
let after = after.trim_start();
let Some((_, label_used)) = braced_group(after) else {
break;
};
position += tail.len() - after.len() + label_used;
}
return Some(position + (raw.len() - trimmed.len()));
}
None
}
/// Put each heading on its own paragraph by ensuring a blank line after the
/// heading (and moving any same-line text after it onto the next line).
fn separate_heading_lines(source: &str) -> String {
let mut output = String::with_capacity(source.len() + 16);
let mut in_verbatim = false;
for line in source.lines() {
let trimmed = line.trim_start();
if trimmed.starts_with("\\begin{verbatim}") {
in_verbatim = true;
}
if trimmed.starts_with("\\end{verbatim}") {
in_verbatim = false;
}
if !in_verbatim && let Some(span) = heading_span(line) {
let head = line[..span].trim_end();
let rest = line[span..].trim();
output.push_str(head);
output.push_str("\n\n");
if !rest.is_empty() {
output.push_str(rest);
output.push_str("\n\n");
}
continue;
}
output.push_str(line);
output.push('\n');
}
output
}
fn heading_command(raw: &str) -> Option<(u8, bool, String, Option<String>)> {
let raw = raw.trim();
for (command, level) in [
@@ -17522,6 +17656,63 @@ A definition.
assert!(text.contains("D"), "{text}");
}
#[test]
fn heading_text_without_blank_lines_is_kept() {
let source = "\\documentclass{article}\n\\begin{document}\n\n\\section{One}\nFirst body.\n\\section{Two}\nSecond body.\n\n\\end{document}\n";
let json = preview_json(source).expect("preview");
let value: serde_json::Value = serde_json::from_str(&json).expect("JSON");
assert_eq!(
value["warnings"].as_array().map(Vec::len),
Some(0),
"{}",
value["warnings"]
);
let pages = value["pages"].as_array().expect("pages");
let mut text = String::new();
for page in pages {
if let Some(svg) = page["svg"].as_str() {
text.push_str(
&svg.split('>')
.filter_map(|piece| piece.split('<').next())
.collect::<String>(),
);
}
}
let text: String = text.split_whitespace().collect();
assert!(text.contains("Firstbody."), "{text}");
assert!(text.contains("Secondbody."), "{text}");
assert!(text.contains("2Two"), "{text}");
}
#[test]
fn floats_after_text_without_blank_lines_are_kept() {
let source = "\\documentclass{article}\n\\begin{document}\n\nBefore.\n\\begin{figure}[h]\n\\rule{10pt}{10pt}\n\\caption{Cap}\\label{fig:cap}\n\\end{figure}\n\\begin{table}[h]\n\\begin{tabular}{ll}\nA & B \\\\\n\\end{tabular}\n\\caption{Tab}\\label{tab:cap}\n\\end{table}\n\\begin{equation}\nx = 1\n\\end{equation}\nAfter.\n\n\\end{document}\n";
let json = preview_json(source).expect("preview");
let value: serde_json::Value = serde_json::from_str(&json).expect("JSON");
assert_eq!(
value["warnings"].as_array().map(Vec::len),
Some(0),
"{}",
value["warnings"]
);
let pages = value["pages"].as_array().expect("pages");
let mut text = String::new();
for page in pages {
if let Some(svg) = page["svg"].as_str() {
text.push_str(
&svg.split('>')
.filter_map(|piece| piece.split('<').next())
.collect::<String>(),
);
}
}
let text: String = text.split_whitespace().collect();
assert!(text.contains("Before."), "{text}");
assert!(text.contains("Figure1:Cap"), "{text}");
assert!(text.contains("Table1:Tab"), "{text}");
assert!(text.contains("After."), "{text}");
}
#[test]
fn eqref_wraps_non_equation_labels() {
let source = "\\documentclass{article}\n\\begin{document}\n\n\\section{Intro\\label{sec:a}}\n\nSee \\eqref{sec:a}.\n\n\\end{document}\n";