Open the tables of contents and bibliography with headings

This commit is contained in:
gloamxun committed 2026-10-25 00:23:41 +08:00
1 parent f23a841e79
commit 0dbca4305b
4 files changed
+151 -17

No files matched your search

+147 -13
View File
@@ -149,6 +149,10 @@ struct PageGeometry {
table_name: Option<String>,
/// `\\abstractname` heading text.
abstract_name: Option<String>,
/// `\\refname` heading for the bibliography.
ref_name: Option<String>,
/// `\\contentsname` heading for the table of contents.
contents_name: Option<String>,
left_skip: Option<i64>,
right_skip: Option<i64>,
tolerance: Option<i64>,
@@ -1020,6 +1024,14 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
geometry.abstract_name = Some(name);
continue;
}
if let Some(name) = renewcommand_text(trimmed, "refname") {
geometry.ref_name = Some(name);
continue;
}
if let Some(name) = renewcommand_text(trimmed, "contentsname") {
geometry.contents_name = Some(name);
continue;
}
if trimmed.starts_with("\\newcommand")
|| trimmed.starts_with("\\renewcommand")
|| trimmed.starts_with("\\providecommand")
@@ -4458,7 +4470,42 @@ fn number_floats(
}
/// Append `thebibliography` entries as numbered paragraphs.
fn append_bibliography(paragraphs: &mut Vec<SourceParagraph>, bibliography: &[(String, String)]) {
fn bibliography_heading(text: &str) -> SourceParagraph {
SourceParagraph {
text: text.to_string(),
page_break: false,
centered: false,
right_aligned: false,
font: ParagraphFont::Bold,
line_height: 14,
math: false,
left_skip: 0,
space_before: 12 * SCALE,
space_stretch: 2 * SCALE,
space_shrink: 0,
equation_number: None,
equation_tag: None,
equation_rows: Vec::new(),
align_centered: false,
heading_level: None,
heading_starred: false,
heading_number: None,
footnotes: Vec::new(),
theorem: None,
float: None,
font_size: None,
}
}
fn append_bibliography(
paragraphs: &mut Vec<SourceParagraph>,
bibliography: &[(String, String)],
heading: &str,
) {
if bibliography.is_empty() {
return;
}
paragraphs.push(bibliography_heading(heading));
for (index, (_, entry)) in bibliography.iter().enumerate() {
paragraphs.push(SourceParagraph {
text: format!("[{}] {entry}", index + 1),
@@ -4469,7 +4516,7 @@ fn append_bibliography(paragraphs: &mut Vec<SourceParagraph>, bibliography: &[(S
line_height: 12,
math: false,
left_skip: 0,
space_before: if index == 0 { 12 * SCALE } else { 4 * SCALE },
space_before: if index == 0 { 4 * SCALE } else { 4 * SCALE },
space_stretch: 2 * SCALE,
space_shrink: 0,
equation_number: None,
@@ -4487,7 +4534,7 @@ fn append_bibliography(paragraphs: &mut Vec<SourceParagraph>, bibliography: &[(S
}
}
fn expand_toc(paragraphs: &mut Vec<SourceParagraph>, tocdepth: u32) {
fn expand_toc(paragraphs: &mut Vec<SourceParagraph>, tocdepth: u32, contents_name: &str) {
let entries: Vec<(u8, String)> = paragraphs
.iter()
.filter(|paragraph| {
@@ -4506,8 +4553,15 @@ fn expand_toc(paragraphs: &mut Vec<SourceParagraph>, tocdepth: u32) {
return;
};
let space_before = paragraphs[position].space_before;
let mut toc = Vec::with_capacity(entries.len());
for (index, (level, text)) in entries.iter().enumerate() {
let mut toc = Vec::with_capacity(entries.len() + 1);
if entries.is_empty() {
paragraphs.remove(position);
return;
}
let mut heading = bibliography_heading(contents_name);
heading.space_before = space_before;
toc.push(heading);
for (level, text) in entries.iter() {
toc.push(SourceParagraph {
text: text.clone(),
page_break: false,
@@ -4517,7 +4571,7 @@ fn expand_toc(paragraphs: &mut Vec<SourceParagraph>, tocdepth: u32) {
line_height: 12,
math: false,
left_skip: 12 * (level.saturating_sub(1) as i64),
space_before: if index == 0 { space_before } else { 2 * SCALE },
space_before: 2 * SCALE,
space_stretch: 0,
space_shrink: 0,
equation_number: None,
@@ -4596,7 +4650,11 @@ fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Do
rename_abstract_heading(&mut paragraphs, name);
}
let section_labels = number_sections(&mut paragraphs, geometry.secnumdepth.unwrap_or(3));
expand_toc(&mut paragraphs, geometry.tocdepth.unwrap_or(3));
expand_toc(
&mut paragraphs,
geometry.tocdepth.unwrap_or(3),
geometry.contents_name.as_deref().unwrap_or("Contents"),
);
let mut references = section_labels;
references.extend(number_theorems(
&mut paragraphs,
@@ -4611,7 +4669,11 @@ fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Do
resolve_section_refs(&mut paragraphs, &references);
number_footnotes(&mut paragraphs);
resolve_citations(&mut paragraphs, &geometry.bibliography);
append_bibliography(&mut paragraphs, &geometry.bibliography);
append_bibliography(
&mut paragraphs,
&geometry.bibliography,
geometry.ref_name.as_deref().unwrap_or("References"),
);
let line_width = geometry.line_width.unwrap_or(480 * SCALE);
let base_units: Vec<Unit> = paragraphs
.iter()
@@ -6034,6 +6096,77 @@ A definition.
));
}
#[test]
fn table_of_contents_starts_with_contents_heading() {
let source = "\\tableofcontents\n\n\\section{Intro}\n\nText.";
let (body, geometry) = geometry_and_body(source);
let paragraphs =
paragraphs_with_definitions(&body, &geometry.theorem_definitions).expect("blocks");
let document = build_document(&paragraphs, &geometry);
let names: Vec<&str> = document
.units
.iter()
.map(|unit| unit.source.as_str())
.collect();
assert_eq!(names[0], "Contents");
assert_eq!(names[1], "1 Intro");
assert_eq!(
document
.units
.iter()
.filter(|unit| unit.source == "Contents")
.count(),
1
);
}
#[test]
fn contents_names_follow_renewcommand() {
let source = "\\renewcommand{\\contentsname}{Inhalt}\n\n\\tableofcontents\n\n\\section{Intro}\n\nText.";
let (body, geometry) = geometry_and_body(source);
assert_eq!(geometry.contents_name.as_deref(), Some("Inhalt"));
let paragraphs =
paragraphs_with_definitions(&body, &geometry.theorem_definitions).expect("blocks");
let document = build_document(&paragraphs, &geometry);
assert_eq!(document.units[0].source, "Inhalt");
}
#[test]
fn bibliography_starts_with_references_heading() {
let source = "See \\cite{alpha}.\n\n\\begin{thebibliography}{9}\n\\bibitem{alpha} Alpha entry.\n\\end{thebibliography}";
let (body, geometry) = geometry_and_body(source);
let paragraphs =
paragraphs_with_definitions(&body, &geometry.theorem_definitions).expect("blocks");
let document = build_document(&paragraphs, &geometry);
let names: Vec<&str> = document
.units
.iter()
.map(|unit| unit.source.as_str())
.collect();
let heading = names.iter().position(|name| *name == "References");
let entry = names.iter().position(|name| *name == "[1] Alpha entry.");
assert!(heading.is_some(), "{names:?}");
assert!(entry.is_some(), "{names:?}");
assert!(heading < entry, "{names:?}");
}
#[test]
fn reference_names_follow_renewcommand() {
let source = "\\renewcommand{\\refname}{Literatur}\n\nSee \\cite{alpha}.\n\n\\begin{thebibliography}{9}\n\\bibitem{alpha} Alpha entry.\n\\end{thebibliography}";
let (body, geometry) = geometry_and_body(source);
assert_eq!(geometry.ref_name.as_deref(), Some("Literatur"));
let paragraphs =
paragraphs_with_definitions(&body, &geometry.theorem_definitions).expect("blocks");
let document = build_document(&paragraphs, &geometry);
assert!(document.units.iter().any(|unit| unit.source == "Literatur"));
assert!(
!document
.units
.iter()
.any(|unit| unit.source == "References")
);
}
#[test]
fn bibliography_entries_resolve_citations() {
let source = "See \\cite{alpha} and \\cite{alpha,beta}.\n\n\\begin{thebibliography}{9}\n\\bibitem{alpha} Alpha entry.\n\\bibitem{beta} Beta entry.\n\\end{thebibliography}";
@@ -6301,7 +6434,7 @@ A definition.
assert_eq!(geometry.tocdepth, Some(1));
let mut blocks = paragraphs_with_definitions(&body, &[]).expect("paragraphs");
number_sections(&mut blocks, 3);
expand_toc(&mut blocks, 1);
expand_toc(&mut blocks, 1, "Contents");
let section = blocks.iter().filter(|block| block.text == "1 A").count();
let subsection = blocks.iter().filter(|block| block.text == "1.1 B").count();
assert_eq!(section, 2, "heading plus one TOC line");
@@ -6331,7 +6464,7 @@ A definition.
let source = "\\tableofcontents\n\n\\section{A}\n\n\\paragraph{P}";
let mut blocks = paragraphs(source).expect("paragraphs");
number_sections(&mut blocks, 3);
expand_toc(&mut blocks, 3);
expand_toc(&mut blocks, 3, "Contents");
let paragraph_entries = blocks
.iter()
.filter(|block| block.text.trim() == "P")
@@ -6378,10 +6511,11 @@ A definition.
"{sources:?}"
);
assert!(!sources.iter().any(|source| source.contains("texrratoc")));
assert_eq!(document.units[0].source, "1 Intro");
assert_eq!(document.units[1].source, "1.1 Sub");
assert_eq!(document.units[0].source, "Contents");
assert_eq!(document.units[1].source, "1 Intro");
assert_eq!(document.units[2].source, "1.1 Sub");
assert_eq!(
document.units[1]
document.units[2]
.paragraph_style
.as_ref()
.unwrap()