Number paragraph and subparagraph headings

This commit is contained in:
gloamxun committed 2026-10-24 22:51:14 +08:00
1 parent 0651e15111
commit 4ac3add870
4 files changed
+61 -13

No files matched your search

+57 -9
View File
@@ -3157,6 +3157,8 @@ fn heading_command(raw: &str) -> Option<(u8, bool, String)> {
("\\section", 1u8),
("\\subsection", 2),
("\\subsubsection", 3),
("\\paragraph", 4),
("\\subparagraph", 5),
] {
let Some(rest) = raw.strip_prefix(command) else {
continue;
@@ -3350,7 +3352,13 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
return SourceParagraph {
text: title,
font: ParagraphFont::Bold,
line_height: if level == 1 { 18 } else { 14 },
line_height: if level == 1 {
18
} else if level <= 3 {
14
} else {
12
},
math: false,
left_skip: 0,
centered: false,
@@ -3358,7 +3366,9 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
space_before: match level {
1 => 12 * SCALE,
2 => 10 * SCALE,
_ => 8 * SCALE,
3 => 8 * SCALE,
4 => 6 * SCALE,
_ => 4 * SCALE,
},
space_stretch: 4 * SCALE,
space_shrink: SCALE,
@@ -3615,7 +3625,7 @@ fn expand_maketitle(blocks: &[SourceParagraph], geometry: &PageGeometry) -> Vec<
/// number. Labels inside a heading (or on their own line after it) resolve to
/// that number, and label-only blocks are removed.
fn number_sections(blocks: &mut Vec<SourceParagraph>) -> Vec<(String, String)> {
let mut counters = [0u32; 3];
let mut counters = [0u32; 5];
let mut appendix = false;
let mut labels = Vec::new();
let mut last_number: Option<String> = None;
@@ -3623,7 +3633,7 @@ fn number_sections(blocks: &mut Vec<SourceParagraph>) -> Vec<(String, String)> {
while index < blocks.len() {
if blocks[index].text.trim() == "\\appendix" {
appendix = true;
counters = [0; 3];
counters = [0; 5];
blocks.remove(index);
continue;
}
@@ -3664,10 +3674,14 @@ fn number_sections(blocks: &mut Vec<SourceParagraph>) -> Vec<(String, String)> {
} else {
counters[0].to_string()
};
let number = match level {
1 => first,
2 => format!("{first}.{}", counters[1]),
_ => format!("{first}.{}.{}", counters[1], counters[2]),
let number = if level == 1 {
first
} else {
let mut parts = vec![first];
for depth in 1..level as usize {
parts.push(counters[depth].to_string());
}
parts.join(".")
};
blocks[index].heading_number = Some(number.clone());
last_number = Some(number.clone());
@@ -3889,7 +3903,7 @@ fn expand_toc(paragraphs: &mut Vec<SourceParagraph>) {
let entries: Vec<(u8, String)> = paragraphs
.iter()
.filter(|paragraph| {
paragraph.heading_level.is_some()
paragraph.heading_level.is_some_and(|level| level <= 3)
&& !paragraph.heading_starred
&& paragraph.heading_number.is_some()
})
@@ -5277,6 +5291,40 @@ A definition.
assert_eq!(document.units[1].source, "Table 1: Numbers");
}
#[test]
fn paragraph_headings_number_to_five_levels() {
let source = "\\section{A}\n\n\\paragraph{P}\n\n\\subparagraph{S}\n\n\\subsection{B}\n\n\\paragraph{Q}";
let mut blocks = paragraphs(source).expect("paragraphs");
number_sections(&mut blocks);
let numbers: Vec<&str> = blocks
.iter()
.filter(|block| block.heading_level.is_some())
.map(|block| block.text.as_str())
.collect();
assert_eq!(
numbers,
["1 A", "1.0.0.1 P", "1.0.0.1.1 S", "1.1 B", "1.1.0.1 Q"]
);
}
#[test]
fn table_of_contents_stops_at_subsubsections() {
let source = "\\tableofcontents\n\n\\section{A}\n\n\\paragraph{P}";
let mut blocks = paragraphs(source).expect("paragraphs");
number_sections(&mut blocks);
expand_toc(&mut blocks);
let paragraph_entries = blocks
.iter()
.filter(|block| block.text.trim().ends_with(" P"))
.count();
assert_eq!(paragraph_entries, 1);
assert!(
blocks
.iter()
.all(|block| block.text.trim() != "\\texrratoc")
);
}
#[test]
fn table_of_contents_lists_numbered_headings() {
let source = "\\tableofcontents\n\n\\section{Intro}\n\nText.\n\n\\subsection{Sub}\n\nMore.\n\n\\section*{Notes}\n\nEnd.";