Join and authors and drop thanks notes

This commit is contained in:
gloamxun committed 2026-10-24 20:25:46 +08:00
1 parent b947f476da
commit 17efd542f7
4 files changed
+66 -8

No files matched your search

+62 -4
View File
@@ -1992,6 +1992,51 @@ fn title_large_size(base_font_size: i64) -> f64 {
}
}
/// Remove `\thanks{...}` groups and join multiple authors set with `\and`.
fn clean_title_value(text: &str) -> String {
let mut out = String::new();
let mut rest = text;
loop {
let Some(index) = rest.find("\\thanks") else {
out.push_str(rest);
break;
};
out.push_str(&rest[..index]);
let after = rest[index + "\\thanks".len()..].trim_start();
let Some(group) = after.strip_prefix('{') else {
out.push_str("\\thanks");
rest = &rest[index + "\\thanks".len()..];
continue;
};
let mut depth = 1usize;
let mut end = None;
for (offset, character) in group.char_indices() {
match character {
'{' => depth += 1,
'}' => {
depth -= 1;
if depth == 0 {
end = Some(offset + 1);
break;
}
}
_ => {}
}
}
match end {
Some(end) => rest = &group[end..],
None => {
out.push_str("\\thanks");
rest = &rest[index + "\\thanks".len()..];
}
}
}
let joined = out.replace("\\and", ", ");
let mut normalized = joined.split_whitespace().collect::<Vec<_>>().join(" ");
normalized = normalized.replace(" ,", ",");
normalized.trim().to_string()
}
fn title_block(text: String, size: f64, space_before: i64) -> SourceParagraph {
SourceParagraph {
text,
@@ -2032,8 +2077,11 @@ fn expand_maketitle(blocks: &[SourceParagraph], geometry: &PageGeometry) -> Vec<
continue;
}
if let Some(title) = geometry.title.as_deref().filter(|title| !title.is_empty()) {
let mut paragraph =
title_block(title.to_string(), title_size(base), block.space_before);
let mut paragraph = title_block(
clean_title_value(title),
title_size(base),
block.space_before,
);
paragraph.page_break = block.page_break;
result.push(paragraph);
}
@@ -2044,14 +2092,14 @@ fn expand_maketitle(blocks: &[SourceParagraph], geometry: &PageGeometry) -> Vec<
.filter(|author| !author.is_empty())
{
result.push(title_block(
author.to_string(),
clean_title_value(author),
title_large_size(base),
author_space,
));
}
if let Some(date) = geometry.date.as_deref().filter(|date| !date.is_empty()) {
result.push(title_block(
date.to_string(),
clean_title_value(date),
title_large_size(base),
12 * SCALE,
));
@@ -2801,6 +2849,16 @@ mod source_tests {
assert!(!expanded[3].centered);
}
#[test]
fn maketitle_joins_authors_and_drops_thanks() {
let source = "\\title{Paper}\n\\author{A \\and B \\thanks{Support}}\n\n\\maketitle\n\nBody";
let (body, geometry) = geometry_and_body(source);
let blocks = paragraphs(&body).expect("blocks");
let expanded = expand_maketitle(&blocks, &geometry);
assert_eq!(expanded[1].text, "A, B");
assert!(!expanded[1].text.contains("thanks"));
}
#[test]
fn maketitle_paints_its_title_block() {
let source = "\\title{Paper}\n\\author{Ada}\n\n\\maketitle\n\nBody";