Resolve section labels and references

This commit is contained in:
gloamxun committed 2026-10-24 20:28:11 +08:00
1 parent 17efd542f7
commit d982e9266c
4 files changed
+94 -17

No files matched your search

+90 -13
View File
@@ -2108,19 +2108,45 @@ fn expand_maketitle(blocks: &[SourceParagraph], geometry: &PageGeometry) -> Vec<
result
}
/// Advance the section counters and prefix each numbered heading with its number.
fn number_sections(blocks: &mut [SourceParagraph]) {
/// Advance the section counters and prefix each numbered heading with its
/// 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];
for block in blocks {
let Some(level) = block.heading_level else {
continue;
};
if block.heading_starred {
let mut labels = Vec::new();
let mut last_number: Option<String> = None;
let mut index = 0;
while index < blocks.len() {
let level = blocks[index].heading_level;
let block_labels = take_labels(&mut blocks[index].text);
if level.is_none() {
if !block_labels.is_empty() && blocks[index].text.trim().is_empty() {
let value = last_number.clone().unwrap_or_else(|| "??".to_string());
for label in block_labels {
labels.push((label, value.clone()));
}
blocks.remove(index);
continue;
}
let value = last_number.clone().unwrap_or_else(|| "??".to_string());
for label in block_labels {
labels.push((label, value.clone()));
}
index += 1;
continue;
}
let index = level as usize - 1;
counters[index] += 1;
for deeper in counters.iter_mut().skip(index + 1) {
let level = level.unwrap();
if blocks[index].heading_starred {
let value = last_number.clone().unwrap_or_else(|| "??".to_string());
for label in block_labels {
labels.push((label, value.clone()));
}
index += 1;
continue;
}
let counter = level as usize - 1;
counters[counter] += 1;
for deeper in counters.iter_mut().skip(counter + 1) {
*deeper = 0;
}
let number = match level {
@@ -2128,8 +2154,37 @@ fn number_sections(blocks: &mut [SourceParagraph]) {
2 => format!("{}.{}", counters[0], counters[1]),
_ => format!("{}.{}.{}", counters[0], counters[1], counters[2]),
};
block.heading_number = Some(number.clone());
block.text = format!("{number} {}", block.text);
blocks[index].heading_number = Some(number.clone());
last_number = Some(number.clone());
for label in block_labels {
labels.push((label, number.clone()));
}
let title = blocks[index].text.trim().to_string();
blocks[index].text = format!("{number} {title}");
index += 1;
}
labels
}
/// Replace section references and turn unresolved `\ref`/`\eqref` into `??`.
fn resolve_section_refs(blocks: &mut [SourceParagraph], labels: &[(String, String)]) {
for block in blocks {
for (label, value) in labels {
block.text = block.text.replace(&format!("\\ref{{{label}}}"), value);
block.text = block.text.replace(&format!("\\eqref{{{label}}}"), value);
}
for command in ["\\ref", "\\eqref"] {
loop {
let prefix = format!("{command}{{");
let Some(start) = block.text.find(&prefix) else {
break;
};
let Some(end) = block.text[start..].find('}') else {
break;
};
block.text.replace_range(start..start + end + 1, "??");
}
}
}
}
@@ -2146,7 +2201,8 @@ fn heading_font_size(level: u8, base: i64) -> i64 {
fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Document {
let mut paragraphs = expand_maketitle(paragraphs, geometry);
number_sections(&mut paragraphs);
let section_labels = number_sections(&mut paragraphs);
resolve_section_refs(&mut paragraphs, &section_labels);
let line_width = geometry.line_width.unwrap_or(480 * SCALE);
let units = paragraphs
.iter()
@@ -2803,6 +2859,27 @@ mod source_tests {
assert_eq!(blocks[1].font_size, Some((9.0 * SCALE as f64) as i64));
}
#[test]
fn section_labels_resolve_from_their_headings() {
let source = "\\section{Intro\\label{sec:intro}}\n\nSee \\ref{sec:intro}.";
let (body, geometry) = geometry_and_body(source);
let blocks = paragraphs(&body).expect("blocks");
let document = build_document(&blocks, &geometry);
assert_eq!(document.units[0].source, "1 Intro");
assert_eq!(document.units[1].source, "See 1.");
}
#[test]
fn standalone_labels_attach_to_the_previous_heading() {
let source = "\\section{A}\n\n\\subsection{B}\n\n\\label{sec:b}\n\nSee \\ref{sec:b} and \\ref{missing}.";
let (body, geometry) = geometry_and_body(source);
let blocks = paragraphs(&body).expect("blocks");
let document = build_document(&blocks, &geometry);
assert_eq!(document.units.len(), 3);
assert_eq!(document.units[1].source, "1.1 B");
assert_eq!(document.units[2].source, "See 1.1 and ??.");
}
#[test]
fn headings_add_vertical_space_and_allow_short_titles() {
let blocks = paragraphs("\\section[Short]{Long title}").expect("blocks");