Give titlepage its own pages

A titlepage environment was unsupported, so a front-page block stayed in the
main flow. A body pass now replaces its begin and end with \newpage breaks
(trimming a leading break when the title page opens the document), and the
environment name counts as supported. The title layout itself remains the
content's responsibility.

A preview test puts a title page before the body and checks two pages with both
texts and no warnings.
This commit is contained in:
gloamxun committed 2026-10-27 04:01:59 +08:00
1 parent 0075352832
commit 7975671882
4 files changed
+49 -6

No files matched your search

+43
View File
@@ -6423,6 +6423,7 @@ fn geometry_and_body_with_images(
body = expand_acks(&body);
body = expand_keywords_environment(&body);
body = strip_multicols(&body);
body = expand_titlepage(&body);
body = body
.replace("\\begin{appendices}", "\n\n\\appendix\n\n")
.replace("\\end{appendices}", "\n\n");
@@ -10327,6 +10328,19 @@ fn optional_group(text: &str) -> Option<(String, usize)> {
Some((text[..end].to_string(), end + 1))
}
/// Put a `titlepage` body on its own page by expanding the environment to page
/// breaks; the title layout itself is left to the content.
fn expand_titlepage(body: &str) -> String {
let mut output = body
.replace("\\begin{titlepage}", "\n\n\\newpage\n\n")
.replace("\\end{titlepage}", "\n\n\\newpage\n\n");
if output.trim_start().starts_with("\\newpage") {
let trimmed = output.trim_start();
output = trimmed["\newpage".len()..].trim_start().to_string();
}
output
}
/// Drop a `multicols` wrapper: the engine lays out one column, so the body is
/// kept at the full measure.
fn strip_multicols(body: &str) -> String {
@@ -14057,6 +14071,7 @@ const SUPPORTED_ENVIRONMENTS: &[&str] = &[
"minipage",
"acks",
"keywords",
"titlepage",
"multicols",
"multicols*",
"appendices",
@@ -17481,6 +17496,34 @@ A definition.
assert!(text.contains("D"), "{text}");
}
#[test]
fn titlepage_gets_its_own_page() {
let source = "\\documentclass{article}\n\\begin{document}\n\n\\begin{titlepage}\nTitle page.\n\\end{titlepage}\n\nBody.\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"]
);
assert_eq!(value["pageCount"].as_u64(), Some(2));
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("Titlepage."), "{text}");
assert!(text.contains("Body."), "{text}");
}
#[test]
fn multicols_wrapper_is_consumed() {
let source = "\\documentclass{article}\n\\begin{document}\n\n\\begin{multicols}{2}\nAlpha\n\nBeta\n\\end{multicols}\n\n\\end{document}\n";