Open float lists with configurable headings

This commit is contained in:
gloamxun committed 2026-10-25 00:25:50 +08:00
1 parent 0dbca4305b
commit 19c492b11d
4 files changed
+70 -13

No files matched your search

+66 -9
View File
@@ -153,6 +153,10 @@ struct PageGeometry {
ref_name: Option<String>,
/// `\\contentsname` heading for the table of contents.
contents_name: Option<String>,
/// `\\listfigurename` heading for the list of figures.
list_figure_name: Option<String>,
/// `\\listtablename` heading for the list of tables.
list_table_name: Option<String>,
left_skip: Option<i64>,
right_skip: Option<i64>,
tolerance: Option<i64>,
@@ -1032,6 +1036,14 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
geometry.contents_name = Some(name);
continue;
}
if let Some(name) = renewcommand_text(trimmed, "listfigurename") {
geometry.list_figure_name = Some(name);
continue;
}
if let Some(name) = renewcommand_text(trimmed, "listtablename") {
geometry.list_table_name = Some(name);
continue;
}
if trimmed.starts_with("\\newcommand")
|| trimmed.starts_with("\\renewcommand")
|| trimmed.starts_with("\\providecommand")
@@ -4590,7 +4602,11 @@ fn expand_toc(paragraphs: &mut Vec<SourceParagraph>, tocdepth: u32, contents_nam
paragraphs.splice(position..=position, toc);
}
fn expand_float_lists(paragraphs: &mut Vec<SourceParagraph>) {
fn expand_float_lists(
paragraphs: &mut Vec<SourceParagraph>,
figure_heading: &str,
table_heading: &str,
) {
let figures: Vec<String> = paragraphs
.iter()
.filter_map(|paragraph| match &paragraph.float {
@@ -4605,7 +4621,10 @@ fn expand_float_lists(paragraphs: &mut Vec<SourceParagraph>) {
_ => None,
})
.collect();
for (marker, entries) in [("\\texrralof", figures), ("\\texrralot", tables)] {
for (marker, heading, entries) in [
("\\texrralof", figure_heading, figures),
("\\texrralot", table_heading, tables),
] {
let Some(position) = paragraphs
.iter()
.position(|paragraph| paragraph.text.trim() == marker)
@@ -4613,8 +4632,15 @@ fn expand_float_lists(paragraphs: &mut Vec<SourceParagraph>) {
continue;
};
let space_before = paragraphs[position].space_before;
let mut list = Vec::with_capacity(entries.len());
for (index, text) in entries.iter().enumerate() {
if entries.is_empty() {
paragraphs.remove(position);
continue;
}
let mut list = Vec::with_capacity(entries.len() + 1);
let mut heading = bibliography_heading(heading);
heading.space_before = space_before;
list.push(heading);
for text in entries.iter() {
list.push(SourceParagraph {
text: text.clone(),
page_break: false,
@@ -4624,7 +4650,7 @@ fn expand_float_lists(paragraphs: &mut Vec<SourceParagraph>) {
line_height: 12,
math: false,
left_skip: 0,
space_before: if index == 0 { space_before } else { 2 * SCALE },
space_before: 2 * SCALE,
space_stretch: 0,
space_shrink: 0,
equation_number: None,
@@ -4665,7 +4691,17 @@ fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Do
geometry.figure_name.as_deref().unwrap_or("Figure"),
geometry.table_name.as_deref().unwrap_or("Table"),
));
expand_float_lists(&mut paragraphs);
expand_float_lists(
&mut paragraphs,
geometry
.list_figure_name
.as_deref()
.unwrap_or("List of Figures"),
geometry
.list_table_name
.as_deref()
.unwrap_or("List of Tables"),
);
resolve_section_refs(&mut paragraphs, &references);
number_footnotes(&mut paragraphs);
resolve_citations(&mut paragraphs, &geometry.bibliography);
@@ -6406,13 +6442,34 @@ A definition.
1,
"{sources:?}"
);
assert_eq!(sources.len(), 5, "{sources:?}");
assert_eq!(sources.len(), 7, "{sources:?}");
assert!(!sources.iter().any(|source| source.contains("texrralof")));
assert!(!sources.iter().any(|source| source.contains("texrralot")));
assert_eq!(document.units[0].source, "Figure 1: Plot");
assert_eq!(document.units[1].source, "Table 1: Numbers");
assert_eq!(document.units[0].source, "List of Figures");
assert_eq!(document.units[1].source, "Figure 1: Plot");
assert_eq!(document.units[2].source, "List of Tables");
assert_eq!(document.units[3].source, "Table 1: Numbers");
}
#[test]
fn float_list_names_follow_renewcommand() {
let source = "\\renewcommand{\\listfigurename}{Abbildungen}\n\n\\renewcommand{\\listtablename}{Tabellen}\n\n\\listoffigures\n\n\\begin{figure}\n\\caption{Plot}\n\\end{figure}\n\n\\listoftables\n\n\\begin{table}\n\\begin{tabular}{l}\nA\n\\end{tabular}\n\\caption{Numbers}\n\\end{table}";
let (body, geometry) = geometry_and_body(source);
assert_eq!(geometry.list_figure_name.as_deref(), Some("Abbildungen"));
assert_eq!(geometry.list_table_name.as_deref(), Some("Tabellen"));
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!(names.contains(&"Abbildungen"), "{names:?}");
assert!(names.contains(&"Tabellen"), "{names:?}");
assert!(!names.contains(&"List of Figures"), "{names:?}");
assert!(!names.contains(&"List of Tables"), "{names:?}");
}
#[test]
fn secnumdepth_stops_numbering_deep_headings() {
let (body, geometry) =