Use heading short titles in the table of contents

This commit is contained in:
gloamxun committed 2026-10-25 00:27:51 +08:00
1 parent 19c492b11d
commit ea0468783e
4 files changed
+67 -14

No files matched your search

+63 -10
View File
@@ -1790,6 +1790,7 @@ struct SourceParagraph {
heading_starred: bool,
/// The section number assigned by the numbering pass.
heading_number: Option<String>,
heading_short: Option<String>,
/// Footnote bodies extracted from this paragraph, with their numbers.
footnotes: Vec<(u32, String)>,
font_size: Option<i64>,
@@ -2001,6 +2002,7 @@ fn paragraphs_with_definitions(
heading_level: None,
heading_starred: false,
heading_number: None,
heading_short: None,
footnotes: Vec::new(),
theorem: None,
float: None,
@@ -2182,6 +2184,7 @@ fn float_block(raw: &str) -> Option<SourceParagraph> {
heading_level: None,
heading_starred: false,
heading_number: None,
heading_short: None,
footnotes: Vec::new(),
theorem: None,
font_size: None,
@@ -2489,6 +2492,7 @@ fn theorem_block(raw: &str, definitions: &[TheoremDefinition]) -> Option<Vec<Sou
heading_level: None,
heading_starred: false,
heading_number: None,
heading_short: None,
footnotes: Vec::new(),
theorem: Some(TheoremSpec {
name: name.clone(),
@@ -2528,6 +2532,7 @@ fn theorem_block(raw: &str, definitions: &[TheoremDefinition]) -> Option<Vec<Sou
heading_level: None,
heading_starred: false,
heading_number: None,
heading_short: None,
footnotes: Vec::new(),
theorem: None,
font_size: None,
@@ -2738,6 +2743,7 @@ fn list_paragraph(text: String, depth: usize) -> SourceParagraph {
heading_level: None,
heading_starred: false,
heading_number: None,
heading_short: None,
footnotes: Vec::new(),
theorem: None,
float: None,
@@ -2774,6 +2780,7 @@ fn verbatim_blocks(raw: &str) -> Option<Vec<SourceParagraph>> {
heading_level: None,
heading_starred: false,
heading_number: None,
heading_short: None,
font_size: None,
footnotes: Vec::new(),
float: None,
@@ -2887,6 +2894,7 @@ fn standalone_graphic(raw: &str) -> Option<SourceParagraph> {
heading_level: None,
heading_starred: false,
heading_number: None,
heading_short: None,
footnotes: Vec::new(),
theorem: None,
font_size: None,
@@ -2939,6 +2947,7 @@ fn parse_blocks_raw(raw: &str, definitions: &[TheoremDefinition]) -> Vec<SourceP
heading_level: None,
heading_starred: false,
heading_number: None,
heading_short: None,
footnotes: Vec::new(),
theorem: None,
float: None,
@@ -2963,6 +2972,7 @@ fn parse_blocks_raw(raw: &str, definitions: &[TheoremDefinition]) -> Vec<SourceP
heading_level: None,
heading_starred: false,
heading_number: None,
heading_short: None,
footnotes: Vec::new(),
theorem: None,
float: None,
@@ -3026,6 +3036,7 @@ fn parse_blocks_raw(raw: &str, definitions: &[TheoremDefinition]) -> Vec<SourceP
heading_level: None,
heading_starred: false,
heading_number: None,
heading_short: None,
footnotes: Vec::new(),
theorem: None,
float: None,
@@ -3680,7 +3691,7 @@ fn parse_runs(
}
/// Recognize a `\section`-family command: level, starred, and title.
fn heading_command(raw: &str) -> Option<(u8, bool, String)> {
fn heading_command(raw: &str) -> Option<(u8, bool, String, Option<String>)> {
let raw = raw.trim();
for (command, level) in [
("\\section", 1u8),
@@ -3697,16 +3708,16 @@ fn heading_command(raw: &str) -> Option<(u8, bool, String)> {
None => (rest, false),
};
let rest = rest.trim_start();
// Discard the optional short title: `\section[Short]{Long}`.
let rest = match rest.strip_prefix('[') {
Some(short) => match short.split_once(']') {
Some((_, rest)) => rest.trim_start(),
None => rest,
// Keep the optional short title for the table of contents.
let (rest, short) = match rest.strip_prefix('[') {
Some(after) => match after.split_once(']') {
Some((short, rest)) => (rest.trim_start(), Some(short.trim().to_string())),
None => (rest, None),
},
None => rest,
None => (rest, None),
};
let (body, _) = braced_group(rest)?;
return Some((level, starred, body.trim().to_string()));
return Some((level, starred, body.trim().to_string(), short));
}
None
}
@@ -3753,6 +3764,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
heading_level: None,
heading_starred: false,
heading_number: None,
heading_short: None,
footnotes: Vec::new(),
theorem: None,
float: None,
@@ -3781,6 +3793,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
heading_level: None,
heading_starred: false,
heading_number: None,
heading_short: None,
footnotes: Vec::new(),
theorem: None,
float: None,
@@ -3809,6 +3822,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
heading_level: None,
heading_starred: false,
heading_number: None,
heading_short: None,
footnotes: Vec::new(),
theorem: None,
float: None,
@@ -3838,6 +3852,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
heading_level: None,
heading_starred: false,
heading_number: None,
heading_short: None,
footnotes: Vec::new(),
theorem: None,
float: None,
@@ -3871,13 +3886,14 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
heading_level: None,
heading_starred: false,
heading_number: None,
heading_short: None,
footnotes: Vec::new(),
theorem: None,
float: None,
font_size: None,
};
}
if let Some((level, starred, title)) = heading_command(raw) {
if let Some((level, starred, title, short)) = heading_command(raw) {
return SourceParagraph {
text: title,
font: ParagraphFont::Bold,
@@ -3909,6 +3925,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
heading_level: Some(level),
heading_starred: starred,
heading_number: None,
heading_short: short,
footnotes: Vec::new(),
theorem: None,
float: None,
@@ -3943,6 +3960,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
heading_level: None,
heading_starred: false,
heading_number: None,
heading_short: None,
footnotes: Vec::new(),
theorem: None,
float: None,
@@ -3969,6 +3987,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
heading_level: None,
heading_starred: false,
heading_number: None,
heading_short: None,
footnotes: Vec::new(),
theorem: None,
float: None,
@@ -4147,6 +4166,7 @@ fn title_block(text: String, size: f64, space_before: i64) -> SourceParagraph {
heading_level: None,
heading_starred: false,
heading_number: None,
heading_short: None,
footnotes: Vec::new(),
theorem: None,
float: None,
@@ -4502,6 +4522,7 @@ fn bibliography_heading(text: &str) -> SourceParagraph {
heading_level: None,
heading_starred: false,
heading_number: None,
heading_short: None,
footnotes: Vec::new(),
theorem: None,
float: None,
@@ -4538,6 +4559,7 @@ fn append_bibliography(
heading_level: None,
heading_starred: false,
heading_number: None,
heading_short: None,
footnotes: Vec::new(),
theorem: None,
float: None,
@@ -4556,7 +4578,13 @@ fn expand_toc(paragraphs: &mut Vec<SourceParagraph>, tocdepth: u32, contents_nam
&& !paragraph.heading_starred
&& paragraph.heading_number.is_some()
})
.map(|paragraph| (paragraph.heading_level.unwrap_or(1), paragraph.text.clone()))
.map(|paragraph| {
let text = match (&paragraph.heading_short, &paragraph.heading_number) {
(Some(short), Some(number)) => format!("{number} {short}"),
_ => paragraph.text.clone(),
};
(paragraph.heading_level.unwrap_or(1), text)
})
.collect();
let Some(position) = paragraphs
.iter()
@@ -4593,6 +4621,7 @@ fn expand_toc(paragraphs: &mut Vec<SourceParagraph>, tocdepth: u32, contents_nam
heading_level: None,
heading_starred: false,
heading_number: None,
heading_short: None,
font_size: None,
footnotes: Vec::new(),
float: None,
@@ -4660,6 +4689,7 @@ fn expand_float_lists(
heading_level: None,
heading_starred: false,
heading_number: None,
heading_short: None,
font_size: None,
footnotes: Vec::new(),
float: None,
@@ -6156,6 +6186,29 @@ A definition.
);
}
#[test]
fn short_titles_appear_in_the_table_of_contents() {
let source = "\\tableofcontents\n\n\\section[Short]{A much longer title}\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!(names.contains(&"1 Short"), "{names:?}");
assert_eq!(
names
.iter()
.filter(|name| **name == "1 A much longer title")
.count(),
1,
"{names:?}"
);
}
#[test]
fn contents_names_follow_renewcommand() {
let source = "\\renewcommand{\\contentsname}{Inhalt}\n\n\\tableofcontents\n\n\\section{Intro}\n\nText.";